Call Now 512.782.4514

How to use node reference fields, relationships and arguments together with views

Aaron Hawkins Posted by Aaron Hawkins September 1, 2010
  • Share

When working with subject matter as complicated as Drupal there is always something new to learn. I remember seeing the relationship section in views 2 for a long time before I actually ended up needing it, and when I did need it, I had to struggle with it for a while before getting it to work. Not that it's really difficult, but you have to do it right. I'm putting this down in writing for you, the up late at night, pulling ones hair out because you have to have this working by tomorrow developer, and for myself, just in case I forget it (Ah, the irony to find your own tutorials when googling a question).

Ok here's the short and simple version of the way it works:

Say you have a content type "product" and you have a content type "car brand". Now these products have a node reference field for the type of car they are made for (these products are accessories like spoilers or seats for a car). How do you go about making a view that accepts an argument and returns the products made for the car type in question?
By the way if you are wondering why in this case we didn't just use taxonomy, it's because for the site in question (a real site) the car type actually needed a lot more information attached to it than a taxonomy term would allow (such as images, a logo, specs etc...).

To get the described scenario to work you basically have to do the following. Go to the relationship section in views and choose the group labeled content. Then from the list of fields choose the node reference field that you used to link the two content types together.

Now here's the tricky part, and the part where I banged my head against a wall for an hour or so. When it comes time to create your argument DON'T choose the content group and your node reference field under it. It won't work. Instead choose the node group, then choose either nid or title depending on how you want to send your arguments (I tend to use titles these days for SEO purposes). Then in the relationship drop down which is now available in the argument section you choose the relationship that you created earlier. Voila, your view should now work.

You can set the validation as you see fit for your application, that's another topic all together.

Anyway, I hope that helped you.

PixelClever newsletter

Stay informed on our latest news!

Syndicate content
Drupal Tutorals RSS

Drupal Tutorials

How to Declare a Views Template Inside of a Module

The usual rule of thumb in web development is that styling (css, javascript etc...) should be done primarily inside of themes....
[Read More]

How to Remove all SVN Folders from a Site

Svn has an annoying habit of adding thousands of folders and files into every level of your site when you put your code under...
[Read More]

How to turn off Vertical Tabs for a specific content type

I rarely do any heavy structural changes to node edit pages, so until today I hadn't ever had a reason to disable vertical tabs...
[Read More]
Drupal Tutorials
Drupal Blog RSS

Drupal Blog

Free, Unlimited, Private Git Hosting - The Holy Grail of Version Control

If I were paid minimum wage for the time I have spent investigating various git and svn hosting services (both free and paid) I...
[Read More]

Mac OSX + Drush = Time saved

The longer I have my mac the more things I find that drastically simplify my life as a Drupal developer. Now for fairness sake I...
[Read More]

New to Drupal? A word from the wise: Never say newbie!

I have nothing against new Drupal users on Drupal.org, even the most advanced Drupal developers were new to Drupal at one point...
[Read More]
Drupal Blog