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

Issue fork drupal-3273334

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

Pasqualle created an issue. See original summary.

pasqualle’s picture

Issue summary: View changes
pasqualle’s picture

Issue summary: View changes
immaculatexavier’s picture

StatusFileSize
new2.04 KB

Adding a patch

immaculatexavier’s picture

Status: Active » Needs review
StatusFileSize
new3.29 KB
new2.3 KB
immaculatexavier’s picture

Status: Needs review » Needs work
immaculatexavier’s picture

Status: Needs work » Needs review
StatusFileSize
new3.4 KB
new2.97 KB
immaculatexavier’s picture

StatusFileSize
new4.82 KB
immaculatexavier’s picture

Status: Needs review » Needs work
immaculatexavier’s picture

immaculatexavier’s picture

StatusFileSize
new4.41 KB
immaculatexavier’s picture

Status: Needs work » Needs review
immaculatexavier’s picture

Status: Needs review » Needs work
yogeshmpawar’s picture

Status: Needs work » Needs review
StatusFileSize
new3.63 KB
new3.65 KB

Resolved CS issues.

Status: Needs review » Needs work

The last submitted patch, 14: 3273334-14.patch, failed testing. View results

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

mikemccaffrey’s picture

Unfortunately, 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.

hemangi.gokhale’s picture

If 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.

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.

smustgrave’s picture

Issue summary: View changes

smustgrave’s picture

Not 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.

smustgrave’s picture

Issue summary: View changes
smustgrave’s picture

Status: Needs work » Needs review

Could use some eyes.

nitinkumar_7’s picture

Tested 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.

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

dcam’s picture

I had to rebase because the lack of search_user broke my local environment while reviewing.

dcam’s picture

Status: Needs review » Reviewed & tested by the community

I 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.

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

acbramley’s picture

Just 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!

acbramley’s picture

Similar 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 Set of base names from every named input, select, and textarea in the exposed form, stripping any [...] suffix (so type[], type[0], type[1] all normalize to type). 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:

  1. <select>/<textarea> exposed filters weren't being removed from the query string at all (only <input> was matched).
  2. Multi-value fields (e.g. a multi-select filter serialized as type[]=1&type[]=2 or type[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."
longwave’s picture

Status: Reviewed & tested by the community » Needs work

I think we can simplify the JavaScript a bit, added two suggestions.

smustgrave’s picture

Status: Needs work » Needs review
fazal_haq’s picture

I 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.

finnsky’s picture

Patch applied and works fine +1 RTBC

smustgrave’s picture

Anyone want to RTBC it :)

dcam’s picture

Status: Needs review » Reviewed & tested by the community

Again, not a JS expert. But the recent code changes look OK to me.

alexpott’s picture

I 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.

alexpott’s picture

Version: main » 11.5.x-dev
Status: Reviewed & tested by the community » Fixed

Committed 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.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

  • alexpott committed 426c41b3 on 11.5.x
    fix: #3273334 Unable to unselect a multi select exposed filter in ajax...

  • alexpott committed 3ff3188f on 11.x
    fix: #3273334 Unable to unselect a multi select exposed filter in ajax...

  • alexpott committed 4584557f on 12.0.x
    fix: #3273334 Unable to unselect a multi select exposed filter in ajax...

  • alexpott committed 48f2609f on main
    fix: #3273334 Unable to unselect a multi select exposed filter in ajax...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.