Continuation of #2429699: Add Views EntityReference filter to be available for all entity reference fields since there were so many comments the page was struggling to load.
Slack thread
https://drupal.slack.com/archives/C6SQM2J94/p1706633005193429
Problem/Motivation
One major piece of functionality from the D7 Entity Reference module was left out entirely in #1801304: Add Entity reference field: the ability to render exposed views filters as a select list or autocomplete of available entities.
Proposed resolution
Create a new Views Entity Reference filter plugin to be available for all entity reference fields but do not activate it at this stage. Make it available as an opt-in via views data alter.
How to use
- Add on an entity type / bundle an entity reference field, ex field_test_reference.
- Use views data alter to allow your field to make use of the reference (see code snippet below)
- Create a view displaying this entity type.
- Add a filter on the view for field_test_reference.
- Configure the entity selection mode and the widget display mode.
- Configure the filter behavior (ex: required, multiple, etc.)
- Finally use the filter field for filtering the results based on the selected entity from the autocomplete or select list.
Code snippet to use this filter:
/**
* Implements hook_views_data_alter().
*/
function MODULE_NAME_views_data_alter(&$data) {
// Use entity reference for the field 'field_test_target'.
$field_name = 'field_test_field';
$data['node__' . $field_name][$field_name . '_target_id]['filter']['id'] = 'entity_reference';
}
Remaining tasks
- ☑ support for content entity reference
- ☑ support for configuration entity reference
- ☑ support for content with and without bundles
☑ taxonomy filter rebased on generic entity reference- ☑ settings forms to configure the filter
- ☑ display widget in select or autocomplete
- ☑ filter values based on reference view
- ☑ filter values based on bundles
☑ maximum filter values in select list for performance concerns- ☑ sort for filter values when in bundle selection handler mode
- ☑ argument support for when view selection handler is used
- ☑ views configuration schema update
☐ existing configuration migration (no longer needed if we create a new filter only)☐ fix select option (#208, #215)- ☑ tests for general functionality
- ☑ write change record
- ☐ get framework manager approval
- ☐ update change record screenshots once green light has been given for this updated direction and considered ready to merge
Issues suggested to leave
As noted in #163, #164, and #169, suggesting we leave the outstanding non-checked items from below. Checked items are handled.
☐ Address points in comment #49 (comment 534 from original ticket)- ☑ add test coverage for calculate dependencies
☐ sort by empty on initial load (needs more info), see comment #49Unable to reproduce, perhaps tied to older version of patch☐ prevent circular reference: validate no selection of an entity reference that uses itself as the filter, see comment #49Suggested to be a follow-up- ☑ node titles in the select options when configuring the filter are double escaped (e.g.
"
shows as"
), see comment #49 - Resolved in #200 - ☑ exposed filters on the chosen entity reference view are also shown on the view, see comment #49 - Resolved in #199
Post tasks
- ☐ #3458099: Views handler loading should respect configuration : have views handler respect configuration to make it easier for filters to opt in
- ☐ After that lands, provide an UI to allow selection of which filter handler to use #3438054: Configurable views filters to allow for different widgets
- ☐ follow-up task for
TaxonomyIndexTid
#3339738: Convert TaxonomyIndexTid to use new EntityReference filter
- ☐ extract selection handler form logic in separate plugins that will specialize for rendering and validating the filter selection config form
- ☐ caching of the value form?
- ☐ documentation updates
- ☑ tests support for base fields
- ☑ #3458102: Add Views EntityReference filter to be available for all entity reference base fields support for base fields + tests support for base fields
User interface
![]() | ![]() |
Known issues
- CANNOT REPRODUCE ATM
sometimes when switching between widget it gets stuck on the previous selected one - FIXED
when switching between the widget types the previous value is left in the exposed form and it's incompatible with the other widget type - FIXED
triggering ajax requests on the extra options form right after adding the filter brings you back to the add handler formfixed
API changes
None.
For 10.2
See hidden branches and comment #143