Problem/Motivation
Exposed filter with 'Does not contain' operator can seem buggy with multi-value fields.
Steps to reproduce
- Install Drupal
- Add a text field 'Year' to the article content type with unlimited values
- Create a node with the values: 2015 and 2016
- Create a node with the values: 2015 and 2018
- Create a view of articles, add an exposed filter for 'Year' with the operator 'Does not contain'
- Search for '2018'
- 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 0Proposed resolution
Remaining tasks
- Agree this is a bug
- Write a patch with tests
- 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.
| Comment | File | Size | Author |
|---|---|---|---|
| #6 | screenshot-no-matching-value.png | 26.04 KB | shriaas |
Comments
Comment #2
chrotto commentedComment #5
pameeela commentedI 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.
Comment #6
shriaasI 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:

So for every value in
Yearfield 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
ANDoperator instead ofORso 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.Comment #7
shriaasViews 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