Problem/Motivation

Exposed filter with 'Does not contain' operator can seem buggy with multi-value fields.

Steps to reproduce

  1. Install Drupal
  2. Add a text field 'Year' to the article content type with unlimited values
  3. Create a node with the values: 2015 and 2016
  4. Create a node with the values: 2015 and 2018
  5. Create a view of articles, add an exposed filter for 'Year' with the operator 'Does not contain'
  6. Search for '2018'
  7. See that both nodes appear

If you update the second node to remove '2015' (so that it only contains a single value of '2018') and try again, you will see that the filter for '2018' does not display that node, as expected.

Here is the SQL query for this:

SELECT "node_field_data"."created" AS "node_field_data_created", "node_field_data"."nid" AS "nid"
FROM
{node_field_data} "node_field_data"
LEFT JOIN {node__field_year} "node__field_year" ON node_field_data.nid = node__field_year.entity_id AND node__field_year.deleted = '0'
WHERE ("node_field_data"."status" = '1') AND ("node_field_data"."type" IN ('article')) AND ("node__field_year"."field_year_value" NOT LIKE '%2018%' ESCAPE '\\')
ORDER BY "node_field_data_created" DESC
LIMIT 11 OFFSET 0

Proposed resolution

Remaining tasks

  1. Agree this is a bug
  2. Write a patch with tests
  3. Review

User interface changes

N/A

API changes

N/A

Data model changes

N/A

Release notes snippet

N/A

Original report

I am trying to use an exposed filter on a view with multi-value field.
The field is a text field in the user account. The field is holding years like 2015, 2016, 2017, 2018.
I want to filter on the appearance of a certain year. Using the filter with the operator Include, it works but using it with the operator Not include it is not filtering at all but display all posts.
This seems to be a bug to me. Perhaps it is a way to make a workaround. If there is, please share it to me.

CommentFileSizeAuthor
#6 screenshot-no-matching-value.png26.04 KBshriaas

Comments

chrotto created an issue. See original summary.

chrotto’s picture

Issue summary: View changes

Version: 8.5.4 » 8.5.x-dev

Core issues are now filed against the dev versions where changes will be made. Document the specific release you are using in your issue comment. More information about choosing a version.

Version: 8.5.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Branches prior to 8.8.x are not supported, and Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

pameeela’s picture

Title: Views filter on multi value field » Exposed filter with 'Does not contain' operator has unexpected behaviour with multi-value fields
Issue summary: View changes
Issue tags: +Bug Smash Initiative

I see why this is occurring. Any node that has a value that does not contain the year will match. E.g. if a node has 2018 and 2015, that is counted as 'does not contain 2015' because one of the values does not. So in a way it works as expected, it just depends what you expect. Updated IS for easier triage, perhaps others will weigh in on whether to change this behaviour.

shriaas’s picture

StatusFileSize
new26.04 KB

I am able to reproduce the issue, in addition to that if you put a value(say 2021) that is not there in any article you will see duplicate results like this:
Screenshot
So for every value in Year field a row will be returned. I am not sure if this is the intended behavior, according to me, there should be a view row for each node instead of each field value.

This will require changes in the query to group the results for each node using AND operator instead of OR
so for example if the filter has the value "2015", and Year field contains 2016,2020,2015 the condition will be something like:
if((f1 != 2015) AND (f2 != 2015) AND (f3 != 2015)) which will return false.

shriaas’s picture

Views returns duplicate results for any multi-valued field(which has its own table) there are many issues for the same problem including the current issue.
Here is one of the related issue: https://www.drupal.org/project/drupal/issues/2993688

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

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

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should 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.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should 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: 9.5.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. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

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.