Blair Wadman 3 minute read

Node relativity view children in parent node

You can use node relativity to attach nodes to other nodes. In this case, I am using it to attach reviews to product nodes, where the reviews are nodes in their own right. I want to be able to view the reviews (children) within the product node (parent).

There is a view (views.module) to control the display of the review nodes. Then, within the node.tpl template, add the 2nd part of the snippet (following //SNIPPET FOR NODE.TPL)

//VIEWS EXPORT
  $view = new stdClass();
  $view->name = 'reviews_of_node';
  $view->description = '';
  $view->access = array (
);
  $view->view_args_php = '';
  $view->page = TRUE;
  $view->page_title = '';
  $view->page_header = '';
  $view->page_header_format = '3';
  $view->page_footer = '';
  $view->page_footer_format = '3';
  $view->page_empty = '<p>No reviews found.</p>';
  $view->page_empty_format = '3';
  $view->page_type = 'node';
  $view->url = 'review';
  $view->use_pager = TRUE;
  $view->nodes_per_page = '10';
  $view->block = TRUE;
  $view->block_title = '';
  $view->block_header = '';
  $view->block_header_format = '3';
  $view->block_footer = '';
  $view->block_footer_format = '3';
  $view->block_empty = '';
  $view->block_empty_format = '3';
  $view->block_type = 'node';
  $view->nodes_per_block = '100';
  $view->block_more = FALSE;
  $view->block_use_page_header = FALSE;
  $view->block_use_page_footer = FALSE;
  $view->block_use_page_empty = FALSE;
  $view->sort = array (
    array (
      'tablename' => 'node',
      'field' => 'changed',
      'sortorder' => 'DESC',
      'options' => 'normal',
    ),
  );
  $view->argument = array (
    array (
      'type' => 'relativity_parent',
      'argdefault' => '7',
      'title' => '%1 Review',
      'options' => '',
      'wildcard' => '',
      'wildcard_substitution' => '',
    ),
  );
  $view->field = array (
  );
  $view->filter = array (
    array (
      'tablename' => 'node',
      'field' => 'status',
      'operator' => '=',
      'options' => '',
      'value' => '1',
    ),
  );
  $view->exposed_filter = array (
  );
  $view->requires = array(node);
  $views[$view->name] = $view;



<div class="review-link">
<?php
  print l("Add your review of the $model","node/add/review/parent/$node->nid");
?>
</div>
 <?php
        // Retrieve the view
        $view = views_get_view('reviews_of_node');

        // Build the view into the $reviews variable with paging on and nodes per page set to 20
        $reviews = views_build_view('block', $view, array(strval($node->nid)), true, 20);

        // Get the number of reviews
        global $pager_total_items;
        $numreviews = $pager_total_items[0];

        // Print the reviews
        echo "<h3>Customer Reviews for the $model</h3>";
        if ($numreviews != 0) {
            print "Number of reviews: $numreviews";
            print $reviews;
            print "<p>Read all: ".l("$model reviews", "review/$node->nid")."</p>";
        }else{
            print "<p>There are currently no reviews for the $model. Be the first: ".l("Add a Review for the $model","node/add/review/parent/$node->nid")."</p>";
        }
}
  }
?>