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

Created on 19 February 2015, almost 10 years ago
Updated 19 August 2024, 3 months ago

Problem/Motivation

See part 2 at Continuation Add Views EntityReference filter to be available for all entity reference fields Needs work

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
  • 📌 Convert TaxonomyIndexTid to use new EntityReference filter Active

User interface


Known issues

  • CANNOT REPRODUCE ATM
  • FIXED
  • FIXED fixed

API changes

None.

📌 Task
Status

Closed: duplicate

Version

11.0 🔥

Component
Views 

Last updated about 1 hour 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.

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 almost 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 almost 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 almost 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 Needs work 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 over 1 year ago
  • 🇬🇧United Kingdom catch

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

  • Pipeline finished with Success
    about 1 year ago
    Total: 145s
    #49632
  • 🇬🇧United Kingdom catch
  • 🇬🇧United Kingdom catch
  • Pipeline finished with Skipped
    6 months ago
    #171057
  • Pipeline finished with Success
    4 months ago
    Total: 138s
    #235619
  • Pipeline finished with Success
    3 months ago
    Total: 260s
    #253735
  • Pipeline finished with Success
    3 months ago
    Total: 138s
    #256039
  • Pipeline finished with Canceled
    3 months ago
    #256270
  • Pipeline finished with Failed
    3 months ago
    Total: 502s
    #256273
  • Pipeline finished with Success
    3 months ago
    Total: 280s
    #256713
  • Pipeline finished with Success
    3 months ago
    Total: 169s
    #264872
  • Pipeline finished with Failed
    3 months ago
    #266435
  • Pipeline finished with Success
    3 months ago
    Total: 172s
    #266451
  • Pipeline finished with Success
    3 months ago
    Total: 360s
    #266450
  • Pipeline finished with Success
    3 months ago
    Total: 318s
    #266458
  • Pipeline finished with Canceled
    3 months ago
    #266473
  • Pipeline finished with Success
    3 months ago
    #266491
  • Pipeline finished with Success
    3 months ago
    Total: 244s
    #266546
  • Pipeline finished with Canceled
    3 months ago
    Total: 195s
    #266990
  • Pipeline finished with Failed
    3 months ago
    Total: 296s
    #269522
  • Pipeline finished with Success
    3 months ago
    Total: 312s
    #269527
  • Pipeline finished with Success
    3 months ago
    Total: 146s
    #272439
  • Pipeline finished with Success
    3 months ago
    Total: 135s
    #272440
  • Pipeline finished with Success
    3 months ago
    Total: 201s
    #273281
  • Pipeline finished with Canceled
    3 months ago
    Total: 107s
    #273772
  • Pipeline finished with Failed
    3 months ago
    Total: 586s
    #273775
  • Pipeline finished with Failed
    3 months ago
    Total: 143s
    #274533
  • Pipeline finished with Success
    3 months ago
    Total: 150s
    #274544
  • Pipeline finished with Failed
    3 months ago
    Total: 152s
    #274748
  • Pipeline finished with Success
    3 months ago
    Total: 147s
    #275433
  • Pipeline finished with Success
    3 months ago
    Total: 155s
    #275461
  • Pipeline finished with Success
    3 months ago
    Total: 146s
    #276000
  • Pipeline finished with Success
    3 months ago
    Total: 169s
    #276023
  • Pipeline finished with Success
    3 months ago
    Total: 136s
    #276059
  • Pipeline finished with Failed
    3 months ago
    Total: 136s
    #276065
  • Pipeline finished with Success
    3 months ago
    Total: 178s
    #276084
  • Pipeline finished with Failed
    2 months ago
    Total: 216s
    #277143
  • Pipeline finished with Success
    2 months ago
    Total: 223s
    #277155
  • Pipeline finished with Success
    2 months ago
    Total: 155s
    #277182
  • Pipeline finished with Success
    2 months ago
    Total: 198s
    #277188
  • Pipeline finished with Failed
    2 months ago
    Total: 151s
    #277192
  • Pipeline finished with Success
    2 months ago
    Total: 153s
    #277195
  • Pipeline finished with Failed
    2 months ago
    Total: 162s
    #277206
  • Pipeline finished with Failed
    2 months ago
    Total: 341s
    #277209
  • Pipeline finished with Success
    2 months ago
    Total: 266s
    #277222
  • Pipeline finished with Success
    2 months ago
    Total: 192s
    #277230
  • Pipeline finished with Success
    2 months ago
    Total: 193s
    #277232
  • Pipeline finished with Failed
    2 months ago
    #277235
  • Pipeline finished with Success
    2 months ago
    Total: 186s
    #277253
  • Pipeline finished with Failed
    2 months ago
    Total: 244s
    #277255
  • Pipeline finished with Failed
    2 months ago
    Total: 145s
    #277264
  • Pipeline finished with Success
    2 months ago
    Total: 162s
    #277314
  • Pipeline finished with Success
    2 months ago
    Total: 135s
    #277602
  • Pipeline finished with Success
    2 months ago
    Total: 211s
    #282687
  • Pipeline finished with Success
    2 months ago
    Total: 148s
    #283329
  • Pipeline finished with Success
    2 months ago
    Total: 144s
    #283330
  • Pipeline finished with Success
    2 months ago
    Total: 215s
    #284427
  • Pipeline finished with Failed
    2 months ago
    Total: 161s
    #284924
  • Pipeline finished with Success
    2 months ago
    Total: 151s
    #286223
  • Pipeline finished with Success
    2 months ago
    Total: 164s
    #286226
  • Pipeline finished with Success
    2 months ago
    #286254
  • Pipeline finished with Success
    2 months ago
    Total: 184s
    #286268
  • Pipeline finished with Success
    2 months ago
    Total: 289s
    #286284
  • Pipeline finished with Success
    2 months ago
    Total: 175s
    #288941
  • Pipeline finished with Success
    2 months ago
    Total: 322s
    #288944
  • Pipeline finished with Success
    2 months ago
    Total: 240s
    #288947
  • Pipeline finished with Success
    about 2 months ago
    Total: 102s
    #292965
  • Pipeline finished with Success
    about 2 months ago
    Total: 189s
    #294114
  • Pipeline finished with Skipped
    about 2 months ago
    #295414
  • Pipeline finished with Success
    about 2 months ago
    Total: 144s
    #297466
  • Pipeline finished with Success
    about 2 months ago
    Total: 257s
    #297720
  • Pipeline finished with Success
    about 2 months ago
    Total: 390s
    #297741
  • Pipeline finished with Success
    about 2 months ago
    Total: 164s
    #298133
  • Pipeline finished with Failed
    about 2 months ago
    Total: 374s
    #299576
  • Pipeline finished with Canceled
    about 2 months ago
    Total: 75s
    #299622
  • Pipeline finished with Failed
    about 2 months ago
    Total: 268s
    #300577
  • Pipeline finished with Success
    about 2 months ago
    Total: 455s
    #300583
  • Pipeline finished with Failed
    about 2 months ago
    Total: 189s
    #301569
  • Pipeline finished with Failed
    about 2 months ago
    Total: 171s
    #301576
  • Pipeline finished with Success
    about 2 months ago
    Total: 349s
    #301580
  • Pipeline finished with Failed
    about 1 month ago
    Total: 199s
    #304281
  • Pipeline finished with Failed
    about 1 month ago
    Total: 402s
    #304292
  • Pipeline finished with Skipped
    about 1 month ago
    #305096
  • Pipeline finished with Failed
    about 1 month ago
    Total: 173s
    #306349
  • Pipeline finished with Canceled
    about 1 month ago
    Total: 91s
    #307699
  • Pipeline finished with Success
    about 1 month ago
    Total: 231s
    #309017
  • Pipeline finished with Success
    about 1 month ago
    Total: 213s
    #309055
  • Pipeline finished with Success
    about 1 month ago
    Total: 216s
    #309086
  • Pipeline finished with Failed
    about 1 month ago
    Total: 346s
    #313799
  • Pipeline finished with Success
    about 1 month ago
    Total: 388s
    #315734
  • Pipeline finished with Success
    30 days ago
    Total: 183s
    #317219
  • Pipeline finished with Failed
    30 days ago
    Total: 423s
    #317256
  • Pipeline finished with Success
    27 days ago
    Total: 205s
    #321016
  • Pipeline finished with Success
    26 days ago
    Total: 145s
    #321506
  • Pipeline finished with Success
    23 days ago
    Total: 146s
    #324308
  • Pipeline finished with Success
    11 days ago
    Total: 165s
    #334632
  • Pipeline finished with Success
    7 days ago
    Total: 156s
    #338407
  • Pipeline finished with Skipped
    1 day ago
    #343744
  • Pipeline finished with Failed
    1 day ago
    Total: 140s
    #344107
  • Pipeline finished with Success
    about 24 hours ago
    Total: 184s
    #344430
  • Pipeline finished with Success
    about 23 hours ago
    Total: 135s
    #344452
  • Pipeline finished with Success
    about 5 hours ago
    Total: 283s
    #345433
Production build 0.71.5 2024