Configurable views filters to allow for different widgets

Created on 4 April 2024, 5 months ago
Updated 23 August 2024, 16 days ago

Problem/Motivation

Views filters are currently hard-coded to a single form element as part of the filter plugin definition. The only way to change the form element is to alter the views data, and this changes it globally.

This makes it hard for site builders to provide a nice user experience when setting up exposed filters.

Two core issues that show the problem are [PP-2] Use form element of type date instead textfield when selecting a date in an exposed filter Needs work (200 followers) and Continuation Add Views EntityReference filter to be available for all entity reference fields Needs work (300 followers), both issues have been open for years.

https://www.drupal.org/project/better_exposed_filters exists in contrib to solve this problem, but comes with its own usability issues - the exposed filters settings are located in the views advanced panel, which is the opposite side of the views UI from filters and their exposed settings.

This means you have to add the filter, expose it, then go over to advanced settings, changed the exposed filters type, then configure the widgets. This isn't BEF's fault it's just a result of being implemented in contrib instead of directly.

Better Exposed Filters has over 100,000 modern Drupal installs putting it in the top 15 Drupal modules.

Steps to reproduce

Proposed resolution

Lots of details TBD but something like:

1. Create a new views plugin type for filterwidget
2. Implement text, select, checkboxes, radios, entity autocomplete plugins (at least) to start with.
3. When configuring a filter, add a widget selector when the filter is exposed which will choose from the options in #2.
4. Update filter settings in config to include the plugin selection.
5. Figure out more details like how to set a default widget, how to restrict widgets to certain filter types etc.
6. Reconcile this new generic method, with the custom implementations of widget selections such as in Continuation Add Views EntityReference filter to be available for all entity reference fields Needs work .

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Feature request
Status

Active

Version

11.0 🔥

Component
Views 

Last updated about 11 hours ago

Created by

🇬🇧United Kingdom catch

Live updates comments and jobs are added and updated live.
  • Needs subsystem maintainer review

    It is used to alert the maintainer(s) of a particular core subsystem that an issue significantly impacts their subsystem, and their signoff is needed (see the governance policy draft for more information). Also, if you use this tag, make sure the issue component is set to the correct subsystem. If an issue significantly impacts more than one subsystem, use needs framework manager review instead.

Sign in to follow issues

Comments & Activities

  • Issue created by @catch
  • 🇬🇧United Kingdom scott_euser

    Is it possible to break up the scope of this a bit? Make the plugin_id configurable, e.g. select entity_reference plugin, numeric plugin, etc for an entity reference as first step. Then make widgets as a second step. Then make the individual widgets as a separate issues?

  • 🇬🇧United Kingdom catch

    The issue summary here is from before we realised views wasn't respecting plugin ID configuration. Now that issue exists I think your step #1 of making the plugin ID configurable is plenty here. e.g. if the result of this issue is being able to choose between numeric and entity ref plugins for entity reference filters that sounds great. Then we can have a followup for anything else.

  • 🇬🇧United Kingdom scott_euser

    Okay great thanks.

    I guess first step is still to decide how a Views Filter Plugin says its available for which fields in which tables. BEF seems to just let you select any plugin and rely on the site builder to choose sensible options but I expect that would not be good enough for Core and the Views Filter Plugin should have some mechanism to say whether its appropriate for a particular field within a particular table, and not show up e.g. in a radio or select element if its not usable.

    So as an example we have field_types that FieldFormatter annotation uses, e.g. see core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/DecimalFormatter.php which has

    #[FieldFormatter(
      id: 'number_decimal',
      label: new TranslatableMarkup('Default'),
      field_types: [
        'decimal',
        'float',
      ],
    )]
    

    But this is more based on field type whereas hook_views_data/hook_views_data_alter allow control per source data table, per column within the table (or other type of course).

    With some sort of annotation, hook_views_plugins_filter_alter() could then add their own supported tables/fields so the change record and other documentation can just document that I guess? To make this less of a breaking change, I expect we can auto-default to whatever hook_views_data/hook_views_data_alter spit out as the filter plugin ID.

    Going this route means though that there would not be the same separation that e.g. the field plugin system has which I think the current (outdated) issue summary suggests with filterwidget, at least I don't think this sets us up for that.

  • 🇬🇧United Kingdom catch

    Yeah BEF you end up with a long list of irrelevant filters that won't work with the field you want to filter on which is not a great UX.

    I think this is where we run into limitations of the views data structure, it's an arrayPI based around database tables and not properly integrated with the entity/field API at all, except that we generate that API from the entity/field data.

    Not sure what's best, but one possible idea would be to add an extra key to views data, maybe the field type plugin ID or the typed data ID, and then allow filter plugins to declare which plugin IDs they're compatible with. Something like that? This wouldn't allow the specific views data definitions to customize which ones are configurable, but it would allow a module providing a new plugin to integrate with any field/typed data types it wants to.

  • 🇬🇧United Kingdom catch

    Tagging for subsystem maintainer review because this is another one that could really use feedback from @lendude.

Production build 0.71.5 2024