Problem/Motivation

The Views boolean field filter currently filters out everything that lacks a field value for the field being filtered.

This is unexpected and does not follow the filter logic configured in the UI. For example, say I've added a new field to one or more of my content types, "Exclude", and my goal is to use this new field to filter certain content items out of my view (anything where Exclude is checked). It would seem I could achieve this by adding a filter of Exclude != TRUE to my view.

Screenshot of example views filter config

With that filter in place I would now expect to see all content where my new "Exclude" checkbox boolean field has not been checked (Exclude != TRUE). This includes old content where there is no value yet for the field, as well as content of types that do not have this "Exclude" field, because Empty/NULL != TRUE.

This does not work, as the filter seems to add an additional condition, behind the scenes, that the field must not be empty/NULL. So in reality we end up with this logic: Exclude != NULL && Exclude != TRUE. The consequence is that content with no value set for the field is also excluded.

In practice I find the mere presence of this filter, configured in any which way, will always exclude all content where there is no value in the field, due to it always adding an unexpected not empty condition into the mix.

I believe this is a bug.

Steps to reproduce

1. Add content of one or more content types.
2. Add a view that displays all of this content.
3. Add a new boolean field to onto some or all of this content. At this point no content as a value of TRUE in this new field.
4. In the view, add a new filter on this new boolean field configured Field != TRUE.
5. See that no content shows in the view.

Proposed resolution

It seems the unexpected, added Field != empty/NULL condition is not intentional, but rather a consequence of how database backends like MySQL handle comparisons to NULL (see ref), combined with no anticipation or handling of this in the filter's query condition-adding code.

It appears this can be resolved by expanding the filter's current Not Equal To conditions from [field-value] <> 1 to ([field-value] <> 1 OR [field-value] IS NULL), which anticipates the potential for NULL values in these comparisons and preserves the basic logical understanding that Empty/NULL is not equal to TRUE (or FALSE, if we're strict about it).

Remaining tasks

MR + Review.

User interface changes

What you configure is what you actually get in the views boolean field value filter.

Introduced terminology

None.

API changes

None.

Data model changes

None.

Release notes snippet

Todo.

Issue fork drupal-3516724

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

chrisolof created an issue. See original summary.

chrisolof’s picture

Issue summary: View changes
StatusFileSize
new2.11 KB

Branch 3516724-views-boolean-filter-null-bug under this issue's drupal-3516724 fork now includes a fix for this bug.

Still needs test coverage, so MR not yet opened.

The fix was to anticipate and handle the possibility of NULL/empty field values when setting up the WHERE clause in the query. Relying on a condition like [field-value] <> 1 is not enough, because that check will evaluate to FALSE if [field-value] is NULL in database backends like MySQL (see ref). Expanding the condition to ([field-value] <> 1 OR [field-value] IS NULL) resolves the issue by handling this gotcha with NULL in comparisons.

The attached patch represents the 3516724-views-boolean-filter-null-bug branch's current diff against 11.x.

Proposed resolution in the issue description updated to align with the fix that is proposed here.

chrisolof’s picture

Status: Active » Needs work
chrisolof’s picture

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.