Using Entity Reference Fields in Drupal Views

| Drupal
Using Entity Reference Fields in Drupal Views

An OSTraining member came to us with a question. The member had two content types that were linked by an Entity Reference field. They wanted to use Views to automatically show import from one content type to another.

In this tutorial, I'm going to show you how to use Entity Reference fields inside Views.

The Entity Reference set-up

Here's the set up we're going to use, with two content types:

2 Drupal content types

Here we have the Animal content type:

A Drupal content type that is targeted with an Entity Reference

And here we have an example of the People content type. "Favorite Animal" is an Entity Reference field linking to the Animal content type:

A Drupal content type that is using an Entity Reference field

Creating the View

  • Go to Structure > Views > Add new view.
  • Create a View to show Content of the type People. We choose People because that is the key content on display - the favorite animal is a secondary piece of information.
Creating a view using Entity Reference fields
  • Under the Advanced tab, click "Add" next to Relationships:
Adding an Entity Reference Relationship in Views
  • Choose "Entity Reference: Referenced Entity".
  • Click Apply (All displays) twice.
Choosing Entity Reference: Referenced Entity Relationships
  • On the left-hand side of the screen, click "Add" next to Fields:
Adding an Entity Reference field in Views
  • You should see that all the field from the Animal content type are now available. For example, here's the Image that is attached to the Animal content type:
Choosing an Entity Reference field in Views
  • Choose the Relationship that you created earlier in this process:
Choosing an Relationship inside an Entity Reference field in Views

Save your view and you'll see that you are automatically pulling in data, via Entity Reference, from the other content type:

A Drupal view using an Entity Reference field

About the author

Steve is the founder of OSTraining. Originally from the UK, he now lives in Sarasota in the USA. He was a teacher for many years before starting OSTraining. Steve wrote the best-selling Drupal and Joomla books.