Problem/Motivation
When using a multi select exposed filter in an ajax view, it is not possible to un-select the given filter in case the filter parameter is present in the url.
Steps to reproduce
1. Install standard profile
2. Modify the content view /admin/structure/views/view/content
- change content type filter to "Allow multiple selections"
- change the view to ajax mode "Use AJAX: Yes"
3. Visit /admin/content?type[]=page
- Unselect "Basic page" (ctrl+click), then click filter
Problem: the "Basic page" is re-selected.
Proposed resolution
1. ajax_view.js only looked for inputs so updated to include select + textareas
2. Walks the exposed form once, collects every input/select/textarea base name into a Set, then compares each URL param's base name against that Set dropping any match before the AJAX request goes out.
Remaining tasks
Review
User interface changes
NA
API changes
NA
Data model changes
NA
Release notes snippet
NA
| Comment | File | Size | Author |
|---|
Issue fork drupal-3273334
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:
- 3273334-unable-to-unselect
changes, plain diff MR !16435
Comments
Comment #2
pasqualleComment #3
pasqualleComment #4
immaculatexavier commentedAdding a patch
Comment #5
immaculatexavier commentedComment #6
immaculatexavier commentedComment #7
immaculatexavier commentedComment #8
immaculatexavier commentedComment #9
immaculatexavier commentedComment #10
immaculatexavier commentedComment #11
immaculatexavier commentedComment #12
immaculatexavier commentedComment #13
immaculatexavier commentedComment #14
yogeshmpawarResolved CS issues.
Comment #19
mikemccaffreyUnfortunately, the patch in #14 did not work for me, and there are not enough details in this ticket or the code comments for me to understand what the solution is supposed to be and exactly how it is supposed to work, so I am unable to easily figure out what is not working.
Comment #20
hemangi.gokhaleIf you're running into this same issue, check whether you're using views_ajax_history. I had the exact same problem on one of my sites, and it turned out the culprit was that module. The site was using version 1.7, and after reading this thread; https://www.drupal.org/project/views_ajax_history/issues/3364206, I updated it to 1.8, which fixed the issue.
Comment #22
smustgrave commentedComment #24
smustgrave commentedNot sure why this bug surfaced for us, maybe #343535: Enable bookmarking of AJAX views made it bubble but I've been hit with this.
Disclosure used LMM for help writing this but reviewed the changes first.
Comment #25
smustgrave commentedComment #26
smustgrave commentedCould use some eyes.
Comment #27
nitinkumar_7 commentedTested the latest MR locally. I was able to reproduce the issue on the current branch, and the lattest changes resolves it. I also verified that clearing the last selected value from an AJAX exposed multi-select filter updates the results correctly and removes the query parameter from the URL. RTBC from my testing.
Comment #29
dcam commentedI had to rebase because the lack of search_user broke my local environment while reviewing.
Comment #30
dcam commentedI followed the steps to reproduce the issue (adding content types since they no longer exist in Core). I was able to reproduce it successfully. After enabling a multi-select and AJAX on the admin Content view you are unable to de-select any exposed filter. After applying the MR the issue stops.
I'm not a JS expert, but the changes look OK to me. The new test looks solid, testing deselection in a couple of different scenarios. I don't have any feedback on the code.
This one looks good to me.
Comment #32
acbramley commentedJust encountered this bug as well, I've rebased the MR and updated the tests a bit to use slightly better asserts. Otherwise this is looking pretty good!
Comment #33
acbramley commentedSimilar to #30 I found it a bit hard to see what the JS changes were actually doing (in hindsight I might have been better off viewing the whole file).
In any case, I had Claude analyze the changes and it makes it a lot easier to understand, here's what it had to say:
Before: the code stripped exposed-filter params from the query string by exact name match —
input[name="${name}"]- checking only<input>elements.After: it builds a
Setof base names from every namedinput,select, andtextareain the exposed form, stripping any[...]suffix (sotype[],type[0],type[1]all normalize totype). It then filters query-string params by checking membership in that set, also stripping the same suffix from the param name before comparing.This fixes two gaps in the old logic:
<select>/<textarea>exposed filters weren't being removed from the query string at all (only<input>was matched).type[]=1&type[]=2ortype[0]=1) weren't matched by exact name, so a user couldn't "unselect" one and have AJAX correctly drop the stale params — which lines up with the issue title "unable to unselect."Comment #34
longwaveI think we can simplify the JavaScript a bit, added two suggestions.
Comment #35
smustgrave commentedComment #36
fazal_haq commentedI manually tested the latest MR !16435 locally.
I enabled AJAX and a multi-select Content type exposed filter on the Content view. I selected Basic page and Article, applied the filter, then removed Basic page and applied the filter again.
The old value was removed from the URL correctly, and only Demo Article remained in the results.
I also ran the focused FunctionalJavascript test `testUnselectMultiSelectExposedFilter`, which passed locally with 1 test and 12 assertions.
RTBC from my testing.
Comment #37
finnsky commentedPatch applied and works fine +1 RTBC
Comment #38
smustgrave commentedAnyone want to RTBC it :)
Comment #39
dcam commentedAgain, not a JS expert. But the recent code changes look OK to me.
Comment #40
alexpottI think it would be possible to produce a version of this fix that didn't re-order the params in the urls so much. That's because this bug only happens for checkboxes and multiple selects - when the its possible for the form field to submit no value. The current code has now been changed to remove all all exposed form fields from the url including non-multiple selects and textareas - but these always have a value so the form value is going to overwrite the URL value.
So is it worth changing the code to only target form elements that might not have input (checkboxes and multiple selects) or should we leave as is. I was veering towards recommending we change the code to only target such elements but I feel the current code is actually more consistent with the original intention of the code as evidenced by the comment
// Remove current exposed filters.- the core in HEAD is not doing this and with this change it does. So let's go forward here.Comment #41
alexpottCommitted and pushed 48f2609fca1 to main and 4584557ff88 to 12.0.x and 3ff3188fb9d to 11.x and 426c41b379a to 11.5.x. Thanks!
Backported to 11.5.x as a low risk bug fix.