Create a view on a node reference field

This tutorial demonstrates how to use View 2 to query the nodes which are referred from a certain CCK's node reference field, and sorting the referred nodes according to their title.

In this example we will reference Artist to his/her Songs.

  1. Install CCK and enable these modules in CCK: Content and Nodereference.
  2. Create the required content types for this demonstration. Administer >> Content Management >> Content Types >> Add Content Type
    1. Create the content type 'Song'
      • Create a content type 'Song'. No additional fields required.
    2. Create and configure the content type 'Artist'
      • Create a content type 'Artist'. The page reloads to the Content types listing.
      • Click manage fields on the Artist content type. Add a new field called Reference song, field_reference_song. Choose Node reference in the 'Select a field type' drop-down menu. Choose Select list, under the widget pull down menu.
      • Click Save.
      • The page reloads to the settings for this field on the Artist content type.
      • Under Number of values: choose 'Unlimited'.
      • Under Content types that can be referenced: choose 'Song'
      • Save.
    3. Create content to work with
      1. Add several Song nodes for testing purposes.
      2. Add an Artist node and in the node reference field select all the Song nodes.
  3. Create the View
    • Go to Administer >> Site building >> Views >> Add to add a new view. Give it the name 'music view', description 'Music', tag 'users', type 'Node' and click Next.
    • This loads the Views interface.
  4. Set up the relationship. Under Relationships section click + to add a relationship.
    • Below, loads Defaults: Add relationships; Under Groups pull-down menu, select: Content and locate your node reference field, e.g., Content: Reference song (field_reference_song). Click to select and click add. Click 'Require this relationship', and click Update.
  5. Set up the argument. Under Groups: select Node and choose Node: Node ID. Click Add.
  6. Add a field. Click + next to Fields. Under Groups, select Node, and choose Node: Title. Under Relationship, select your node reference field, e.g., Reference song, click Update.
  7. Add Sort Criteria. Under Groups: select Node and choose Node: title, click Add. Click Sort order: Ascending, and click Update.

Your view is ready to test. In the Live preview enter your Artist's node id and you should get all the songs referred from that node sorted in alphabetical order.

Alternate uses: This could be used to get a custom node listing as a block on a Nodereference page.

For example, if there are two given node types Series and Lectures. Series uses node reference to refer to multiple Lectures.

  1. On any given /series/* page, you'll see the Series title, description, and Nodereference list of Lectures.
  2. To use the same teaser display set up for Lectures on the home page, create a view following the steps above; however, using Node display instead of Fields.
  3. Then specify the Argument -> Node: ID with a default argument. Clicking on that, the last radio button is "Provide default argument", then select Node ID from URL as your "Default argument type." Then create a block view.
  4. Last, move the block to the main content region and limited to /series/* pages. Make a blank tpl so that the default Nodereference list won't render. This required the unfortunate name of content-field-field_noderef-series.tpl.php.

In my case, I wanted to

yambag - July 13, 2009 - 18:44

In my case, I wanted to display imagefields from the "song" content type on the artist nodes. On the artist nodes I was using a custom tpl.php... and this is what i did.

For the fields section, instead of node:title choose your song image field.

In your custom.tpl.php do the following:

<?php
$viewName
= 'INSERT_VIEW_NAME'; //whatever you called the view
print views_embed_view($viewName, $display_id, $node->nid); //the last argument here is the song node id
?>

-=-=-=-=-=-=-=-=-=-=-
www.twitter.com/amysteen

A more complex Views

no2x - August 17, 2009 - 15:11

Hi,

thanks for this tutorial - I did rebuild that with success.

But now I have a more complex construction - and I have no idea how to realize it with views:

1) photo node has a node reference to gallery node
and
2) story node has a (multiple) node reference to gallery node

With 1) I can construct Photo Gallerys (those can be viewed separately) like in this tutorial: http://www.lullabot.com/articles/photo-galleries-views-attach.
With 2) I want to link the Photo Gallerys to the Story Contents - If you are viewing a story node, there should be listed at least 3 images of one or more related gallerys. I'd prefer to do that via Views attach modul.

But for now I can only achieve, that the titles of the related Gallerys are shown below the story nodes, not the photos, which reference to those gallerys. Has anybody an idea how to do this?

 
 

Drupal is a registered trademark of Dries Buytaert.