How to Use Entity Reference Views in Drupal 8

How to Use Entity Reference Views in Drupal 8

Entity Reference Views are a great way to make life easier for Drupal content creators.

Normally, when people create content on your site, each field is very plain. However, Entity Reference Views allows you to provide far more information. For example, instead of just showing a list of users, your content creators can browse through a list of names, photos and personal details.

Both Views and Entity Reference are now part of the Drupal 8 core. This made using Entity Reference Views in Drupal 8 much easier.

If you’re a Drupal 7 user, read this version of the tutorial.


Step #1. Create a content view

  • Create a view of the content that you want content editors to choose from. In this example, I’m going to make a view by the name “List of Speakers”:
Create a view of content in Drupal
  • At the top of the page, click “Add” then “Entity Reference”:
Adding an entity reference view
  • Click “Add Fields”:

add field

  • Set For to ‘This entity_reference (override)
  • Add the fields you want to have displayed and searchable.

03 add field

  • Click “Settings” next to “Entity Reference list”.
Click
  • Search fields: Choose the field(s) you want users to search by.
  • Click “Apply (this display)”.
04
  • Check the preview to make sure your view is working:
The preview of our entity reference view

Step #2. Add a Field with the Entity Reference View

  • Go to Structure > Content types.
  • Add an “Entity Reference” field.
Adding an Entity Reference field in Drupal 8
  • Save and continue
  • Save field settings
06
  • Click “Add content” and the data entry for your field will use the View you created.
  • If you prefer a checkbox to autocomplete go to Administrator > Structure > Types > Manage > Speakers > Form-display and change the widget type.
07

Author

0 0 votes
Article Rating
Subscribe
Notify of
22 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Julius35
Julius35
6 years ago

Great Tutorial, thanks! It is working so far if the users (speakers) are in a content node. I tried to have direct access to fields of registered users. So I took for refenence “Users” instead of “Content”. But a a result I get only a link to the user profile. What I want to get is a selection of user fields below the content. Do I anything wrong? To have a user profile below the content (as you have it in this article) is a frequent requirement.

Julius

kshaw
5 years ago
Reply to  Julius35

I’m having a similar problem. My view is showing the proper results from the webpage field, however the field is displaying the content items title field which isn’t in the view. I have no idea what’s going on.

Vitaliy Demchuk
Vitaliy Demchuk
6 years ago

My views do not connect to the field. Autocompletion is implemented via standard output. What could be the error?

Nick Hope
Nick Hope
6 years ago

How to display all those speakers automatically, without having to choose them?

Jeoul
Jeoul
6 years ago
Reply to  Nick Hope

You go into structure > content type > manage display > find the ‘speakers’ field and choose your preferred option 🙂

Toki
Toki
6 years ago

Entity Reference is a must-have for Drupal but here it seems strange that you do not have a widget with the list previously built in Views (i.e. with user picture and name). Actually, I have tried to build a simple widget to display images and titles and the only thing I get is the entity title (either node or taxonomy term)? As Entity Reference View Widget is not ported to D8, I have tried Entity Reference Views Select but it does not work either… Or maybe it is me. Do you think you could add a short bonus on the tutorial to show how to get a nice View as an Entity Reference Widget?

Lost and Confused
Lost and Confused
5 years ago

I cant seem to get it to work. All I get is the entity title, and I’m unable to search any of the fields I included to search. Any thoughts as to what I could be doing wrong?

maikr
5 years ago

Same here, that’s why I’ve requested a review of this tutorial. The old version of this blog post seems to be split into two separate articles (D7 and D8), but the Reference View still doesn’t work as expected.

In my case I have ‘books’ that have ‘titles’ and ‘categories’. I want the user to use an autocomplete field that can search for ‘titles’ and ‘categories’. But the latter won’t work, even while both are selected (and displayed) in the referenced view. When using a drop down widget only the ‘titles’ are shown, not the combination of ‘title’ and ‘category’ as shown in the referenced view.

Is there another way to get this to work? A popup or fly-out window with selectable items that populate a form field? Two separate dropdowns on the edit form: one for ‘categories’ that filters a second widget containing the ‘titles’ within the selected category?

laurence
laurence
5 years ago

the search output doesnt match the view? the view has a image and title, the search only title.  if you change form widget to select or radio box it overrides view even if selected.

laurence
laurence
5 years ago
Saren
Saren
5 years ago

Strange article considering this has never worked in Drupal 8. Hopefully soon though!

https://www.drupal.org/node/2174633

Arian
Arian
4 years ago
Reply to  Saren

If I apply the patch in #227 on Drupal 8.6.10, it works. (still having one minor issue that might be unrelated… a dropdown for content types shows up next to my field like: [DropdownForContentTypesShowsUpHereForSomeReason][Add new ContentTypeX][Add Existing ContentTypeX] )

Arian
Arian
4 years ago
Reply to  Arian

Note: I raised an issue about this… Thinking about it i figured out why it happens and suggested a way to improve Entity Inline Forms: https://www.drupal.org/project/inline_entity_form/issues/3044429

Salman Haider
Salman Haider
4 years ago

This is GREAT! Thanks for sharing Daniel

Suppose I have field C which is a list of colleges names and it is dependent upon the values of two different fields i.e A and B? let suppose field A is the state where colleges reside, and B is the gender of college students.

So if I have selected New York in field A, and I choose Male in field B, I should be provided with a list of schools names in field C that are in New York and of type Male

How do I do this?

alis umak
alis umak
4 years ago

Hi,It only gets last item. 
What am i missing ?

Rajivgandhi
Rajivgandhi
4 years ago

Is there any way to show the pages created from views in entity reference field?

marco
marco
2 years ago

Thy for this tutorial. One question left: How can I use the Node-Id of the node I edit with the entity-reference-view as an argument for the view. In the view that is clear but how is the correct token that has to be set in the “Views Arguments” setting of the reference field?

Peter
Peter
1 year ago
Reply to  marco

Yes, would have been nice to include the use of arguments. 🙁

Tim
Tim
2 years ago

The search field doesn’t seem to work for any fields that are from Views Relationships or any global: Custom text fields.

Is this the case? Any ideas how to get that to work?

imrodmartin
2 years ago
Reply to  Tim

Hi Tim, 
the search field works on any field associated with the content type that’s being referenced.
As noted in the image above, you can only select the fields from the content type to be “searchable”.  The search definitely works on any text field.

I’ve not been able to make it work on custom text fields added at the view.

I was also able to apply this to the User table… and search on username, first name (a custom field).  You can not search on the actual name of an image of course.

I hope that helps.
Rod

James Rome
James Rome
2 years ago

In D9, I have a view with an Entity Reference Display, but when I set the Reference Method to [i]Filter by an Entity Reference View[/i], It says no eligible views were found, and there is no field to enter the view.

Chris Fish
Chris Fish
2 years ago
Reply to  James Rome

Hi James – in order to see an eligible view you need to create an entity reference display in the the a view first – then it will show up when you are looking for it.  (this is the 3rd part of step 1 above)

I hope this helps 🙂

22
0
Would love your thoughts, please comment.x
()
x