Filtering Entity Reference Fields: How to Enforce Selection of Published Contents

| | 3 min read

Unpublished Content in Entity Reference Fields

In Drupal, the default behavior of entity reference fields allows us to select unpublished content if the content creator has permission to view unpublished content, which can be useful in specific scenarios where we need to establish relationships even with unpublished content.

However, in many cases, we want to refer only to published content. The problem arises when we add references during content creation without verifying whether the referenced items are published. This can lead to confusion later when we wonder why the referenced contents are not visible when viewing the content.

Leveraging Drupal's Views Module for Published Content Filtering

To address this issue, we can leverage Drupal's Views module. By creating a view specifically for the desired content type and filtering it to display only published content, we can use this view as an alternative method for entity reference fields, replacing the default behavior.

The process becomes even more streamlined when dealing with multiple entity reference fields for different content types. Instead of creating separate views for each content type, we can utilize Views' contextual filter feature. With this approach, we can create a single display of the view, utilizing the content type as the contextual filter.

We need to enable the 'Allow multiple values' option to handle cases where we need to refer to multiple content types in a single entity reference field. Once this is set, we can easily apply the previously created view to the field settings of any entity reference field. To do this, we provide the machine names of the desired content types in the View Arguments option, separated with a '+' symbol.

By implementing this approach, we can streamline the process of content association, ensuring that only published content is referenced while avoiding the need to create numerous view blocks for each content type. This results in a more efficient and organized content management experience within Drupal.

Step-by-Step Guide on How to Use a Drupal View to Filter the Values Appearing in an Entity Reference Field

1) Create a view of published contents and add a display of type ENTITY REFERENCE.

Entity Reference Display in Views

2) Click on Settings near the Format “Entity Reference List” and select the Search fields as Content: Title.
 

Content title in views


3) Add content type in the contextual filter and select “Provide default value” in WHEN THE FILTER VALUE IS NOT AVAILABLE section and set the TYPE to “Fixed”.

Contextual filter

4) Enable the “Allow multiple values” option in the MORE section at the bottom of contextual filter settings and save the view.

Allow multiple values in contextual filter

5) Now the view is ready, you can select it in the field settings of any entity reference field for contents as below. Select the Reference Method as Views, select the view we just created and add the machine names of the content types you want to refer to as below. Note that you must use ‘+’ when multiple content types exist.

Entity Reference Field in the View


6) All is set; your entity reference field will allow you to select only published content. You can also utilize this method to get more control over the selection of contents in the entity reference fields by adding more filters to the view.