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
- 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
- In the extra settings form in the Views Admin UI, note that only a
selectorautocompletewidget can be selected. Chooseselect. - Create more than 100 referenced nodes (Devel module & drush will help here)
- Visit the view page and note that the filter is a
selectwidget and only 100 options are listed.
Proposed resolution
- Add a
list_maxViews EntityReference filter exposed form configuration option. - This configuration option is only visible when the
selectwidget is selected. - If
list_maxis set to 0 (meaning unlimited), the filter will will always be aselectwidget. - If
list_maxis set to a number greater than 0, when the number of options hits or exceeds this limit, the widget will automatically switch from aselectwidget to anautocompletewidget.
Remaining tasks
Create MR containing new config,\Drupal\views\Plugin\views\filter\EntityReferencechanges and new tests.- 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
selectwidget to anautocompletewidget, if theselectwidget is chosen and the number of options equals or exceeds thelist_maxvalue.
Introduced terminology
N/A
API changes
N/A
Data model changes
N/A
Release notes snippet
N/A
| Comment | File | Size | Author |
|---|---|---|---|
| #30 | 3481590-views-entity-reference-list-max-11.4.x.patch | 51.37 KB | mcortes19 |
| #5 | 3481590-limit-set-to-100-items.png | 37.44 KB | dimaro |
| #5 | 3481590-unlimited_items.png | 63.07 KB | dimaro |
Issue fork drupal-3481590
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
Comment #3
tame4tex commentedI 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.
Comment #4
smustgrave commentedThanks for adding test coverage. For the new schema change will need an upgrade path for existing and test coverage for that update.
Comment #5
dimaro commentedHey! 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
Comment #7
ramprassad commented+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
Comment #8
joewhitsittComing 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?
Comment #9
tame4tex commentedAdded 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.
Comment #10
tame4tex commentedComment #12
tame4tex commentedI 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.
Comment #13
needs-review-queue-bot commentedThe 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.
Comment #14
tame4tex commentedFailing test is related to #3539366: Default DB transaction isolation set to read-committed breaks InstallerIsolationLevelExistingSettingsTest test. Setting back to NR.
Comment #16
smustgrave commentedProblem was the bot was looking at the latest MR and checking it against 11.x so I hid it for now
Comment #17
needs-review-queue-bot commentedThe 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.
Comment #18
tame4tex commentedThanks smustgrave! Trying again to see if the bot is now ok with NR.
Comment #19
needs-review-queue-bot commentedThe 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.
Comment #20
smustgrave commentedComment #21
tame4tex commentedThere 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:
Is one ofand select one of the valuesGiven we are modifying
EntityReference::validateExposedwe should add more comprehensive testing when the operator is and isn't exposed and the different filter widget configurations.Working on this now.
Comment #22
tame4tex commentedI 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.
Comment #24
smustgrave commentedLeft some additional comments on the MR.
Comment #28
ironnuts commentedIt 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.
Comment #30
mcortes19 commentedAttaching 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.views.post_update.phphunk, which fails on context.views_post_update_entity_reference_list_max()is appended at the end of the file unchanged.list_maxschema label andviews.view.test_filter_entity_reference.yml.Testing on 11.4.10 (PHP 8.4):
git applyis clean on a pristine 11.4.10 tree.Kernel/Handler/FilterEntityReferenceTest: OK (9 tests, 133 assertions).Functional/Update/EntityReferenceListMaxUpdateTest: OK (301 assertions).list_max: '0'on those filters fixed it.Feedback from using it on a real site:
list_max(default 100), the widget becomesentity_autocompletewith no#options. On our site this caused:TypeErrorin Better Exposed Filters (BetterExposedFiltersHelper::sortOptionsCustom()received null fromFilterWidgetBase) when a sort is configured for the filter;TypeErrorin a customhook_form_views_exposed_form_alter()that calledasort()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.
count($options) >= $list_maxalso switches at exactlylist_max. That's unavoidable, since onlylist_maxentities are fetched, but it means a filter with exactly 100 targets is affected too.ViewsConfigUpdaterpresave writelist_max: '100'onto everyentity_referencefilter. Sites that don't export afterwards see those views as changed inconfig:statusafter every import, because presave adds the key back each time. Worth mentioning in the release note.EntityReferenceListMaxUpdateTestis 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 inviews/tests/src/Functional/Update/already have it.Also found while debugging, and not caused by this issue: filters saved with the legacy
handler/handler_settingskeys (instead ofsub_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.