Last updated December 16, 2014. Created on July 31, 2008.
Edited by HongPong, sillygwailo, heather, esmerel. Log in to edit this page.

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.

(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.)

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.
  • Set up the argument. Under Groups: select Node and choose Node: Node ID. Click Add. When presented with the Argument Configuration options, click "Update".
  • 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.
  • 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.

    Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

    Comments

    amysteen’s picture

    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
    ?>
    
    amcoms’s picture

    Hi I have created a content type and want to display the views associated with individual nodes within the content type. Is the best way to do this to use the title as the match for the view?

    For example I create views based on the exact name of the machine name for the node and then match them up?

    If node name = view name show view?

    Not really sure the best way to code it, but first want to knwo if this is the right way to do it?

    Thanks
    Andrew

    no2x’s picture

    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?

    giorgio79’s picture

    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

    dr_thomas64’s picture

    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.

    davpas’s picture

    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.

    harryadu’s picture

    I've got one... I HARDLY EVER get any assistance on here, but I keep trying all the same.

    I have a set of nodes of content type shows. Each time a node is clicked, its related information is displayed in the main content and the related performance dates in the right sidebar. I have 3 nodes, all with the same content except the field, "Performance Times". I used the node repeat module to split them. Now, I want all the Show dates and times of the related nodes to be displayed in 1 block! So far, I can get the related performance dates and times of EACH node to display in the block, but not all 3.

    Thanks in advance,

    AdAstra’s picture

    Artist has a node reference to several songs.

    To show images of "Song" nodes in a block, just add the image field:
    - Click + next to Fields. Under Groups, select Content, and choose Content: Image (or whatever you name your imagefield). Scroll down a little and under "Defaults: Configure field Content: Image (field_image), under Relationship drop down, select your node reference field, e.g., Reference song, click Update.

    In short, you can add any field you want as long as the Relationship drop down is set to your node reference field.

    wlftn’s picture

    Perfect. Just what I was looking for. Thanks!

    Rob T’s picture

    Thanks for the run through on this. All worked as hoped for.

    fehin’s picture

    subscribing

    danielbeeke2’s picture

    thanks

    very helpful!

    iparsin’s picture

    Thank you for very clear and helpful tutorial. I need something to add on this tutorial. Let's say...currently song1 is listed under Artist1. When I go to Artist2 and select song1, now both are listed the same song. Is there any way to automatically delete song1 under Artist1 after I made change on Artist2?

    I would really appreciate all help and answers,
    iparsin

    501cGeek’s picture

    I'm trying to accomplish the same task, display all the "songs" by an "artist" when looking at the artist's node.

    But the way you're describing the relationship between artists and songs seems backwards to me. The relationship you describe would require two steps to add a new song to the site: first create the song node, then edit the artist node to link to the new song node.

    Instead of having to update the artist node with a new node reference to each song that you add, it seems more logical that when you add a new song, you create a node reference in the song node, back to the artist. If the node reference is in the song, then adding a new song related to an artist is a one-step process. The artist node doesn't ever have to be updated.

    What would be the best way to accomplish the same result, a list of songs by the artist, shown on the artist's node, given such an inverse relationship? I've tried using Node Relationships, Reverse Node Reference, and Associated Nodes modules, but haven't been able to get any of those to work right off the bat.

    NodeReferrer did work for me, but the way it works seems a little odd. Just wondering if there's a more elegant way to achieve the same.

    Thanks!

    rbpvu’s picture

    I have a similar workflow.....associating a quotation with an author. Node Relationships module works well for this.

    I create a content type quotation and content type author. Add a field for author in the quotation content type which is a node reference to a author.

    In the "node reference extras" tab of the quotation content type (content type edit form, Relationships), I enable the reference to author using the table view and create/edit/view settings.

    When I enter a new quotation I am able to create a new author or select an author already in database.

    Then I use the back reference settings under Relationships in the author content type and choose the appropriate rendering on the author node.

    I then have the author node displaying "related content" which in my case is all quotations attributed to the author.
    Then I theme the display to my liking.

    I have cloned the views of the noderelationship module and changed fields displayed etc for other similar workflows

    Hope this helps

    Have a great day!

    iparsin’s picture

    Please try http://drupal.org/project/cnr , "Corresponding node references"
    I myself think this is the best among what you mentioned + what I have tried.

    Hope it fits your need,
    iparsin

    Summit’s picture

    Hi,
    And how can I show the artist and his songs in one list please?
    So I need the node, and all it's references.
    When I click on the artist, all songs must be shown in a list.
    When I click on one of those songs, all other songs, and the artist must be shown in a list.
    Songs are references to Artist.

    Thanks a lot in advance for your reply!
    greetings, Martijn

    iparsin’s picture

    It has been a while since I used it. To use CNR, you need to have node reference fields on both content types, and point to each other. So when you view an artist, you see related songs. And when you view a song, you see related artists. The difference from plain node reference is whenever you updated related songs on artist content type or related artists on song content type, they update each other.

    rolland.huie’s picture

    I'm wondering about another form of relationships. Would NodeReferrer still work?

    Using the analogy of the song and artists...
    if you had a content type of Artist and content type of Song

    Song would actually have 2 field node references back to Artists
    -one for primary artist
    -another for featured artist

    Default view would display something like:

    Song Name | Primary Artist | Featured Artist
    Song1 | Artist1 | Artist2
    Song2 | Artist3 | Artist2
    Song3 | Artist3 | Artist1
    Song4 | Artist1 | Artist1

    Can another view be made to reflect the following?:

    Artist | Song (Primary) | Song (Featured)
    Artist1 | Song1 | Song3
    [space] | Song4 | Song4
    Artist2 | Song2 | [blank]
    [space] | Song3 | [blank]
    Artist3 | [blank] | Song1
    [space] | [blank] | Song2

    DonQuijote’s picture

    there is a checkbox in the user reference field settings says: If selected, a reverse link back to the referencing nodes is displayed on the referenced user record.

    So i have a list of nodes as 'related content' in every user profile. BUT i can NOT find a field according to this list.

    every solution i found on drual.org depends on having a field like the node_reference_field to deal with. the user reference reverse links are not shown in views or other places like custom formatters.

    any ideas? thank you for any piece of help....

    regards DonQ

    emanaton’s picture

    +1

    It seems like the user reference field type is often neglected when compared to node reference functionality; I, too, would like to have this particular ability.

    Dig1’s picture

    This post really 'hit the spot' on what I was looking to do! Thanks so much.

    Dig

    madhattertech’s picture

    This is exactly what I was looking for. I used the same logic for listing publications (type = publication), author name, and author photo (type = person).

    debo7debo’s picture

    I have gotten the reverse reference to show up only to the first reference of my unlimited reference but not the second. How do I set that up in Views 3. I been having a hard time with this for days now.

    NathanM’s picture

    Has anyone figured out how to reference a field from a node when said field has more than one instance? I can only get it to reference the node itself, not the fields inside the node. I can create a custom view which will show the fields, but when I try to reference this using a node reference field, it only shows 1 of the options.

    Imagine having a node called Restaurants, which has a field called "Address", and there may be two addresses. Is there any way to select an address via node reference and choose which address I'd like to display?

    ch_larsen’s picture

    The field references from a node reference are treated as a whole, so you cannot select the individual values from such a referenced field, if there are several values. Example: In step one of a questionnaire, the user selects four instruments from a long list of possible options in the course of a staged questionnaire, where responses are saved with each step (multistep module!). During a later step, the user is requested to select from his previously selected instruments those that are interfaced to a common data centre.
    This does not work, neither using taxonomy, nor simple allowed values text files - for the reson given above.
    This is a huge shortcoming in terms of user-friendliness.
    I have spend days on this - the other way around would be to write a php script to list the node's taxonomy terms (assigned in step one above) in the allowed options of a text field. I seem to be too dumb to get it right, or multistep interferes, as all the suggestions on Drupal snippets pages do not work. Thoughts?

    jddeli’s picture

    Did anyone find a solution for drupal 7 ?

    docans’s picture

    I also need a drupal 7 solution. I want to display custom fields instead of the node title.

    Thanks