Providing an Entity Reference result View

The Entity reference field, covered in Chapter 2, The Content Authoring Experience, can utilize a custom view for providing the available field values. The default entity reference field will display all available entities of the type it is allowed to reference. The only available filter is based on the entity bundle, such as only returning Article nodes. Using an entity reference view you can provide more filters, such as only content that user has authored.

In this recipe we will create an entity reference view that filters content by the author. We will add the field to the user account form, allowing users to select their favorite contributed content.

How to do it…

  1. Visit Structure and then Views. This will bring you to the administrative overview of all the views that have been created. Click on Add new view to begin creating a new view.
  2. Set the View name to My Content Reference View. Modify the View settings. We want to display Content of the type All and leave the Tagged with empty.
  3. Do not choose to create a page or block. Click Save and edit to continue working on your view.
  4. Click on the Add button to create a new display. Select the Entity Reference option to create the display.
    How to do it…
  5. The Format will be automatically set to Entity Reference List, which utilizes fields. Click on Settings next to it to modify the style format.
  6. For Search Fields, check the Content:Title option then click Apply. This is what the field will autocomplete search on.
  7. You will need to modify the Content: Title field to stop it from wrapping the result as a link. Click on the field label and uncheck Link to the Content. Click Apply (all displays) to save.
    How to do it…
  8. Click on Save to save the view.
  9. Go to Configuration and then Account settings to be able to Manage fields on user accounts.
  10. Add a new Entity Reference field that references Content, call it Highlighted contributions, and allow it to have unlimited values. Click the Save field settings button.
  11. Change the Reference type method to use View: Filter by an entity reference view and select the view we have just created:
    How to do it…

How it works…

The entity reference field definition provides selection plugins. Views provides an entity reference selection plugin. This allows entity reference to feed data into a view to receive available results.

The display type for Views requires you to select which fields will be used to search against when using the autocomplete widget. If not using the autocomplete widget and using the select list or checkboxes and radio buttons, then it will return the view's entire results.

There's more…

View arguments

Entity reference view displays can accept contextual filter arguments. These are not dynamic, but can be passed manually through the field's settings. The View arguments field allows you to add a comma separated list of arguments that are passed to the view. The order should match the order of the contextual filters as configured.

In this recipe we could have added a Content: type contextual filter that fell back to Display all results if the argument was missing. This allows the view to be reused in multiple references. Perhaps there is one view that should limit the available references to all Articles created by the current user. You would then add Article to the text field and pass the argument to the view.

See also

  • Chapter 7, Plug and Play with Plugins, to learn more about plugins
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset