Community Documentation

Create a 'related content' View.

Last updated April 19, 2009. Created by Amitaibu on February 6, 2009.
Log in to edit this page.

Let's continue the previous tutorial at Create a view on a node reference field with a more advanced View. First let's define what we already have and what we need.

We have a Songs content type with a node reference field, that references an Artist content type.
What we now want is that when a user enters a Song node, she will see all the other songs that belong to the Artist.

  1. Have the content types set up.
  2. Create a new view 'Related content' displaying the items of type Node.
  3. In Relationships add content:field_reference_song. Set relationship name 'Content reference'.
  4. The first argument that we want to pass is the existing node. This node shouldn't, obviously, appear in the Related content block. In Arguments add Node:nid and Provide default argument >> Node ID from URL. Enable the 'Exclude the argument' checkbox. Note that this argument does not need any relationship.
  5. Now we want to get all the nodes that are referencing the Artist node. In Argument add a second Node:Nid and set the relationship to 'Content reference'. We need to pass the node ID of the Artist node using some PHP (Provide default argument >> PHP code) . Remember to remove the <?php ?> when pasting the code.
    <?php
    // The referencing field name.
    $field_name = 'field_multi_content_reference'; // <--- Change this to your field name.
    if ($node = menu_get_object()) {
     
    // Pass the first node id on the content reference.
     
    return !empty($node->{$field_name}[0]['nid']) ? $node->{$field_name}[0]['nid'] : NULL;
    }
    ?>
  6. Add relationship to the second argument we added.
  7. Setup the View to use Table and add some fields such as the title (i.e. you will see the title of the song), and create a Block view based on this Default view. Note that since we use menu_get_object() you can't check the View in the Live preview - you will need to check it on the real node view.
  8. In the blocks admin enable the block and navigate to a song node, and enjoy your new Related content block.

Here's an export of a View you can import:

$view = new view;
$view->name = 'gizra_multi_content_reference';
$view->description = 'Get all the content referenced through the content reference field';
$view->tag = 'gizra';
$view->view_php = '';
$view->base_table = 'node';
$view->is_cacheable = FALSE;
$view->api_version = 2;
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
$handler = $view->new_display('default', 'Defaults', 'default');
$handler->override_option('relationships', array(
  'field_multi_content_reference_nid' => array(
    'label' => 'Content reference',
    'required' => 1,
    'delta' => '-1',
    'id' => 'field_multi_content_reference_nid',
    'table' => 'node_data_field_multi_content_reference',
    'field' => 'field_multi_content_reference_nid',
    'relationship' => 'none',
  ),
));
$handler->override_option('fields', array(
  'title' => array(
    'label' => 'Title',
    'link_to_node' => 1,
    'exclude' => 0,
    'id' => 'title',
    'table' => 'node',
    'field' => 'title',
    'relationship' => 'none',
  ),
));
$handler->override_option('sorts', array(
  'title' => array(
    'id' => 'title',
    'table' => 'node',
    'field' => 'title',
  ),
));
$handler->override_option('arguments', array(
  'nid' => array(
    'default_action' => 'not found',
    'style_plugin' => 'default_summary',
    'style_options' => array(),
    'wildcard' => 'all',
    'wildcard_substitution' => 'All',
    'title' => '',
    'default_argument_type' => 'php',
    'default_argument' => '',
    'validate_type' => 'none',
    'validate_fail' => 'not found',
    'break_phrase' => 0,
    'not' => 0,
    'id' => 'nid',
    'table' => 'node',
    'field' => 'nid',
    'relationship' => 'field_multi_content_reference_nid',
    'default_options_div_prefix' => '',
    'default_argument_user' => 0,
    'default_argument_fixed' => '',
    'default_argument_php' => '',
    'validate_argument_node_type' => array(
      'product' => 0,
      'book' => 0,
      'cluster' => 0,
      'grading_rule' => 0,
      'group' => 0,
      'image' => 0,
      'marker' => 0,
      'page' => 0,
      'spec' => 0,
      'story' => 0,
      'style' => 0,
      'task' => 0,
    ),
    'validate_argument_node_access' => 0,
    'validate_argument_nid_type' => 'nid',
    'validate_argument_vocabulary' => array(
      '3' => 0,
    ),
    'validate_argument_type' => 'tid',
    'validate_argument_is_member' => 0,
    'validate_argument_php' => 'dpm ($handler->argument);
                                          if ($node = menu_get_object() && $node->type == \'style) {
                                            $handler->argument = $node->nid;
                                          }',
  ),
  'type' => array(
    'default_action' => 'ignore',
    'style_plugin' => 'default_summary',
    'style_options' => array(),
    'wildcard' => 'all',
    'wildcard_substitution' => 'All',
    'title' => '',
    'default_argument_type' => 'fixed',
    'default_argument' => '',
    'validate_type' => 'none',
    'validate_fail' => 'not found',
    'id' => 'type',
    'table' => 'node',
    'field' => 'type',
    'relationship' => 'none',
    'default_options_div_prefix' => '',
    'default_argument_user' => 0,
    'default_argument_fixed' => '',
    'default_argument_php' => '',
    'validate_argument_node_type' => array(
      'product' => 0,
      'book' => 0,
      'cluster' => 0,
      'grading_rule' => 0,
      'group' => 0,
      'image' => 0,
      'marker' => 0,
      'page' => 0,
      'spec' => 0,
      'story' => 0,
      'style' => 0,
      'task' => 0,
    ),
    'validate_argument_node_access' => 0,
    'validate_argument_nid_type' => 'nid',
    'validate_argument_vocabulary' => array(
      '3' => 0,
    ),
    'validate_argument_type' => 'tid',
    'validate_argument_is_member' => 0,
    'validate_argument_php' => '',
    'override' => array(
      'button' => 'Override',
    ),
  ),
));
$handler->override_option('filters', array(
  'status' => array(
    'operator' => '=',
    'value' => 1,
    'group' => '0',
    'exposed' => FALSE,
    'expose' => array(
      'operator' => FALSE,
      'label' => '',
    ),
    'id' => 'status',
    'table' => 'node',
    'field' => 'status',
    'relationship' => 'none',
  ),
));
$handler->override_option('access', array(
  'type' => 'none',
  'role' => array(),
  'perm' => '',
));
$handler->override_option('header', '<?php
       
if ($node = menu_get_object()){
         
// The referencing field name.
         
$field_name = field_multi_content_reference;
         
$ref_node = node_load($node->{$field_name}[0][nid]);
          print
strtr(Other content referencing <a href="@node-ref">@ref</a>., array(@node-ref => url(node/. $ref_node->nid), @ref => check_plain($ref_node->title)));
        }
       
?>
');
$handler->override_option('header_format', '4');
$handler->override_option('header_empty', 0);
$handler->override_option('items_per_page', 0);
$handler->override_option('style_options', NULL);
$handler = $view->new_display('block', 'Related content block', 'block_2');
$handler->override_option('relationships', array(
  'field_multi_content_reference_nid' => array(
    'label' => 'Content reference',
    'required' => 0,
    'delta' => '-1',
    'id' => 'field_multi_content_reference_nid',
    'table' => 'node_data_field_multi_content_reference',
    'field' => 'field_multi_content_reference_nid',
    'relationship' => 'none',
    'override' => array(
      'button' => 'Use default',
    ),
  ),
));
$handler->override_option('fields', array(
  'title' => array(
    'label' => 'Title',
    'link_to_node' => 1,
    'exclude' => 0,
    'id' => 'title',
    'table' => 'node',
    'field' => 'title',
    'relationship' => 'none',
    'override' => array(
      'button' => 'Use default',
    ),
  ),
));
$handler->override_option('arguments', array(
  'nid' => array(
    'default_action' => 'default',
    'style_plugin' => 'default_summary',
    'style_options' => array(),
    'wildcard' => 'all',
    'wildcard_substitution' => 'All',
    'title' => '',
    'default_argument_type' => 'php',
    'default_argument' => '',
    'validate_type' => 'none',
    'validate_fail' => 'not found',
    'break_phrase' => 0,
    'not' => 1,
    'id' => 'nid',
    'table' => 'node',
    'field' => 'nid',
    'override' => array(
      'button' => 'Use default',
    ),
    'relationship' => 'none',
    'default_options_div_prefix' => '',
    'default_argument_user' => 0,
    'default_argument_fixed' => '',
    'default_argument_php' => '// Pass the argument only if we are on node view page.
                      $item = menu_get_item();
                      return $item[\'path\'] == \'node/%\' ? $item[\'page_arguments\'][0]->nid : NULL;',
    'validate_argument_node_type' => array(
      'product' => 0,
      'forum' => 0,
      'book' => 0,
      'cluster' => 0,
      'grading_rule' => 0,
      'group' => 0,
      'image' => 0,
      'marker' => 0,
      'page' => 0,
      'spec' => 0,
      'story' => 0,
      'style' => 0,
      'task' => 0,
    ),
    'validate_argument_node_access' => 0,
    'validate_argument_nid_type' => 'nid',
    'validate_argument_vocabulary' => array(
      '13' => 0,
      '23' => 0,
      '21' => 0,
      '24' => 0,
      '29' => 0,
      '32' => 0,
      '34' => 0,
      '11' => 0,
      '8' => 0,
      '19' => 0,
      '30' => 0,
      '35' => 0,
      '22' => 0,
      '4' => 0,
      '5' => 0,
      '7' => 0,
      '10' => 0,
      '12' => 0,
      '17' => 0,
      '18' => 0,
      '9' => 0,
      '14' => 0,
      '20' => 0,
      '25' => 0,
      '31' => 0,
      '33' => 0,
      '36' => 0,
      '27' => 0,
      '37' => 0,
      '26' => 0,
      '28' => 0,
      '16' => 0,
    ),
    'validate_argument_type' => 'tid',
    'validate_argument_is_member' => 0,
    'validate_argument_php' => '',
  ),
  'nid_1' => array(
    'default_action' => 'default',
    'style_plugin' => 'default_summary',
    'style_options' => array(),
    'wildcard' => 'all',
    'wildcard_substitution' => 'All',
    'title' => '',
    'default_argument_type' => 'php',
    'default_argument' => '',
    'validate_type' => 'none',
    'validate_fail' => 'not found',
    'break_phrase' => 0,
    'not' => 0,
    'id' => 'nid_1',
    'table' => 'node',
    'field' => 'nid',
    'override' => array(
      'button' => 'Use default',
    ),
    'relationship' => 'field_multi_content_reference_nid',
    'default_options_div_prefix' => '',
    'default_argument_user' => 0,
    'default_argument_fixed' => '',
    'default_argument_php' => '// The referencing field name.
                            $field_name = \'field_multi_content_reference\';
                            if ($node = menu_get_object()) {
                              // Pass the first node id on the content reference.
                              return !empty($node->{$field_name}[0][\'nid\']) ? $node->{$field_name}[0][\'nid\'] : NULL;
                            }',
    'validate_argument_node_type' => array(
      'product' => 0,
      'forum' => 0,
      'book' => 0,
      'cluster' => 0,
      'grading_rule' => 0,
      'group' => 0,
      'image' => 0,
      'marker' => 0,
      'page' => 0,
      'spec' => 0,
      'story' => 0,
      'style' => 0,
      'task' => 0,
    ),
    'validate_argument_node_access' => 0,
    'validate_argument_nid_type' => 'nid',
    'validate_argument_vocabulary' => array(
      '13' => 0,
      '23' => 0,
      '21' => 0,
      '24' => 0,
      '11' => 0,
      '8' => 0,
      '16' => 0,
      '19' => 0,
      '27' => 0,
      '22' => 0,
      '4' => 0,
      '5' => 0,
      '7' => 0,
      '10' => 0,
      '12' => 0,
      '15' => 0,
      '17' => 0,
      '18' => 0,
      '9' => 0,
      '14' => 0,
      '20' => 0,
      '25' => 0,
      '26' => 0,
    ),
    'validate_argument_type' => 'tid',
    'validate_argument_is_member' => 0,
    'validate_argument_php' => '',
  ),
));
$handler->override_option('title', 'Related content');
$handler->override_option('style_plugin', 'table');
$handler->override_option('style_options', array(
  'grouping' => '',
  'override' => 1,
  'sticky' => 1,
  'order' => 'asc',
  'columns' => array(
    'field_icon_image_fid' => 'field_icon_image_fid',
    'title' => 'title',
    'type' => 'type',
  ),
  'info' => array(
    'field_icon_image_fid' => array(
      'separator' => '',
    ),
    'title' => array(
      'sortable' => 1,
      'separator' => '',
    ),
    'type' => array(
      'sortable' => 1,
      'separator' => '',
    ),
  ),
  'default' => 'title',
));
$handler->override_option('block_description', 'Related content');
$handler->override_option('block_caching', -1);

Comments

Thanks for this great

Thanks for this great tutorial !
I've modified a little bit the PHP argument code like this :

<?php
$field_name
= 'field_multi_content_reference'; // <--- Change this to your field name.
if ($node = node_load($view->args[0])) {
 
$refs = "";
  foreach(
$node->{$field_name} as $ref){
    if(!empty(
$ref['nid'])){
       
$refs .= $ref['nid'].",";
    }
  }
  if(
$refs != ""){
    return
substr($refs,0,-1);
  }
  else{
    return
NULL;
  }
}
?>

I've changed the method to retreive the node from the first argument. Now you can place the block anywhere, and it works in preview mode.
I also add a loop to look at every references. So, now you also have to check the "Allow multiple terms per argument" box in the argument settings.

Fantastic!

Your update here worked for me - thanks so much! This is the only tutorial I've found showing how to do this...many more doing the easier part of getting the "Songs" to show up on the "Artist" page.

Thanks for this code!

I really appreciate all the help this thread has given me, I was struggling to do exactly this.

I did notice this PHP argument code creates an error on any page without the node reference field. I have edited the code to eliminate the error below:

<?php
$field_name
= 'field_multi_content_reference'; // <--- Change this to your field name.
if ($node = node_load($view->args[0])) {
 
$refs = "";
  if (
$node->{$field_name})
  {
    foreach(
$node->{$field_name} as $ref){
      if(!empty(
$ref['nid'])){
         
$refs .= $ref['nid'].",";
      }
    }
  }
  if(
$refs != ""){
    return
substr($refs,0,-1);
  }
  else{
    return
NULL;
  }
}
?>

This code is great

I was looking for this solution the whole day. But I need one last thing. How can I also display the displayed node in the viewslist. I need this for some kind of navigation.

Thank you very much.

This worked for me. Thanks

This worked for me. Thanks

A little bit confusing to me

A little bit confusing to me for the content set up.

Looks like:

For song type, we have a node reference field, which refers to artist; and

For artist type, we have a node reference field, which refers to song.

Am I right?

Thanks

Anohter thanks

I'd been messing around for ages and this provided what I needed for a simple issue/article type structure and this article provided me with the info I needed. I used the revised code above.

little question..

hi

in my case i have:

Song content with noderef field = Event content
Artist content = Study content

i would to display on the study content page a list of lstest event related to him (via noderef)...so i dont see what is about the field_multi_content_reference ?

in my case the study content is created before event content, and it's a "static" content ( change one time per year)...

if someone can explain to me..

thanks

Dev Server Ubuntu 10.04 LAMP PHP 5.2.10 ( 1 Laptop)
7 websites powered by drupal 6 - Hosted by OVH and Always Data

Not sure I understand...

I am not sure I understand exactly what you are asking. It sounds like you are confused as to what the "field_multi_content_reference" means.

In the php code replace "field_multi_content_reference" with the machine name of the node reference field in your Event content type.

Hi fellows,

1. In previous tutorial, I've set up two content types Song and Artist, and added a node reference field in Artist referencing
Song, it works fine. For this tutorial, I've set a node reference field in content type Song referencing content type Artist, and have
put couple of sample records both in Song and Artist.
(so this means that Song and Artist need to reference each other for accomplishing this task, am I right?)

2. I followed all the in this tutorial and couldn't get work done. After struggling days, I found out:
even condition
($node=menu_get_object()) is TRUE,
($node->{$field_name}[0]['nid']) returns EMPTY

therefore the PHP code returns NULL.
(here I did assigned $field_name with my own defined node reference field ) basically returns
empty.

So, I am wondering whether accessing CCK Nodereference fields in Drupal 7 works differently as you said
in this doc under circumstance of Drupal 6.x?
Thanks.

Notice 'und'

You may want to add 'und' as below to get it working for drupal 7:

// Change field_nodereference accordingly
return ($node = menu_get_object()) ? $node->field_nodereference['und'][0]['nid'] : FALSE;

Add it to the second argument.

love, light n laughter

Need help applying this to drupal 7 with views 3

The problem I am having is that, when i set up the contextual filter(arguments in views3) to provide default value of content id from url. It displays on the b content page of the first referenced field but not any of the other unlimited references. I have the field set to unlimited values on node reference because it can be referenced more than one of the b content type, but the block does not display any of the other references, only the first. How do correct this.

Figured out?

I'm having trouble getting Magazine "Issues" to list out all the "Articles" that reference it.

How can I change the code, to

How can I change the code, to include the actual displayed node in the viewslist?

Edit: I got it, only delete the first argument ;)

Did anyone find a solution for drupal 7 ?

Did anyone find a solution for drupal 7 ?