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 'MusicView', description 'Music', and type 'Node' and click Next.
    • This loads the Views interface.
    • Add a Page display, with a path of 'artists' in the "Page settings" section.
  4. Set up the relationship. Under the Relationships section click + to add a relationship.
    • Under "Add relationships" you will see all fields listed.
    • Drop down the Groups menu and select "Content".
    • Locate your node reference field, for example 'Content: Reference song (field_reference_song)'. Check the box to the left of it.
    • Click the Add button.
    • Check the box labeled 'Require this relationship', and click the Update button.
  5. Set up the argument. Under Groups: select Node and choose Node: Node ID. Click Add. When presented with the Argument Configuration options, click "Update".
  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?

Anyone tried using an

giorgio79 - November 24, 2009 - 05:54

Anyone tried using an Editview? So that other users can add and edit node references on the fly?

I wonder if it would work : http://drupal.org/project/editview

My Drupal sites:
Review Critical
ClipGlobe - World Travel
I created these 100% from concept, to design and build on Drupal.

Filled a big hole for me - thanks

dr_thomas64 - December 10, 2009 - 10:06

I spent 2 days banging away at this problem before I noticed the unoffiicial tutorial section. Really good and easy to follow tutorial - and it worked. Now I just have to go back and understand why. It does leave me with a tricky issue though.

The reason that I couldn't get my view to work (I think) is that I wanted to organise my 'songs' by the 'artist' (or artists) that they referred to - rather than by the artists that referred to them . Having reversed the relationship, so that the artist node references the song node, the view works but I have a data maintenance problem. When I add a song node, I have to go separately to the artist node and add a reference to that song. Over time an artist may write a lot of songs and just to malke matters worse, in my case one song can be written by an unlimited number of artists.

Obviously my app's not really about songs and artists - it's about travel destinations and journey segments (or legs). You MIGHT see what I mean if you take a look at http://noairtravel.com and follow the 'choose a destination link'. I say 'might' because there's very little data there yet.

Anyway, re data maintenance issue, I imagine there's a way to construct a user interface that updates 2 related nodes at once - so I'm going to try and hunt that down.

You could also try a module

davpas - January 14, 2010 - 21:11

You could also try a module like Node Referrer (http://drupal.org/project/nodereferrer). This generates a list of all nodes that have a reference to the target node and allows you to use that as a relationship in views (ie: the reverse of using a node reference as a relationship). There are several other modules that do things along this line as well, so maybe take a look through Views modules section.

 
 

Drupal is a registered trademark of Dries Buytaert.