create a drupal view using the node reference field

not too long ago, i had a drupal project that required me to display a set of nodes from one content type onto another and vice-versa. the simplest method would be to create a node reference field for both content types but it isn’t practical. instead, i would prefer the relationship be created automatically, defined through only one of the content types. this would be less cumbersome as it would take twice as long to update each content type’s node if done manually. the solution lies in the power of the views module because it can display and sort any piece of content created in drupal on the fly. in this example, i’m going to create a content type that uses the node reference field to make a relationship between another content type’s nodes. this time, however, i will create a view to display the other half of this relationship.

the node reference field

make sure you have downloaded and installed the views and cck (content construction kit) modules and create two new content types, “mobile phone” and “wireless carrier”. select manage fields from the “mobile phone” content type, create a new field labeled “carriers” and name this new field “field_carriers”. select node reference as the type of data to store and choose check boxes/radio buttons as the element to edit the data.

Node Reference Field

upon saving, you will be taken to the settings page for this new field. the only configuration that needs to be done is the global settings at the bottom of the page. change the number of values to unlimited, select wireless carrier as the content type that can be referenced, and save your settings.

the content type nodes

with the node reference field set up, we need some content that can be referenced. under the wireless carrier content type, create 3 nodes. i’ve titled each as the following:

  • verizon wireless
  • at&t
  • sprint

now create 4 nodes using the mobile phone content type.

  • apple iphone
  • motorola droid
  • palm pre
  • htc evo

unlike the wireless carrier nodes, each mobile phone node has an extra field available to edit. this is the node reference field we made earlier. it is set to list each wireless carrier node. go ahead and choose the appropriate wireless carrier for each phone. you may select more than one node to reference, for instance, the palm pre is carried on sprint and verizon wireless while the iphone is exclusive to at&t. when finished, you will see that each mobile phone displays a list of wireless carriers it’s available on.

the views module

let’s achieve the same effect on our wireless carrier nodes, except this time we won’t need to create a node reference field for the wireless carrier content type. create a new view and give it a name and a description. leave the default selection of “node” as is because this view will be referencing other nodes. for this view, the only settings that matter are fields and arguments. the fields setting lets you choose which field should be displayed in the view. we only want the node title to be displayed, so select “node: title” from the list of choices. all node titles are now displayed but we are only interested in the node titles from the mobile phone content type.

Views Fields

to reduce this result set, you have to use arguments. arguments are parts of the url. for example, if you use the url “node/3” as an argument, that view would be restricted to a node with an id of 3. in this case, we want to restrict the view to nodes using the node reference field. in the arguments setting, select “content: carrier (field_carrier)”. from there select “provide default argument” and choose “node id from url”. this tells the view to only display the nodes that used the node reference field.

Views Arguments

as a caveat, under “basic settings” select the style option and set it as an unordered list. this displays the node titles in an unordered list when the page is rendered.

embedding a view

the only thing left to do is display the view in the wireless carrier nodes. because i used the default display in the view all you have to do is edit your node template and add the following:

<?php print views_embed_view('viewname'); ?>

change “viewname” with the name of your view and save the template. now view one of your wireless carrier nodes and you will see a list of all mobile phone nodes that referenced the current wireless carrier node.

resources

view the final build here or download the view to import into your own build.

Leave a Reply

Your email address will not be published. Required fields are marked *