How to Create a Entity Reference View

The Entity Reference module is a useful Drupal 7 module because it can be used to reference any entity from a field added to any other entity. For example, you can add an entity reference field to the Basic Page content type, which references taxonomy terms of vocabulary (bundle) tags. When you are creating a page and want to reference a particular term, it may help the user to see a custom format for the terms. By default the term name will be displayed for the terms that can be referenced. It may be convenient for the user to have a more detailed descprition of the term to make a decision. Perhaps for good reasons you have terms named "1" and "2" in a Staff vocabulary.  You may write descriptions for those terms where "1"s description is "Director" and "2"s description is "Teacher". You want to reference one of these terms when you create a Basic Page. The entity reference module comes with built in funcitonality for the use of views in creating a filtered list of entities available for selection for an entity reference field.

I want the options in my Staff Reference field to be "1 - Director" and "2- Teacher".

First create the Taxonomy vocabulary and terms

  • Goto: admin/structure/taxonomy/add
  • Enter Staff in the name field
  • Click Save
  • Click on add terms for the Staff vocabulary
  • (Creating first term)
    • Enter 1 for the Name field
    • Enter Director for the Description field
    • Save
  • Create second term
    • Enter 2 for the Name field
    • Enter Teacher for the Description field
    • Save

Now create an entity reference view

  • Goto structure/views/add
  • Name the view Staff Reference View
  • Select Taxonomy terms from the Show select list
  • Select Staff from the Type select list
  • Uncheck the Create a page checkbox
  • Click Continue and Edit

Configuring the view

  • Click the +Add button
  • Select Entity Reference
  • In the Fields section click the add link
  • Check the box next to Taxonomy term: Term description
  • Click Apply
  • Uncheck Create a Label
  • Click Apply
  • Click the Taxonomy term: Name link in the Fields section
  • Uncheck Link this field to its taxonomy term page
  • Under the Format section you will see a Format: Entity Reference List | Settings
  • Click Settings
  • For Search fields check Taxonomy term:Name
  • Click Apply
  • Under the Format section you will also see Show: Inline fields | Settings
  • Click Settings
  • Under Inline Fields
    • Check Taxonomy term: Name
    • Check Taxonomy term: Term description
  • Click Apply
  • Save the view

Let's add and configure an entity reference field.

  • Go to admin/structure/types/manage/page/fields
  • Enter Staff Reference in the Add new field text field.
  • Select Entity Reference from the Field Type select list
  • Select Select List from the Widget select list
  • Click Save

You are now presented with the Field Settings page

Target Type sets the entity which this field will reference. In our example we will be using Taxonomy Term. Inside the Entity Selection fieldset is the Mode field with two options: Simple (with bundle filter) , and views (Filter by an Entity Reference View).

  • For Mode Select Views (Filter by an Entity Reference View)
  • For View used to select the entities choose the view we created earlier staff_reference_view  Entity reference
  • Click the Save Field Settings button
  • Click Save Settings when the next page loads

Create a page

  • Goto: node/add/page
  • You will see the Article Reference field select list under the body field
  • The Options say "1-Director" and "2-Teacher"

Share this post