Let's continue the previous tutorial at Create a view on a node reference field with a more advanced View. (If you want to set reference fields to nodes manually, you can use EntityReference in Drupal 7, similar to CCK node references in Drupal 6.)
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.
- Have the content types set up.
- Create a new view 'Related content' displaying the items of type Node.
- In Relationships add
content:field_reference_song
. Set relationship name 'Content reference'. - 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 >> Content ID from URL. Enable the 'Exclude the argument' checkbox. Note that this argument does not need any relationship. - 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.
This code is for Drupal 6:// 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; }
And this code is for Drupal 7:
// Change field_nodereference accordingly return ($node = menu_get_object()) ? $node->field_nodereference['und'][0]['nid'] : FALSE;
- Add relationship to the second argument we added.
- 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. - 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 (for Drupal 6 only):
$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 :
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:
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 Shared Hosting
8 websites powered by drupal 6,8 & 9 - Hosted by 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.
Create a 'related content' View not working on Drupal 7
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:
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 ?
one easy solution for drupal 7
you will now have a block which can be dropped somewhere in your artist pages (set the block to only show on pages 'artist/*'), or better yet, use the Display Suite and take over the manage display of your content type, just create a block field and select your block view and place it in any region you like for any displays you have defined, i.e. teaser, full view, etc.
Thank you, Thank you,Thank
Thank you, Thank you,Thank you!!
this is been very usefull, you saved my day!!
Thank you again!
@blasthaus Your guidle is for
@blasthaus
Your guidle is for "Create a view on a node reference field" http://drupal.org/node/289738. Very good for that. But NOT is "Related content"
"Related content" means: when you see the song 1 of artist A, you will see a block of all songs of artist A (Song 1, song 2, song 3, ...)
My sites: Medicare
I've two content types
I've two content types Authors and Experts (kind of articles). Each "experts " have one authors. I want to show all the experts by one author in a block. Following the tutorial here I try my best but didn't work. See my view code exported
[code]
$view = new view;
$view->name = 'experts_sidebar';
$view->description = '';
$view->tag = '';
$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_expert_nid' => array(
'label' => 'Expert',
'required' => 0,
'delta' => -1,
'id' => 'field_expert_nid',
'table' => 'node_data_field_expert',
'field' => 'field_expert_nid',
'relationship' => 'none',
),
));
$handler->override_option('fields', array(
'title' => array(
'label' => '',
'alter' => array(
'alter_text' => 0,
'text' => '',
'make_link' => 0,
'path' => '',
'link_class' => '',
'alt' => '',
'prefix' => '',
'suffix' => '',
'target' => '',
'help' => '',
'trim' => 0,
'max_length' => '',
'word_boundary' => 1,
'ellipsis' => 1,
'html' => 0,
'strip_tags' => 0,
),
'empty' => '',
'hide_empty' => 0,
'empty_zero' => 0,
'link_to_node' => 1,
'exclude' => 0,
'id' => 'title',
'table' => 'node',
'field' => 'title',
'relationship' => 'none',
),
));
$handler->override_option('arguments', array(
'nid' => array(
'default_action' => 'default',
'style_plugin' => 'default_summary',
'style_options' => array(),
'wildcard' => 'all',
'wildcard_substitution' => 'All',
'title' => '',
'breadcrumb' => '',
'default_argument_type' => 'node',
'default_argument' => '',
'validate_type' => 'none',
'validate_fail' => 'not found',
'break_phrase' => 0,
'not' => 1,
'id' => 'nid',
'table' => 'node',
'field' => 'nid',
'validate_user_argument_type' => 'uid',
'validate_user_roles' => array(
'2' => 0,
'3' => 0,
),
'relationship' => 'none',
'default_options_div_prefix' => '',
'default_argument_fixed' => '',
'default_argument_user' => 0,
'default_argument_cck' => 'nodereference',
'default_argument_cck-nodereference' => 'field_activity_experience',
'content_taxonomy_format' => 'tid',
'argument_default_cck_nodereference_format' => 'nid',
'argument_default_cck_nodereference_delta' => '0',
'param_name' => '',
'default_argument_php' => '',
'validate_argument_node_type' => array(
'faq' => 0,
'accommodation' => 0,
'activity_type' => 0,
'article' => 0,
'authors' => 0,
'blog' => 0,
'booking_enquiry' => 0,
'continent' => 0,
'country' => 0,
'destination_map' => 0,
'experience' => 0,
'experts' => 0,
'experts_main' => 0,
'feature_panel' => 0,
'header_panel' => 0,
'intineraryitem' => 0,
'offer' => 0,
'package' => 0,
'page' => 0,
'story' => 0,
'supplier' => 0,
'video' => 0,
'webform' => 0,
),
'validate_argument_node_access' => 0,
'validate_argument_nid_type' => 'nid',
'validate_argument_vocabulary' => array(
'1' => 0,
),
'validate_argument_type' => 'tid',
'validate_argument_transform' => 0,
'validate_user_restrict_roles' => 0,
'validate_argument_php' => '',
),
'nid_1' => array(
'default_action' => 'default',
'style_plugin' => 'default_summary',
'style_options' => array(),
'wildcard' => 'all',
'wildcard_substitution' => 'All',
'title' => '',
'breadcrumb' => '',
'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',
'validate_user_argument_type' => 'uid',
'validate_user_roles' => array(
'2' => 0,
'3' => 0,
),
'relationship' => 'field_expert_nid',
'default_options_div_prefix' => '',
'default_argument_fixed' => '',
'default_argument_user' => 0,
'default_argument_cck' => 'nodereference',
'default_argument_cck-nodereference' => 'field_activity_experience',
'content_taxonomy_format' => 'tid',
'argument_default_cck_nodereference_format' => 'nid',
'argument_default_cck_nodereference_delta' => '0',
'param_name' => '',
'default_argument_php' => '$field_name = \'field_expert\'; // <--- 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;
}
}',
'validate_argument_node_type' => array(
'faq' => 0,
'accommodation' => 0,
'activity_type' => 0,
'article' => 0,
'authors' => 0,
'blog' => 0,
'booking_enquiry' => 0,
'continent' => 0,
'country' => 0,
'destination_map' => 0,
'experience' => 0,
'experts' => 0,
'experts_main' => 0,
'feature_panel' => 0,
'header_panel' => 0,
'intineraryitem' => 0,
'offer' => 0,
'package' => 0,
'page' => 0,
'story' => 0,
'supplier' => 0,
'video' => 0,
'webform' => 0,
),
'validate_argument_node_access' => 0,
'validate_argument_nid_type' => 'nid',
'validate_argument_vocabulary' => array(
'1' => 0,
),
'validate_argument_type' => 'tid',
'validate_argument_transform' => 0,
'validate_user_restrict_roles' => 0,
'validate_argument_php' => '',
),
));
$handler->override_option('filters', array(
'type' => array(
'operator' => 'in',
'value' => array(
'experts' => 'experts',
),
'group' => '0',
'exposed' => FALSE,
'expose' => array(
'operator' => FALSE,
'label' => '',
),
'id' => 'type',
'table' => 'node',
'field' => 'type',
'relationship' => 'none',
),
));
$handler->override_option('access', array(
'type' => 'none',
));
$handler->override_option('cache', array(
'type' => 'none',
));
$handler = $view->new_display('block', 'Block', 'block_1');
$handler->override_option('block_description', '');
$handler->override_option('block_caching', -1);
[/code]
Any help? I'm using D6 and Views 2