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

Instructor

0 0 votes
Blog Rating
Subscribe
Notify of
22 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Julius35

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

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

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

Nick Hope

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

Jeoul

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

Toki

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

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

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

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
Saren

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

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

Arian

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

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

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

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

Rajivgandhi

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

marco

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

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

Tim

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

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

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

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