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:
Here we have the Animal content type:
And here we have an example of the People content type. "Favorite Animal" is an Entity Reference field linking to the Animal content type:
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.
- Under the Advanced tab, click "Add" next to Relationships:
- Choose "Entity Reference: Referenced Entity".
- Click Apply (All displays) twice.
- On the left-hand side of the screen, click "Add" next to Fields:
- 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:
- Choose the Relationship that you created earlier in this process:
Save your view and you'll see that you are automatically pulling in data, via Entity Reference, from the other content type: