Display parent node and referenced child nodes

Last updated on
24 December 2018

Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites

Overview

This short guide is a how-to on displaying a View of child nodes that are associated through an Entity Reference to the single parent node currently being displayed.

Example Use Case

We have two content types, a parent Club content type, and a child Club Agenda content type.

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 for each parent club node to have many child club agenda nodes, and for each child club agenda node to be associated to its parent club node through an Entity Reference field in the child club agenda content type.

In the end, we want to display a link to the latest agenda on a club's page. (The view of club agendas can easily be modified to display more than just the most recent agenda.)

Any use case that involves the display of a list of child nodes for a parent node could be set up in a similar manner. E.g. an author's books, (books for an author), company contacts (people at a company), order line items (line items for an order), et cetera.

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

D7 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

D8 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 "Content using field_related_content" relationship and check the box.

D7 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.

D8 Add a contextual filter

Click the "Add" button to the right of Contextual filter and select "ID" with "Category = Content".

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!

Help improve this page

Page status: No known problems

You can: