Problem/Motivation

It would be useful to distinguish between a field that has no value for a given entity and a field that has one value (delta 0). However, Views displays a NULL delta as 0 even when "Hide if empty" is checked and "Count the number 0 as empty" is not checked.

Steps to reproduce

I encountered this with Layout Builder, where there is no actual field value to test on, so that's the example I'll give. Enable Layout Builder and add a layout for a node. Edit the /admin/content/node View and add the layout_builder__layout:delta field. Note that it displays 0 for the nodes that have no layouts as well as for the node that has a layout. (If the node has more than one layout, it will appear once for each delta.)

Now try editing the field settings in the View and under No Results Behavior, check the box for "Hide if empty." Do not check the box for "Count the number 0 as empty." Apply changes, and you will see all the 0s hidden, including the one that is actually 0 and not NULL.

Now go to /web/admin/structure/views/settings and enable "Show the SQL query." Go back to your View and copy the query. Strip the quotes and curly brackets and run the query directly on your database. Note that NULL values are distinct from 0 values.

Proposed resolution

Render NULL values as an empty string. If this would break something, then at least allow for a distinction when "Hide if empty" is checked.

Remaining tasks

I have no idea. Maybe it's in the NumericField plugin?

Comments

BenStallings created an issue. See original summary.

benstallings’s picture

Issue summary: View changes

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.

clayfreeman’s picture

At least as far as I can tell, this appears to be happening because the NumericField views plugin is using strict equality:

    // Check to see if hiding should happen before adding prefix and suffix
    // and before rewriting.
    if ($this->options['hide_empty'] && empty($value) && ($value !== 0 || $this->options['empty_zero'])) {
      return '';
    }

This probably isn't correct, because the value my table returns is a string ('0') even though the column is using an int type.

clayfreeman’s picture

Title: NULL delta displayed as 0 » NULL values for numeric fields displayed as 0 instead of empty
sime’s picture

I was unable to use the delta to show whether an entity had a layout override because NULL and 0 rendered to 0.

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.