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

Created on 19 February 2015, over 9 years ago
Updated 25 January 2024, 5 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 1 minute 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

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 1 year 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 1 year 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

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave
  • Status changed to Needs work over 1 year 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 about 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.

  • πŸ‡¬πŸ‡§United Kingdom catch
  • πŸ‡¬πŸ‡§United Kingdom catch
Production build 0.69.0 2024