Community Documentation

Using Views Contextual Filter and Relationship to display reference on referenced node

Last updated May 14, 2013. Created by Courtney.B on November 14, 2012.
Edited by DSquaredB. Log in to edit this page.

Overview

This short guide is a how-to on displaying one content type associated to another content type using Entity Reference and Views.

Example Use Case

We have two content types, one is Club, the other is Club Agenda.

A club node contains the details of the club - such as location and time of the club regularly meets, plus maybe a short description. Changes made to a club node are rare.

However, a club agenda node will change for each meeting. So in this use case, it makes sense to use an Entity Reference field in the club agenda content type in order to associate a club node to a club agenda node.

The end result we're going for in this use case is to display a link to the latest agenda on a club's page.

Set up the content types

Hopefully, you are already familiar with how Content Types work in Drupal. If you're not, then please head on over to http://drupal.org/documentation/modules/field-ui for a detailed guide.

Club Content Type

club

Club Agenda Content Type

clubagenda

Set up the view

Initial Set-up

Go to Administration > Structure > Views > Add a New View. Make sure to uncheck "Create a page" and check "Create a block". Included is a screenshot of what the process looks like before clicking "Continue & edit". Also, make sure that the content type is the one that has the entity reference field and not the other way around.
first step

Add a relationship

After clicking "Continue & edit", you'll be taken to the View display. In the third column (farthest to the right), click "Advanced". Click the "Add" button to the right of Relationships. Find the "Entity Reference: Referenced Entity" relationship and check the box. Click "Apply". On the next screen, click "Apply" right away unless you want to change the identifier or make the relationship required.
second step

Add a contextual filter

Click the "Add" button to the right of Contextual filter and select "Content: Nid".
third step

Under "Relationship", ensure that the entity reference relationship you added previously is selected. Next, select "Provide default value" and then set the type to "Content ID from URL". Click "Apply".
fourth step

Unless you would like to adjust the fields or add other items to the view, go ahead and save the view.

Adding the block

In order for the view block to show up, you will need to go to Administration > Structure > Block and find the View block you just created. Place it into the region you would like for it to show up in when a club node is accessed. This will most likely be the Content region. Navigate to one of your club nodes that you have referenced in an agenda node. It should now display a link to the agenda!

AttachmentSize
view_entityreference.txt4.03 KB
club_ct.gif94.85 KB
club-agenda_ct.gif84.32 KB
view_step1.gif15.75 KB
view_step2.gif39.91 KB
view_step3.gif37.51 KB
view_step4.gif30.45 KB

Comments

Thank you

Your explanation is excellent and worked for my use case.

It's work for me and thanks a

It's work for me and thanks a lot for this awesome tutorial.

Your tutorial works great for

Your tutorial works great for the pages where I want a specific testimonial to show up (in my case, my main content type is Basic Page and my sub-content type is Testimonial), but what if I wanted a random testimonial to be displayed if the currently-viewed content item does not have a testimonial that references it? How would I set that up?

I did not realize I had to

I did not realize I had to add the relationship and then ALSO add a contextual filter. I had been stuck with just the relationship... so I could filter for "has an entry in this particular entity reference field", but I couldn't filter on what that entry was.

Thank you!

This is kind of a dumb

This is kind of a dumb question. I have all this set up, and when I run my "preview with contextual filters" with nid in the dialogue box, it works perfectly. However, if I enter node/nid in the box, nothing shows up. I think this is why my block isn't working - because the node string in the url is messing with my contextual filter. I thought that the "provide a default value" setting in the contextual filter would handle that. Can anyone help me out here?

Page status

No known problems

Log in to edit this page

About this page

Drupal version
Drupal 7.x
Audience
Site builders
Level
Intermediate
Keywords
Views

Site Building Guide

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.