Problem/Motivation

An old patch associated with issue #3347343: Add Views EntityReference filter to support better UX for exposed filters 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 #2429699: Add Views EntityReference filter to be available for all entity reference fields 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 #3347343: Add Views EntityReference filter to support better UX for exposed filters 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 #3347343: Add Views EntityReference filter to support better UX for exposed filters
  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

Issue fork drupal-3481590

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

tame4tex created an issue. See original summary.

tame4tex’s picture

Issue summary: View changes
Status: Active » Needs review

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 #2429699: Add Views EntityReference filter to be available for all entity reference fields.

Another reminder that you MUST follow the instruction on #3347343: Add Views EntityReference filter to support better UX for exposed filters under the "How to use" heading to set up the EntityReference filter first for this to work.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs upgrade path, +Needs upgrade path tests

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

dimaro’s picture

StatusFileSize
new63.07 KB
new37.44 KB

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

ramprassad made their first commit to this issue’s fork.

ramprassad’s picture

+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

joewhitsitt’s picture

Coming from https://www.drupal.org/project/drupal/issues/3347343, 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?

tame4tex’s picture

Status: Needs work » Needs review

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.

tame4tex’s picture

tame4tex’s picture

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.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new91 bytes

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.

tame4tex’s picture

Status: Needs work » Needs review

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

smustgrave’s picture

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

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new91 bytes

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.

tame4tex’s picture

Status: Needs work » Needs review

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

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new91 bytes

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.

smustgrave’s picture

Status: Needs work » Needs review
Issue tags: +no-needs-review-bot
tame4tex’s picture

Status: Needs review » Needs work

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.

tame4tex’s picture

Status: Needs work » Needs review

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.

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

smustgrave’s picture

Status: Needs review » Needs work

Left some additional comments on the MR.

oily changed the visibility of the branch 11.x to hidden.

oily changed the visibility of the branch 10.5.x to hidden.

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

ironnuts’s picture

It is a bit confusing having several MR's. I have hidden all but what seems to be the 'working' one. Not that it is the only valuable MR.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

mcortes19’s picture

Attaching a static patch of MR !9866 for 11.4.x sites that apply it through Composer, since the MR diff doesn't apply there.

What's in it: the MR at 374f0aad, rerolled onto 11.4.10.

  • The only conflict is the views.post_update.php hunk, which fails on context. views_post_update_entity_reference_list_max() is appended at the end of the file unchanged.
  • I also removed trailing whitespace from two added lines: the list_max schema label and views.view.test_filter_entity_reference.yml.
  • No other code changes. Status stays Needs work; this is for sites that need it now, not a replacement for the MR.

Testing on 11.4.10 (PHP 8.4):

  • git apply is clean on a pristine 11.4.10 tree.
  • Kernel/Handler/FilterEntityReferenceTest: OK (9 tests, 133 assertions).
  • Functional/Update/EntityReferenceListMaxUpdateTest: OK (301 assertions).
  • On a production site: an exposed filter with 102 target nodes was capped at 100. It rejected valid selections with "The submitted value … is not allowed" because hidden duplicate filters each dropped different entities. Setting list_max: '0' on those filters fixed it.

Feedback from using it on a real site:

  1. Switching to autocomplete can break code that expects a select. Once the option count reaches list_max (default 100), the widget becomes entity_autocomplete with no #options. On our site this caused:
    • a TypeError in Better Exposed Filters (BetterExposedFiltersHelper::sortOptionsCustom() received null from FilterWidgetBase) when a sort is configured for the filter;
    • a TypeError in a custom hook_form_views_exposed_form_alter() that called asort() on #options.

    Those filters already had 100 options, so applying the patch changed their widget with no config change. A change record calling this out, or a BEF follow-up, would help.

  2. count($options) >= $list_max also switches at exactly list_max. That's unavoidable, since only list_max entities are fetched, but it means a filter with exactly 100 targets is affected too.
  3. The post-update and ViewsConfigUpdater presave write list_max: '100' onto every entity_reference filter. Sites that don't export afterwards see those views as changed in config:status after every import, because presave adds the key back each time. Worth mentioning in the release note.
  4. Test attribute: EntityReferenceListMaxUpdateTest is missing #[RunTestsInSeparateProcesses]. That raises the 11.3 deprecation "Functional/FunctionalJavascript test classes must specify the #[RunTestsInSeparateProcesses] attribute", which becomes an exception in 12.0. Other tests in views/tests/src/Functional/Update/ already have it.

Also found while debugging, and not caused by this issue: filters saved with the legacy handler / handler_settings keys (instead of sub_handler / sub_handler_settings) silently ignore their target bundles. They list the first 100 entities of any bundle. With this patch those filters switch to autocomplete. Not in scope here, but it might deserve its own issue or an update path.

AI-assisted: I prepared this reroll and these notes with an AI coding assistant. I reviewed the patch and the test results and take responsibility for them.