Drupal 8 - Reverse entity reference in view

In this article, I will assume that you are already aware of entity reference field and usage of relationships and context filters in views. If not, you can search using these terms and you will find plenty of resources for these topics.

Most of the time, entity reference field is used in following manners.

Suppose you have two content types Album and Song. Every song belongs to some Album and an Album can contain more than one song. To mitigate this relation in Drupal, you can create two content types Album and Song respectively and then add a reference field for Album content type inside Song content type. So that whenever you create a song you can choose which Album this song belongs to. Then later you can create a view which can list all songs of a specific album, using relationship and context filter.

But sometimes, we might want to get the information in other way around. In above scenario, we might want to get Album info using specific song id. This is a bit trickier than the first scenario because there is no reference field for song in Album content type.

Here is one way to accomplish this using just views in core and without any third party module.

Suppose we have two albums and each album contain two songs like following with their respective node ids:

  • Thriller (1)
    • Billie Jean (6)
    • Beat It (11)
  • Dangerous (16)
    • Jam (21)
    • Heal the World (26)

And now suppose you want to get the Album info of song Jam (21).

01 - Create a new view to list Album info:

Add Drupal view

02 - Set the display of the view to Page

Set Drupal view display

03 - From ADVANCED section, add a new relationship. Search for album to get the album reference field which is available in song content type. Select the field and click Add and configure relationships button.

Add view relationship

04 - In next window, make sure that Require this relationship options is checked and click Apply.

View relationship settings

05 - Again from ADVANCED section, we need to add a context filter so that we can filter results by songs id. In Add contextual filters dialogue, search for id and select ID from results. Click Add and configure contextual filters.

Add view context filter

06 - In next window, from When the filter value is NOT int the url section, choose Provide default value and set the Type to Content ID from URL.

Context filter settings

Change the view format from Unformatted list to Table.

07 - In FIELDS section, click on Content: Title field to configure it. In Configure field window, change the value for Relationship dropdown to use the relation that we created (field_album: Content)

Title field configuration

08 - Similarly use this relation in all filter and sort criteria as well.

Other fields configuration

09 - Then for the last step, set the path in Page Settings. I will use the following path for this example:


View page url path

After this click Save to save your view changes.

10 - In Preview section, enter id of the song in Preview with contextual filters field for which you want to get the album name and click Update preview

Contextual filter preview

11 - And you will see title of the album for that song in Preview table

Contextual filter preview results


In order to be able to use reverse entity reference in views, we must define a relationship and use it in all our fields and criteria. Then we can use the contextual filter to filter the results according to our needs.

  PHP, Drupal 8