Problem/Motivation
When an in_operator filter plugin is exposed and "Limit list to selected items" is checked, the default selection of "- Any -" displays all items, including those from unselected options. The "Limit..." option has no effect.
Steps to reproduce
Starting from an install of the Standard profile:
- Create an Article node.
- Create a Basic page node.
- Edit the Content admin view at
/admin/structure/views/view/content. - Open the settings for the "Content: Content type (exposed)" filter.
- Under the Operator settings, verify that "Is one of" is selected.
- Under the Content Types settings, check "Article".
- Check the "Limit list to selected items" checkbox.
- Apply the filter settings and save the edited view.
- View the Content admin view at
/admin/content. - Verify that the exposed filter "Content type" has the default option of "- Any -" selected.
Expected result
The results should be limited to only nodes of the Article content type.
Actual result
The results contain nodes of all content types.
Proposed resolution
InOperator::acceptExposedInput() has a condition on $this->options['expose']['limit']. The 'limit' filter option does not exist. Change it to the actual option's name, 'reduce'.
The above solution was committed to 11.4.0, but was reverted because this was a behaviour change (see #54 onwards).
Instead we are doing the following:
- A new limit setting is added to InOperator fliters
- When BOTH reduce and limit are set to TRUE, the selected filter options are applied by default via InOperator::acceptExposedInput()
- An upgrade path to set limit to FALSE for all existing exposed filters using in_operator plugin. This maintains BC and won't change behaviour for existing views but also allows users to configure the new behaviour.
Remaining tasks
Review
User interface changes
NA
API changes
NA
Data model changes
NA
Release notes snippet
NA
| Comment | File | Size | Author |
|---|---|---|---|
| content-type-filter.jpg | 132.51 KB | devad |
Issue fork drupal-3132725
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 #2
Ashutosh.tripathi commentedHi @devad,
As the statement "Limit list to selected items" states that it should limit the items shown in the dropdown. It seems to be working fine as its properly filtering the dropdown. This checkbox "Limit list to selected items" only meant to limit the dropdown list not the result set as its an exposed filter and will act only when you select something. Hope this makes sense!
Thanks
Ashutosh
Comment #3
dwwMore accurate category and status.
Thanks,
-Derek
Comment #4
dwwp.s. If you don't want the '- Any -' choice (which is what's giving you grief), you have to set that exposed filter to be 'Required'. That's really the problem you're hitting...
Comment #5
devad commentedThank you all for kind replies and help in understanding the real meaning of checkbox title and description.
Due to not so precise use of words in checkbox title and description for all drupalers who will come for the first time to this checkbox in the future - there will always be two ways how they can understand checkbox functionality.
So, I have opened separate issue as follow-up to this one... a small initiative to slightly alter checkbox description in order to make its meaning more precise and to avoid any further misunderstandings about its proper use.
Please, feel free to join: #3133906: Change unclear Exposed filter option "Limit list to selected items" title and description
Comment #6
steyep commentedUnderstanding that this is working as intended but the InOperator filter contains code that should limit the "all" filter to the selected items:
The related exposed filter form is assigning that value to
reduceinstead oflimit:So, one possible work around would be to alter the value in a pre build hook:
Comment #7
devad commentedThis bug (which has evolved into feature) is still around although Views joined the Drupal core many years ago.
Let's assign the "Bug smash initiative" to this issue and see if the Bug smash initiative team has will to deal with it.
It would be great to have Views cleaned up from this ugly 14 years old bug before D11 is released. :)
The issue below has a working D7 patch witch depicts nicely the original
'reduce' -> 'limit'typo-mistake which caused this bug to appear 14 years ago:#1309578: When using operator "Is one of" and "Limit list to selected items" - 'Any' should not ignore the selected items.
I am willing to help with manual tests and feedbacks as much as I can.
Comment #8
devad commentedAdditionally... it would be nice if we can make here the "Limit list to select items" option to work nicely with "Is not one of" operator as well... which is not the case currently... but of course, it can be done in the separate issue as well.
Comment #9
quietone commentedOccasionally, I check the Drupal 8 issue queue and today was one of those days. Since Drupal 8 is no longer supported I am moving this to 11.x where it will be seen by the community. I am also removing the bug smash tag because that is added after the Bug Smash Initiative has triaged an issue.
Comment #13
carlos romero commentedGood morning everyone.
Comment #6 is right, I have tried what he says and he is right, thank you very much steyep.
I have created a fork branch and done mr to 10.2.x and 11.x.
Greetings to all.
Comment #14
carlos romero commentedComment #15
smustgrave commentedThanks for working on this old one.
Can the issue summary be updated using the standard issue template please
Changed to a bug as support requests typically don't have code fixes. But will need a test case showing the issue.
Comment #16
carlos romero commentedComment #17
carlos romero commentedComment #18
carlos romero commentedComment #19
carlos romero commentedHello, I have updated the summary as you suggested, I hope you like it, greetings.
Comment #20
johnvComment #21
johnvComment #22
johnvUpdated the issue summary to standard format, and added a better test script.
Comment #23
johnvComment #24
smustgrave commentedNot sure if you forgot to push but don’t see any test added to the MR
Comment #27
smustgrave commentedWanted to comment that I got hit by this bug today and the MR does appear to work.
Comment #29
smustgrave commentedComment #30
smustgrave commentedComment #31
smustgrave commentedComment #32
dcam commentedI had to rewrite the issue summary. The instructions were so vague it took 20-30 minutes of reading and testing to figure out what the problem is.
I left two comments on the MR. The change to the plugin is simple enough. The test looks good. But the documentation needs work.
Comment #33
smustgrave commentedComment #34
dcam commentedMy feedback was addressed. Looks good to me.
Comment #35
quietone commentedI tested this on main. The standard install profile no longer provides Article and Page, so I created three content types a, b, and c. Then I made a node of each type. I then followed the steps to reproduce from #3 onward. AT step 7, "Check the "Limit list to selected items" checkbox." there is no such checkbox. I continued, and when on the content page, the results showed all 3 nodes. I applied the diff, cleared cache, refreshed the view and only content of type A was displayed. And when two content types are selected to be exposed, then only nodes of that type were displayed.
It would be better to properly fix this and test for TRUE. But that could be done in a followup where the 'weird implementations' mentioned in this comment are considered.
I updated credit and didn't find any unanswered questions.
Comment #36
dwwI also recently got nailed by this on a project. I tried the MR diff. Unfortunately, it had no effect in my case, since the filter that's giving me the grief allows multiple inputs. For reasons I don't fully understand, the first clause in the impacted
ifis this:empty($this->options['expose']['multiple']). So that specifically prevents this from working as expected, even with the patch applied. I tried removing that check, but it still doesn't work. 😅 So something is more fishy here. Seems like it should be possible for this to work as described, even with multiple inputs allowed. But that's not yet the case. So, another follow-up would be to either:- Get this working, even for 'allow multiple' exposed filters.
OR
- Use
#statesto hide the Limit list to selected items checkbox if the Allow multiple selections box is checked.All that said, once I re-configured the exposed filter to not accept multiple values, the 'Limit list...' checkbox is now working as expected, and rows that do not match the available choices in the filter are excluded.
So +1 to RTBC and committing this as-is, but yeah, definitely want to revisit this in a few follow-ups...
Comment #37
dwwThe failure in the last pipeline is due to #3588024: PHP 8.6 - Functions mb_regex_encoding() and mb_ereg() are deprecated. It would be all green if we rebase against latest main. Not sure that's needed, but it's an option...
Meanwhile, opened an MR thread. I'm glad to see any test coverage at all here, but the
Unittest is so specific, it doesn't give me tons of confidence. The comment for the test is misleading. Either we should fix that comment, or make a real test that confirms what the comment says we're testing...Comment #38
smustgrave commentedIf I get time this week I’ll try and convert to another test. I didn’t see any existing view that had this setting and not sure it was overkill to add a new one
Comment #39
smustgrave commentedComment #40
dwwcore/modules/views/tests/src/Kernel/Handler/FilterInOperatorTest.phpis close. We don't necessarily need a whole new test view. The test can alter the configuration of the test view to enable this feature and see how that changes the behavior/output.Comment #41
dwwp.s. I don't mind leaving the Unit test as-is (except for fixing the comment). It's okay to have Unit tests. 😅 We just shouldn't put too much faith in them on their own.
Comment #42
dwwOpened #3592464: Either make 'Limit list to selected items' work with multiple inputs, or use #states to hide the option for my concerns at #36...
And #3592465: [PP-1] Change clause in InOperator::acceptExposedInput() for 'reduce' to test for TRUE instead of !empty() for https://git.drupalcode.org/project/drupal/-/merge_requests/7121#note_816736
Comment #43
smustgrave commentedConverted to a kernel test! Thanks for the pointers @dww
Comment #44
dwwCool, that's much better. Due to #3576458: [regression] Subsystem and Topics maintainers require access to re-run, trigger, or view tests I can't trigger the test-only job. But running locally, if I revert the fix, I get this:
With the fix, it passes locally (just like the bot).
I wondered for a second if we should also assert that the options in the exposed filter match the configuration. But that would require either using the new
drupalGet()inKernelstuff, or converting this toFunctional. No thanks. I think this test is enough to handle the actual bug here, and additional test coverage could happen later.I don't see anything else to complain about. Back to RTBC.
Thanks!
-Derek
Comment #49
godotislateCommitted and pushed c6be972 to main, 4f29e29 to 11.x, and f7c6fbe to 11.4.x. Thanks!
Comment #51
smustgrave commentedWooo!!
Comment #52
devad commentedAmazing! Thanks all.
Comment #54
acbramley commentedI just opened a bug report for this exact issue #3608886: Limit list to selected items option applies exposed filter by default I think this behaviour is only going to be applicable for some sites, this has broken some of our views.
For example, we have a search_api view that shows multiple entity types. We have a block type filter and that is limited to just several block types that we are indexing.
Now with this change, every other entity type is filtered out by default and there is no way to get around that because those limited options are being applied automatically.
This feature should probably be put behind another config option?
Comment #55
acbramley commentedI think there's a fairly good argument to revert this, nothing in the configuration screen mentions that using the "Limit list to selected items" option would automatically apply the filters. There's plenty of scenarios where you would want to limit the list but still allow the filter to be optional.
Comment #56
catchRe-opening, a revert sounds reasonable to me and probably adding back with an additional checkbox. Also I think I have the same understanding of this feature as @acbramley - e.g. it should limit what can be selected, not preselect?
Comment #57
pameeela commentedI agree with @acbramley and @catch that I've always understood this to mean "limit the list of filters to the selected options."
+1 to revert.
Comment #58
smustgrave commentedSo putting back a key that doesn’t exist?
Comment #59
smustgrave commentedNew configuration sounds nice but think I’d be -1 for reverting a bug. Previously to get this to work you’d have to add a 2nd filter that actually does the filter which can have its own fun.
Also want to add that limiting a drop down seems like something better_exposed_filters would do. But adding a filter that doesn’t actually filter doesn’t seem right in the section it’s in.
Comment #60
acbramley commentedIt does filter, you are just limiting what a user can apply. With this change in place, you are forcing an exposed filter on to the query before the user does any action, this is quite a big change in behaviour depending on how your view is set up.
A clean revert would be easiest, and then we work on the change again and agree how it should actually be implemented. IMO this could just be another toggle that is displayed after selecting the "Limit list" option that says something like "Automatically apply these filters" or whatever wording makes sense.
Comment #62
pameeela commentedI don't think it's a question of what seems right or not, this is a pretty significant and unexpected change in behaviour with no warning. I agree reverting seems weird, but the new config has to provide the new behaviour otherwise it doesn't solve anything here.
You can argue the original intent but the wording is ambiguous and it's reasonable for people to assume the intent was what it actually did, which was to limit the filter list.
Comment #63
godotislateThis is almost certainly a fix of a typo bug that dates to at least 2009: https://git.drupalcode.org/project/views/-/blob/31dd0540d4a893f03d0fead8...
There was never an options property called
limitin the In operator, and it was alwaysreduce, so it was broken code that never evaluated to TRUE.Regardless, it was that way for so long, it's natural that people to expect or gotten used to the former functionality.
Comment #64
acbramley commentedI think that's the biggest argument here for revert, I have several views that are broken because of this change. The search api one (described in #54) especially I can't see a way around to have it work again without reverting this and then adding additional configuration to toggle it.
Comment #68
catchWent ahead and committed/pushed the revert to main, 11.x and 11.4.x
Let's try to add this back with the extra checkbox, can't really see another way around it.
Comment #70
devad commentedRe: #68. I agree.
This issue will never be solved to everyone's satisfaction until the additional checkbox is added to fully maintain backwards compatibility.
Some suggestions for the checkbox label:
"Limit view results to selected items"
"Limit results to selected items"
Comment #71
acbramley commentedI'm working on the new implementation
Comment #73
acbramley commentedMR is up with the following:
1. New option
limit(I'm not really sure what the key should be, but I thought it would be fun to reuse the one that went missing)2. States API to only show when reduce is checked
3. Adjusts logic to only apply the filters when BOTH reduce and limit are TRUE in
acceptExposedInput(tbh I don't really understand why this particular line of code does what it does but it was the original fix so I've kept it there)4. Upgrade path with tests
5. Updates all shipped config in core
6. Expands test coverage with reduce = TRUE and limit = FALSE
I think the title and IS are going to need updates but wanted to send for review first to get opinions on the new setting key first.
Comment #74
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. 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 #75
acbramley commentedComment #76
smustgrave commentedThanks @acrambely for working on this. I tested following the summary
Created 3 content types and content for each
Created a view with an exposed filter limited to just 2 content types selected
See content for all 3 (as before)
With the MR the new configuration option appears and filters out the 3rd content type.
I re-ran a new pipeline and the nightwatch was random and the other failure seems unrelated.
LGTM!
Comment #77
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 #78
acbramley commentedComment #79
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 #80
acbramley commentedbot lied, rebased cleanly.
Comment #81
godotislateApologies, took a while for me to get back to this one.
We've started to avoid needing to populate new config properties, examples:
https://www.drupal.org/project/drupal/issues/3529464#comment-16149611
https://www.drupal.org/project/drupal/issues/256287#comment-16738735
I think we can do the same here, with this change to views.filter.schema.yml:
We should also make so that either the limit value is saved as
true, or that it isn't saved in config at all, (i.e., there should be nolimit: falseentries). I think this can be done in InOperator by overridingFilterPluginBase::submitOptionsFormand unsetting thelimitvalue from form state if it isFALSEor ifreduceisFALSE.Then we can remove all the changes to the views config files, the update hook, and update test.
Everything else looks pretty good to me.
Comment #82
acbramley commented@godotislate do we have any examples of this for views config? It seems to be much more complicated in this case.
I implemented
submitExposeForm(this is called from submitOptionsForm) on InOperator and unset the key from form_state when either are false, however there is a lot of options merging/handling inDrupal\views_ui\Form\Ajax\ConfigHandler::submitForm.At line 219 -
$handler = $form_state->get('handler');- at this stage$handler->optionsalready contains the limit key. When it gets down tounpackOptions, it then merges the form state's options into the handler options. Since the key already exists at that point it doesn't get removed.I then added
unset($this->options['expose']['limit']);to the submitExposedForm but it still didn't work because the same ConfigHandler initialises a new handler and calls init() on it. init() populates options again based on options defined in defaults via defineOptions. You can't remove the key from defineOptions otherwise it won't save in the first place...That means we have to unset it in submitFormCalculateOptions as well. It's pretty messy but it does work and has test coverage. Let me know what you think.
I used Claude to help me figure out this last bit, I've left its comment in the method override as I think it provides useful context.
Comment #83
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. 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 #84
acbramley commentedComment #85
godotislateNo, I don't think so. It's something fairly new we've only tried or done in a couple issues. Thanks for investigating. I'm on PTO this week, so I can't say for sure when I'll take a look, but it's on my list.