Views UI should offer a 'Treat NULL values as FALSE' on boolean field filtering to expose the already existing code path for handling this

Created on 19 July 2016, almost 8 years ago
Updated 26 June 2024, 2 days ago

Problem/Motivation

Views provides an option for filtering boolean fields on null/not null, but this is not exposed in the Views UI.

Proposed resolution

Expose this code in the UI (from #2):

      if ($this->accept_null) {
        if ($query_operator == static::EQUAL) {
          $condition = (new Condition('OR'))
            ->condition($field, 0, $query_operator)
            ->isNull($field);
        }
        else {
          $condition = (new Condition('AND'))
            ->condition($field, 0, $query_operator)
            ->isNotNull($field);
        }
        $this->query->addWhere($this->options['group'], $condition);
      }

Possible workaround using a views override in the meantime (from #3):

function MODULENAME_views_pre_build(ViewExecutable $view) {
  if ($view->storage->id() == 'VIEWMACHINENAME') {
    // If reviewed is set to false then we should include NULLs.
    if ($view->filter['FIELDMACHINENAME']->value == 0) {
      $view->filter['FIELDMACHINENAME']->accept_null = TRUE;
    }
  }
}

Remaining tasks

Code, review, documentation.

User interface changes

Adds "is NULL" and "is not NULL" to filtering options in UI on boolean fields.

API changes

None.

Data model changes

None.

Original report by nicxvan

I have a boolean field on a node.

I would like the view to return all nodes that do not have the checked box. (Either never set or selected and unset later)
I set the filter to Is not equal to TRUE.

No nodes with the boolean that is null are returned.

✨ Feature request
Status

Needs work

Version

11.0 πŸ”₯

Component
Views UIΒ  β†’

Last updated 2 days ago

Created by

πŸ‡ΊπŸ‡ΈUnited States nicxvan

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.69.0 2024