February 24, 2012

Relating Nodes with Views

Just today we were struggling with a Views concept that was leaving a couple of us scratching our head -- how do we get a list of nodes that share a taxonomy term with another node? At first I thought this would be easy, just throw in a relationship and we'd be done! Unfortunately this was only partly true, the final solution was a little more complicated and involved a bit of trial and error. If you're interested in building something similar, hopefully this helps you out however keep in mind that the order in which you do this is important and there's a few little pitfalls you need to be aware of.

For background, we have a content type 'Product' and a content type 'Article' both having a common vocabulary named 'Indications'. For Views, the first thing we did was set up a View page with a path 'node/%/learning-centre'. The '%' in the path is an argument being supplied to the View, in which case the node ID of the Product for which we want to list the related information (Articles). You can set up a few of the basic filters now if you want, we had the usual suspects (content is published, content language matches the user's current language) as well as a content type of Article.

The first thing you'll want to do is add the 'Content: Taxonomy terms on node' relationship. In doing this, make sure you select the common vocabulary between your content types. Pretty easy so far!

Now you'll want to add a second relationship. If you're keeping a close eye on the list of relationships you might notice you now have access to some new relationships which weren't available without adding the aforementioned relationship. The new relationship you'll want to add (replacing the vocabulary name as appropriate) is called "Taxonomy term: Content using Indication(s)". This part has a pitfall, however -- you should notice there are multiple relationships with that name available. As it turns out, there'll be one for each content type to which the vocabulary has been assigned with no discernible way to tell which belongs to which content type. At this point I fired up my browser's inspector and took a closer look at the checkboxes to figure out which one I needed to add. Another pitfall -- at first I assumed I was looking for the relationship which corresponds to the content type I am attempting to relate to (Articles for me) however this turned out to be wrong. In actual fact I needed the checkbox which corresponded to the original content type (Product), which has the id of "edit-name-taxonomy-term-datareverse-field-product-indication-node". Once you figure out which relationship to add and move on to configuring it, you'll want to specify the previously defined relationship as a relationship for this relationship (confused yet? I am just writing that).

Hopefully you're still following along because we're on the home stretch. The last thing you'll want to do is add a contextual filter on Content: Nid. For this you'll want to set the relationship of the contextual filter to the second relationship you created.

If you've been playing along hopefully you now have a fully functional View. In my View I can now enter a Product node ID and get a list of Articles which have common taxonomy terms as I hoped. If you're still struggling with this I've included a screenshot of my Views configuration page, hopefully it will be of some use to you.