Multi-selected grouped view filters use AND instead of OR.

Created on 3 April 2025, 2 months ago

Problem/Motivation

Grouped filters are not behaving correctly when multiple groups are selected in an exposed filter. The expectation for multiselect view filters, is that they combine selections using OR, not AND.

Steps to reproduce

My view was very complex and depended on multiple modules. But I followed these steps with a blank view and the problem still occurred.

  1. Create a view (based on any fieldable entity)
  2. Add an exposed dropdown filter (I recommend a List (text) field)
  3. Select Grouped filters and create some groupings
  4. Allow multiple selections
  5. Add a view page for easy testing and save your view
  6. Visit your view and select multiple groups on your exposed filter

Example: Selecting Group 1 and Group 2

Selecting multiple groups should show all the items that match any of the group values (Group 1 OR Group 2)

Selecting multiple groups shows all the items that match both of the group values (Group 1 AND Group 2)

Proposed resolution

The issue is linked to ViewExecutable::_build() and FilterPluginBase::convertExposedInput().
_build() loops through the different selected groups and gets the group values and saves them in the exposed_data property. It then adds them to the query within the loop, creating a different WHERE condition for each selected group, which is why it follows AND instead of OR.

I have created a patch that only adds the query once it has looped through each selected group. However, each loop overrides the previously set values, so only the final group would be added to the query. In my patch, after the first loop, the code starts combining the values of each group. This way it creates only 1 WHERE condition with all the values for that filter to apply.

🐛 Bug report
Status

Active

Version

10.4

Component

views.module

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @scambler
  • Pipeline finished with Success
    2 months ago
    Total: 533s
    #464660
  • Pipeline finished with Success
    2 months ago
    Total: 640s
    #464667
  • 🇳🇿New Zealand quietone

    Changes are made on on 11.x (our main development branch) first, and are then back ported as needed according to the Core change policies .

    Drupal 10 is now an LTS, or in maintenance mode, and there are limited allowed changes for a maintenance minor release .

  • scambler changed the visibility of the branch 11.x to hidden.

  • Thanks @quietone. I've made a bit of a mess, but I think I've done it right this time.

  • Pipeline finished with Success
    about 2 months ago
    #467429
  • 🇺🇸United States smustgrave

    Thanks for reporting, will also need a test case showing the issue.

  • Pipeline finished with Failed
    about 2 months ago
    Total: 118s
    #467762
  • Pipeline finished with Success
    about 2 months ago
    Total: 525s
    #467766
  • Hi, I've verified and tested MR! 11755 and applied the patch successfully on 11. x-dev Version: The changes are working as expected.

    1. Create a Test Content Type : Go to /admin/structure/Content types/Add content type
    2. Add a List (text) Field: Go to Manage fields for Test Content. Add a field: Category (List (text)).Add the allowed values: eg, one, two, three. Create at least 3 items, assigning various Category values
    3. Create Some Content: Go to Content > Add content > Test Content
    4. Create a View: Go to Structure > Views > Add view. Show: Content of type Test Content. Create a page display. Check "Create a page", set path (e.g. /test-filter)
    5. Add Exposed Filter: In your view:. Click Add Filter Criteria. Choose the Category field you created. Click Expose this filter to visitors. Check Allow multiple selections. Click Grouped filters. Add groups, eg, G1 one and two, G2 three .Save the view
    6. Test the Filter Behavior: Visit your view page (/test-filter). Try selecting Group 1 and Group 2
    7. Observed the changes here

    Testing Result:

    After selecting the multi-selected filter, the OR function is working (You can see all content with either one, two, or three). Hence, the changes are working as expected.

    Attaching the videos for reference.

    Hence, moving to RTBC!

    Thanks

  • Hi, I've verified and tested MR! 11755 and applied the patch successfully on 11. x-dev Version: The changes are working as expected.

    1. Create a Test Content Type : Go to /admin/structure/Content types/Add content type
    2. Add a List (text) Field: Go to Manage fields for Test Content. Add a field: Category (List (text)).Add the allowed values: eg, one, two, three. Create at least 3 items, assigning various Category values
    3. Create Some Content: Go to Content > Add content > Test Content
    4. Create a View: Go to Structure > Views > Add view. Show: Content of type Test Content. Create a page display. Check "Create a page", set path (e.g. /test-filter)
    5. Add Exposed Filter: In your view:. Click Add Filter Criteria. Choose the Category field you created. Click Expose this filter to visitors. Check Allow multiple selections. Click Grouped filters. Add groups, eg, G1 one and two, G2 three .Save the view
    6. Test the Filter Behavior: Visit your view page (/test-filter). Try selecting Group 1 and Group 2
    7. Observed the changes here

    Testing Result:
    After selecting the multi-selected filter, the OR function is working (You can see all content with either one, two, or three). Hence, the changes are working as expected.

    Attaching the videos for reference.

    Hence, moving to RTBC!
    Thanks

  • The 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.

Production build 0.71.5 2024