Add Views EntityReference filter to be available for all entity reference fields

Created on 19 February 2015, over 10 years ago
Updated 2 February 2023, over 2 years ago

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 and migrate the existing taxonomy filters to be based on the new generic filter plugin.

How to use

  1. Add on an entity type / bundle an entity reference field, ex field_test_reference.
  2. Create a view displaying this entity type.
  3. Add a filter on the view for field_test_reference.
  4. Configure the entity selection mode and the widget display mode.
  5. Configure the filter behavior (ex: required, multiple, etc.)
  6. Finally use the filter field for filtering the results based on the selected entity from the autocomplete or select list.

Remaining tasks

  • ☑ support for content entity reference
  • ☑ support for content with and without bundles
  • ☑ settings forms to configure the filter
  • ☑ display widget in select or autocomplete
  • ☑ filter values based on reference view
  • ☑ filter values based on bundles
  • ☑ sort for filter values when in bundle selection handler mode
  • ☑ argument support for when view selection handler is used
  • ☑ views configuration schema update
  • ☑ tests
  • ☐ get framework manager approval
  • ☐ write change record

Post tasks

  • ☐ follow-up task to have TaxonomyIndexTid use this entity reference plugin
  • ☐ conversion of the "authored by" filter to use the entity reference 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

User interface


Known issues

  • CANNOT REPRODUCE ATM
  • FIXED
  • FIXED fixed

API changes

None.

📌 Task
Status

Needs work

Version

10.1

Component
Views 

Last updated 10 minutes ago

Created by

🇺🇸United States jhedstrom Portland, OR

Live updates comments and jobs are added and updated live.
  • Usability

    Makes Drupal easier to use. Preferred over UX, D7UX, etc.

  • VDC

    Related to the Views in Drupal Core initiative.

  • Needs framework manager review

    It is used to alert the framework manager core committer(s) that an issue significantly impacts (or has the potential to impact) multiple subsystems or represents a significant change or addition in architecture or public APIs, and their signoff is needed (see the governance policy draft for more information). If an issue significantly impacts only one subsystem, use Needs subsystem maintainer review instead, and make sure the issue component is set to the correct subsystem.

  • Needs change record

    A change record needs to be drafted before an issue is committed. Note: Change records used to be called change notifications.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇺🇸United States smustgrave

    Still seeing the same error.

    Also see it was tagged for change record which still needs to happen.

    Will reach out for framework manager review.

  • 🇺🇸United States smustgrave

    Also think I may know why the test fails on drupal and not locally

    /admin/structure/views/nojs/handler-extra/content/page_1/filter/field_test_target_id_reference is locally

    but in drupal I bet it's

    subdirectory/admin/structure/views/nojs/handler-extra/content/page_1/filter/field_test_target_id_reference

  • 🇺🇸United States smustgrave

    Lets try this.

    Leaving in NW for change record.

    Hiding files to avoid confusion

  • 🇺🇸United States smustgrave

    Lets try this.

    Leaving in NW for change record.

    Hiding files to avoid confusion

  • Status changed to Needs review over 2 years ago
  • 🇵🇱Poland Graber

    https://www.drupal.org/node/3338903 Here's a change record draft.
    Not sure about it though as this is a new feature actually that doesn't affect any previous instances. Do we need a CR in such cases as well?

  • Status changed to RTBC over 2 years ago
  • 🇺🇸United States smustgrave

    Add small tweak to CR to include images.

    So a CR is also useful to announce new features/major changes. So announcing users can use this new feature could be helpful for some.

  • 🇬🇧United Kingdom catch

    The issue summary currently mentions converting the TaxonomyIndexTid filter to be based on this, but this isn't done in the patch. I don't think we should do it here, already at a possibly record-breaking 530 comments, but I couldn't see a follow-up to do that in the related issues - would be good to create that if it really doesn't exist and link it from the issue summary.

  • 🇺🇸United States smustgrave

    This gets any more comments I'm afraid my page won't load haha

    Opened 📌 Add Views EntityReference filter to be available for all entity reference fields Closed: duplicate

  • Status changed to Needs work over 2 years ago
  • 🇳🇱Netherlands Lendude Amsterdam

    @catch asked me to look at this again, mostly the test coverage. The test coverage seems mostly good, but ran into some other issues.

    • calculateDependencies is untested and should be since this is different than the Term plugin
    • The 'sort by' field is empty on initial adding of the filter (the field widget does something ajaxy when selecting bundles, the views widget does not, might be it?)
    • Grouped autocomplete filters don't work/save (but don't work for taxonomy ref filters either), please find the related issue or open it (should be one I think)
    • Description "as a Reference filter" feels a bit vague, did we get UX feedback on this?
    • Should we add this new filter for taxonomy terms too, or should we skip those since it is unclear what the difference is now when have to chose between the two different filters
    • I can create a circular reference by creating an Entity reference View that uses itself as the filter, not sure what goes wrong (mostly getting an empty filter after saving), but that sounds iffy
    • Maybe related : Managed to click my way into "Warning: Undefined array key "reference_views" in Drupal\views\Plugin\views\filter\EntityReference->submitExtraOptionsForm() (line 440 of /app/drupal/core/modules/views/src/Plugin/views/filter/EntityReference.php)"
    • Node titles in the select element are double escaped
    • Exposed filters on the chosen entity reference view are also shown on the view
  • 🇵🇱Poland Graber

    I'm afraid a few more comments and infra will break for good. Maybe we should close this and open in a new issue with a summary? We could also choose contrib approach I took with Views Date Filter, create a core readiness issue there and merge to core when ready? Some changes seem too big for a single issue and should be handled as epics with their own sub-tasks really, otherwise we're stuck for years and end users don't see any progress.

  • 🇺🇸United States smustgrave

    Opened up Continuation Add Views EntityReference filter to be available for all entity reference fields Active and asked @catch or @lendude to take a look and see if we can close this one one out.

    Moved over the changes in the MR and copied comment #534 as those need to be done.

  • 🇪🇸Spain psf_ Huelva

    Hi, I don't like add more noise to this issue but I think it's a related use case.

    The module https://www.drupal.org/project/config_views allow create views that query configuration entities, and I can create a new view that result all taxonomy vocabularies, for example. If I add a entity reference display to that view I can't select it in a reference field to use the view how list options.

    I can't because this:

    File: web/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php line 141 of Drupal 10.0.9.

          if (in_array($view->get('base_table'), [$entity_type->getBaseTable(), $entity_type->getDataTable()])) {
            $display = $view->get('display');
            $options[$view_id . ':' . $display_id] = $view_id . ' - ' . $display[$display_id]['display_title'];
          }
    

    If I replace that code by:

            $display = $view->get('display');
            $options[$view_id . ':' . $display_id] = $view_id . ' - ' . $display[$display_id]['display_title'];
    

    I can select my view, but after saving the site break with this error:

    Error: Call to a member function bundle() on null in Drupal\views\Plugin\EntityReferenceSelection\ViewsSelection->stripAdminAndAnchorTagsFromResults() (line 289 of core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php).
    
    Drupal\views\Plugin\EntityReferenceSelection\ViewsSelection->getReferenceableEntities() (Line: 618)
    Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem->getSettableOptions(Object) (Line: 138)
    Drupal\Core\Field\Plugin\Field\FieldWidget\OptionsWidgetBase->getOptions(Object) (Line: 34)
    Drupal\Core\Field\Plugin\Field\FieldWidget\OptionsSelectWidget->formElement(Object, 0, Array, Array, Object) (Line: 349)
    Drupal\Core\Field\WidgetBase->formSingleElement(Object, 0, Array, Array, Object) (Line: 92)
    Drupal\Core\Field\WidgetBase->form(Object, Array, Object) (Line: 287)
    Drupal\Core\Field\FieldItemList->defaultValuesForm(Array, Object) (Line: 125)
    Drupal\field_ui\Form\FieldConfigEditForm->form(Array, Object) (Line: 106)
    Drupal\Core\Entity\EntityForm->buildForm(Array, Object)
    call_user_func_array(Array, Array) (Line: 536)
    Drupal\Core\Form\FormBuilder->retrieveForm('field_config_edit_form', Object) (Line: 283)
    Drupal\Core\Form\FormBuilder->buildForm(Object, Object) (Line: 73)
    Drupal\Core\Controller\FormController->getContentResult(Object, Object)
    call_user_func_array(Array, Array) (Line: 123)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 580)
    Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 163)
    Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 74)
    Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58)
    Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
    Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106)
    Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
    Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 48)
    Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
    Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 51)
    Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object, 1, 1) (Line: 686)
    Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
    

    I think that fail because views only work with content entities, not with configuration entities.

  • Status changed to Closed: duplicate about 2 years ago
  • 🇬🇧United Kingdom catch

    Closing this as a duplicate of Continuation Add Views EntityReference filter to be available for all entity reference fields Active because this issue is crashing browsers at this point. Let's continue over there.

  • 🇬🇧United Kingdom catch
  • 🇬🇧United Kingdom catch
  • Pipeline finished with Success
    11 months ago
    Total: 138s
    #256039
  • Pipeline finished with Success
    11 months ago
    Total: 158s
    #257939
  • 🇷🇴Romania biancaradu27

    add patch for 10.3.x

  • Pipeline finished with Failed
    11 months ago
    #266435
  • Pipeline finished with Success
    11 months ago
    Total: 172s
    #266451
  • Pipeline finished with Success
    11 months ago
    Total: 360s
    #266450
  • Pipeline finished with Canceled
    11 months ago
    Total: 195s
    #266990
  • Pipeline finished with Success
    11 months ago
    Total: 146s
    #272439
  • Pipeline finished with Success
    10 months ago
    Total: 201s
    #273281
  • Pipeline finished with Canceled
    10 months ago
    Total: 107s
    #273772
  • Pipeline finished with Success
    10 months ago
    Total: 158s
    #275536
  • Pipeline finished with Success
    10 months ago
    Total: 146s
    #276000
  • Pipeline finished with Success
    10 months ago
    Total: 136s
    #276059
  • Pipeline finished with Success
    10 months ago
    Total: 198s
    #277188
  • Pipeline finished with Success
    10 months ago
    #283326
  • Pipeline finished with Success
    10 months ago
    Total: 215s
    #284427
  • Pipeline finished with Success
    10 months ago
    Total: 151s
    #286223
  • Pipeline finished with Success
    10 months ago
    #286254
  • Pipeline finished with Success
    10 months ago
    Total: 322s
    #288944
  • Pipeline finished with Skipped
    10 months ago
    #295414
  • Pipeline finished with Success
    10 months ago
    Total: 144s
    #297466
  • Pipeline finished with Failed
    10 months ago
    Total: 374s
    #299576
  • Pipeline finished with Success
    10 months ago
    Total: 267s
    #299623
  • Pipeline finished with Failed
    9 months ago
    Total: 268s
    #300577
  • Pipeline finished with Success
    9 months ago
    Total: 216s
    #309086
  • Pipeline finished with Success
    9 months ago
    Total: 388s
    #315734
  • Pipeline finished with Failed
    9 months ago
    Total: 423s
    #317256
  • Pipeline finished with Success
    9 months ago
    Total: 146s
    #324308
  • Pipeline finished with Success
    8 months ago
    Total: 156s
    #338407
  • Pipeline finished with Success
    8 months ago
    Total: 149s
    #339829
  • Pipeline finished with Failed
    8 months ago
    #347078
  • Pipeline finished with Success
    8 months ago
    Total: 242s
    #353438
  • Pipeline finished with Failed
    7 months ago
    Total: 138s
    #361014
  • Pipeline finished with Success
    7 months ago
    Total: 150s
    #366557
  • Pipeline finished with Failed
    7 months ago
    Total: 165s
    #370432
  • Pipeline finished with Failed
    7 months ago
    Total: 229s
    #371177
  • Pipeline finished with Failed
    7 months ago
    Total: 153s
    #374045
  • Pipeline finished with Success
    7 months ago
    Total: 157s
    #380471
  • Pipeline finished with Failed
    7 months ago
    Total: 108s
    #381162
  • Pipeline finished with Success
    7 months ago
    Total: 157s
    #381175
  • Pipeline finished with Failed
    7 months ago
    Total: 161s
    #381210
  • Pipeline finished with Success
    6 months ago
    Total: 160s
    #385278
  • Pipeline finished with Success
    6 months ago
    Total: 926s
    #385291
  • Pipeline finished with Success
    6 months ago
    Total: 138s
    #386202
  • Pipeline finished with Success
    6 months ago
    Total: 186s
    #392274
  • Pipeline finished with Success
    6 months ago
    #394505
  • Pipeline finished with Success
    6 months ago
    Total: 187s
    #396386
  • Pipeline finished with Success
    6 months ago
    Total: 199s
    #401496
  • Pipeline finished with Failed
    6 months ago
    Total: 173s
    #401575
  • Pipeline finished with Success
    6 months ago
    Total: 143s
    #407439
  • Pipeline finished with Success
    6 months ago
    Total: 227s
    #409167
  • Pipeline finished with Success
    6 months ago
    Total: 170s
    #409295
  • Pipeline finished with Success
    6 months ago
    Total: 148s
    #409497
  • Pipeline finished with Success
    6 months ago
    Total: 160s
    #409566
  • Pipeline finished with Success
    6 months ago
    Total: 150s
    #409683
  • Pipeline finished with Success
    6 months ago
    Total: 336s
    #409736
  • Pipeline finished with Success
    6 months ago
    Total: 161s
    #410031
  • Pipeline finished with Success
    6 months ago
    Total: 194s
    #410063
  • Pipeline finished with Success
    6 months ago
    Total: 147s
    #411482
  • Pipeline finished with Success
    5 months ago
    Total: 166s
    #419620
  • Pipeline finished with Success
    5 months ago
    Total: 143s
    #420727
  • Pipeline finished with Success
    5 months ago
    Total: 641s
    #423371
  • Pipeline finished with Failed
    5 months ago
    Total: 147s
    #424159
  • Pipeline finished with Canceled
    5 months ago
    Total: 202s
    #424197
  • Pipeline finished with Canceled
    5 months ago
    Total: 94s
    #424921
  • Pipeline finished with Failed
    5 months ago
    Total: 210s
    #434701
  • Pipeline finished with Failed
    5 months ago
    Total: 175s
    #434823
  • Pipeline finished with Failed
    4 months ago
    Total: 217s
    #441464
  • Pipeline finished with Success
    4 months ago
    Total: 160s
    #441689
  • Pipeline finished with Success
    4 months ago
    Total: 196s
    #456231
  • Pipeline finished with Success
    4 months ago
    Total: 156s
    #457550
  • Pipeline finished with Success
    4 months ago
    Total: 205s
    #460873
  • Pipeline finished with Success
    3 months ago
    Total: 327s
    #463053
  • Pipeline finished with Success
    3 months ago
    Total: 144s
    #467608
  • Pipeline finished with Success
    3 months ago
    Total: 142s
    #467627
  • Pipeline finished with Success
    3 months ago
    Total: 165s
    #468553
  • Pipeline finished with Failed
    3 months ago
    #468577
  • Pipeline finished with Canceled
    3 months ago
    Total: 116s
    #474110
  • Pipeline finished with Success
    3 months ago
    Total: 142s
    #475656
  • Pipeline finished with Success
    3 months ago
    Total: 194s
    #483451
  • Pipeline finished with Failed
    3 months ago
    Total: 316s
    #484966
  • Pipeline finished with Success
    3 months ago
    Total: 151s
    #486042
  • Pipeline finished with Failed
    2 months ago
    Total: 252s
    #488513
  • Pipeline finished with Success
    2 months ago
    Total: 226s
    #488517
  • Pipeline finished with Failed
    about 2 months ago
    #503843
  • Pipeline finished with Failed
    about 2 months ago
    Total: 159s
    #506832
  • Pipeline finished with Failed
    about 2 months ago
    Total: 313s
    #506996
  • Pipeline finished with Success
    about 2 months ago
    Total: 150s
    #507954
  • Pipeline finished with Success
    about 2 months ago
    Total: 149s
    #508402
  • Pipeline finished with Success
    about 2 months ago
    Total: 142s
    #508410
  • Pipeline finished with Success
    about 1 month ago
    Total: 167s
    #511540
  • Pipeline finished with Success
    25 days ago
    Total: 184s
    #527123
  • Pipeline finished with Success
    22 days ago
    Total: 141s
    #529811
  • Pipeline finished with Success
    20 days ago
    Total: 164s
    #531659
  • Pipeline finished with Success
    20 days ago
    #531873
  • Pipeline finished with Success
    19 days ago
    Total: 179s
    #532260
  • Pipeline finished with Skipped
    19 days ago
    #532851
  • Pipeline finished with Failed
    18 days ago
    Total: 285s
    #533020
  • Pipeline finished with Success
    18 days ago
    Total: 432s
    #533032
  • 🇺🇸United States andyg5000 North Carolina, USA

    We were using the patch from 545. After upgrading to Drupal 10.5.1, it still applied cleanly, but caused critical errors because it duplicated the <?php tag and rest of the code.

    If you're seeing `Parse error: syntax error, unexpected '<'` remove the patch.

    Just a warning for others that may be using it!

  • Pipeline finished with Success
    11 days ago
    Total: 152s
    #538886
  • Pipeline finished with Success
    11 days ago
    Total: 235s
    #538982
  • 🇬🇷Greece ngal

    I run into the same issue as #546 is describing, when updated a project to 10.4 so I updated the patch. Feel free to test, its working as expected for me.

  • 🇺🇸United States xjm

    Locking issue to prevent further comments and patches from being posted. This was fixed in Continuation Add Views EntityReference filter to be available for all entity reference fields Active and is available in all supported versions of Drupal core.

  • 🇺🇸United States xjm

    Locking issue to prevent further comments and patches from being posted. This was fixed in Continuation Add Views EntityReference filter to be available for all entity reference fields Active and is available in all supported versions of Drupal core.

  • 🇺🇸United States xjm
Production build 0.71.5 2024