Views EntityReference filter: Add functionality to switch from select to autocomplete widget when a max number of options reached

Created on 18 October 2024, 11 months ago

Problem/Motivation

An old patch associated with issue Continuation Add Views EntityReference filter to be available for all entity reference fields Active included the functionality to switch from a select widget to an autocomplete widget for an Views EntityReference filter when the number of select list options hit a predefined value. This functionality was removed in part 1 of this issue 📌 Add Views EntityReference filter to be available for all entity reference fields Closed: duplicate in an attempt to simplify the patch and improve its chances of getting committed. Current functionality of this filter in 11.x limits the select widget to 100 options and there is no way to change this limit. Also the type of widget is fixed, there is no automatic switching to an autocomplete widget when that limit is exceeded.

We found the original patch functionality very useful on a site where different groups with their own content use the same view. Some groups only have a few referenced nodes, so a select widget is more user-friendly, and other groups have hundreds of references node so an autocomplete widget is preferred.

Now that Continuation Add Views EntityReference filter to be available for all entity reference fields Active is in 11.x (YAY!), I am hoping this functionality can be re-added in this issue.

Steps to reproduce

  1. Set up an exposed EntityReference filter on a View as per the instructions in Continuation Add Views EntityReference filter to be available for all entity reference fields Active
  2. In the extra settings form in the Views Admin UI, note that only a select or autocomplete widget can be selected. Choose select.
  3. Create more than 100 referenced nodes (Devel module & drush will help here)
  4. Visit the view page and note that the filter is a select widget and only 100 options are listed.

Proposed resolution

  • Add a list_max Views EntityReference filter exposed form configuration option.
  • This configuration option is only visible when the select widget is selected.
  • If list_max is set to 0 (meaning unlimited), the filter will will always be a select widget.
  • If list_max is set to a number greater than 0, when the number of options hits or exceeds this limit, the widget will automatically switch from a select widget to an autocomplete widget.

Remaining tasks

  1. Create MR containing new config, \Drupal\views\Plugin\views\filter\EntityReference changes and new tests.
  2. Review and feedback from the community.

User interface changes

  • Addition of a "Maximum entities in select list" (list_max) field to the Views EntityReference filter extra settings form in the Views admin UI.
  • Views EntityReference filter exposed form will automatically switch from a select widget to an autocomplete widget, if the select widget is chosen and the number of options equals or exceeds the list_max value.

Introduced terminology

N/A

API changes

N/A

Data model changes

N/A

Release notes snippet

N/A

Feature request
Status

Active

Version

11.0 🔥

Component

views.module

Created by

🇨🇦Canada tame4tex

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @tame4tex
  • 🇨🇦Canada tame4tex

    I have added MR 9866 to add this feature. Code was inspired by functionality that existed in https://www.drupal.org/files/issues/2020-09-30/2429699-351--on-9_1_x.patch on 📌 Add Views EntityReference filter to be available for all entity reference fields Closed: duplicate .

    Another reminder that you MUST follow the instruction on Continuation Add Views EntityReference filter to be available for all entity reference fields Active under the "How to use" heading to set up the EntityReference filter first for this to work.

  • Pipeline finished with Failed
    11 months ago
    Total: 937s
    #313157
  • Pipeline finished with Success
    11 months ago
    Total: 3849s
    #313162
  • 🇺🇸United States smustgrave

    Thanks for adding test coverage. For the new schema change will need an upgrade path for existing and test coverage for that update.

  • 🇪🇸Spain dimaro Seville, Spain

    Hey! I'm writing this comment after reading that one of the remaining tasks is to get feedback from the community. I find this feature quite useful. In fact, in one of the projects I'm working on, there is a need to increase the maximum number of items in an exposed filter that contains many countries. I believe that the functionality provided here provides both increasing that limit in the views you want, as well as making future filters that exceed that limit in other views automatically have the autocomplete widget.

    Example for unlimited items: 3481590-unlimited_items.png
    Example with the limit set to 100 items: 3481590-limit-set-to-100-items.png

  • First commit to issue fork.
  • Pipeline finished with Success
    9 months ago
    Total: 601s
    #357339
  • 🇮🇳India ramprassad

    +1 for this functionality to be added in core. Tested this and it works fine, it switches to autocomplete when list in the select box exceeds the limit

  • Pipeline finished with Failed
    9 months ago
    Total: 638s
    #366973
  • Status changed to Needs work 2 months ago
  • 🇺🇸United States joewhitsitt Iowa

    Coming from https://www.drupal.org/project/drupal/issues/3347343 Continuation Add Views EntityReference filter to be available for all entity reference fields Active , does anyone recall the decision to make it 100 static? What kind of performance and ux issues were being experienced to warrant this limit? Should we be providing an override if the consequences are severe?

  • Pipeline finished with Success
    13 days ago
    Total: 1010s
    #574102
  • Pipeline finished with Failed
    10 days ago
    Total: 786s
    #575981
  • Pipeline finished with Success
    10 days ago
    Total: 814s
    #575992
  • 🇨🇦Canada tame4tex

    Added upgrade path and upgrade path tests. Back to NR.

    @dimaro - Thanks for the bug fix and the review.

    @joewhitsitt - I don't recall where 100 came from. I am assuming it was a "good guess" but I could be wrong and some testing was done. Regardless, this MR will enable the developer to set it to whatever value works for them based on their server resources and site configuration.

  • Pipeline finished with Running
    9 days ago
    #576654
  • 🇨🇦Canada tame4tex

    I have also added a 10.5.x version as a patch from the 11.x won't apply to D10 due to ViewsConfigUpdater not being a Service yet.

  • The Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

    This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

    Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

  • 🇨🇦Canada tame4tex

    Failing test is related to 🐛 [10.5.x] test DB transaction isolation set to read-committed breaks tests Active . Setting back to NR.

  • 🇺🇸United States smustgrave

    smustgrave changed the visibility of the branch 3481590-views-entityreference-filter_10.5.x to hidden.

  • 🇺🇸United States smustgrave

    Problem was the bot was looking at the latest MR and checking it against 11.x so I hid it for now

  • The Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

    This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

    Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

  • 🇨🇦Canada tame4tex

    Thanks smustgrave! Trying again to see if the bot is now ok with NR.

  • The Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

    This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

    Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

  • Pipeline finished with Success
    7 days ago
    Total: 475s
    #578604
  • Pipeline finished with Failed
    7 days ago
    Total: 494s
    #578618
  • Pipeline finished with Failed
    1 day ago
    Total: 566s
    #583598
  • 🇨🇦Canada tame4tex

    There is a bug in the current MR when the filter operator is exposed.

    Error:
    TypeError: Cannot access offset of type string on string in Drupal\views\Plugin\views\filter\EntityReference->validateExposed() (line 681 of core/modules/views/src/Plugin/views/filter/EntityReference.php).

    Steps to reproduce:

    1. Follow the IS Steps to reproduce to create a View with an Entity Reference filter using the select widget.
    2. Apply the MR patch
    3. Edit the Entity Reference filter settings (via the Settings link beside the filter):
      1. Ensure the Selection type is set to Select list
      2. Ensure the Maximum entities in select list is set to more than the number of nodes created. We don't want the widget to switch to auto-complete to reproduce this bug.
    4. Modify Entity Reference filter configuration (via the filter link):
      1. Check Expose operator to expose the filter operator.
      2. Check Allow multiple selections
    5. Save the view
    6. When viewing the view set the Operator to Is one of and select one of the values
    7. Press Filter
    8. Actual result: The above error is thrown
    9. Expected result: The view results should be correctly filtered.

    Given we are modifying EntityReference::validateExposed we should add more comprehensive testing when the operator is and isn't exposed and the different filter widget configurations.

    Working on this now.

  • Pipeline finished with Failed
    about 9 hours ago
    Total: 514s
    #584522
  • Pipeline finished with Canceled
    about 9 hours ago
    Total: 125s
    #584531
  • Pipeline finished with Failed
    about 9 hours ago
    Total: 393s
    #584528
  • Pipeline finished with Failed
    about 9 hours ago
    Total: 319s
    #584533
  • Pipeline finished with Failed
    about 8 hours ago
    Total: 69s
    #584566
  • Pipeline finished with Success
    about 8 hours ago
    Total: 398s
    #584568
  • 🇨🇦Canada tame4tex

    I have pushed more comprehensive testing, of which some will fail due to the bug mentioned in #21.

    I have pushed the fix. Needed to account for the element being within a wrapper when the operator is exposed.

    Back to NR.

  • Pipeline finished with Success
    about 7 hours ago
    #584573
  • 🇨🇦Canada tame4tex

    tame4tex changed the visibility of the branch 3481590-views-entityreference-filter_10.5.x to active.

Production build 0.71.5 2024