Drupal Association members fund grants that make connections all over the world.
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.
- Install CCK and enable these modules in CCK: Content and Nodereference.
- Create the required content types for this demonstration. Administer >> Content Management >> Content Types >> Add Content Type
- Create the content type 'Song'
- Create a content type 'Song'. No additional fields required.
- 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'
- Create content to work with
- Add several Song nodes for testing purposes.
- Add an Artist node and in the node reference field select all the Song nodes.
- Create the content type 'Song'
- 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.
- 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.
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.
- On any given /series/* page, you'll see the Series title, description, and Nodereference list of Lectures.
- 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.
- 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.
- 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.