Problem/Motivation

UI Issue
If a base field on an entity's base table is used as a filter criteria, there are no operator choices for "Is empty (NULL)" and "Is not empty (NOT NULL)".

I discovered this with a custom entity when I wanted to find null values. The only example I can find in core uses the file entity.

Steps to reproduce

  1. Create a view for files.
  2. Add a filter criteria. Select filename as the field.
  3. Note that "Is empty (NULL)" and "Is not empty (NOT NULL)" do not appear in the operator selection.

Proposed resolution

Make "Is empty (NULL)" and "Is not empty (NOT NULL)" options available on Views UI filters for all Not Required fields.

Remaining tasks

Code review

User interface changes

"Is empty (NULL)" and "Is not empty (NOT NULL)" options available on Views UI filters for all Not Required fields.
Before:
Before MR

After MR Solution:
After MR

API changes

None

Data model changes

None

Original description

I'm not quite sure if this should be a bug or a feature request.

If a base field on an entity's base table is used as a filter criteria, there are no operator choices for "Is empty (NULL)" and "Is not empty (NOT NULL)".

I discovered this with a custom entity when I wanted to find null values. The only example I can find in core uses the file entity.

Steps to reproduce.

  1. Create a view for files.
  2. Add a filter criteria. Select filename as the field.
  3. Note that "Is empty (NULL)" and "Is not empty (NOT NULL)" do not appear in the operator selection.

I think it is a reasonable expectation to be able to search for null and not null. I know "allow empty" can be added per entity in a views_data handler or a hook_views_data or hook_views_data_alter but is there any reason this shouldn't be added automatically?

Issue fork drupal-3098560

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

tetranz created an issue. See original summary.

tetranz’s picture

StatusFileSize
new675 bytes

Let's see what the tests do with this.

I figured that this only makes sense if the field is not required.

tetranz’s picture

Status: Active » Needs review

Version: 8.8.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. 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.

pankaj.singh’s picture

StatusFileSize
new22.6 KB
new25 KB

@tetranz thanks for the patch. Tested the patch on 8.9. It's working on my end.

Please find the SS attached for ref.

pankaj.singh’s picture

StatusFileSize
new23.65 KB
new17.28 KB

Also, tested the patch on 9.1, it works as intended.

rgpublic’s picture

Thanks for the patch. It works perfectly for me. Hopefully, this gets committed soon. It's always a bit annoying & unexpected if you need patches for very basic stuff like that.

chaithanya.m’s picture

Status: Needs review » Reviewed & tested by the community

I have applied the patch #2 and it works fine after applying the patch. The option for Is empty (NULL) and Is not empty (NOT NULL) will be there in the options list after applying the patch.

lendude’s picture

Category: Bug report » Feature request
Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

Thanks for working on this, but this can't be committed without automated tests. So we need to add some.

Setting this to be a feature request.

er.garg.karan’s picture

I created a custom field of type boolean and this patch didn't work in that case.

vasike’s picture

tested the patch (#2) on 8.9.15 and i confirm it's working

for entity reference Base field ...

vikashsoni’s picture

StatusFileSize
new24.04 KB
new28.3 KB

@tetranz I have applied Patch
Patch is working fine for reference sharing screenshot ....

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.

vasike’s picture

Version: 9.5.x-dev » 10.1.x-dev

let's try with latest 10.1 to "gain attention"

Version: 10.1.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, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

vasike’s picture

Version: 11.x-dev » 10.1.x-dev
Status: Needs work » Needs review
Issue tags: -Needs tests

Rebased the PR and updated with tests for checking "allow empty" option for not required fields.

smustgrave’s picture

Version: 10.1.x-dev » 11.x-dev
Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative, +Needs issue summary update

Will need a 11.x branch as that's the latest development branch.
Recommend starting a new one.

Also think the issue summary could use some love. Recommend adding the default template.

vasike’s picture

Issue summary: View changes
Status: Needs work » Needs review
Issue tags: -Needs issue summary update

Updates:
- Create new MR for 11.x branch
- Update issue summary

so i think we're back to Needs Review.

vasike’s picture

Issue summary: View changes
smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Confirmed the MR addresses the Null and Not Null options are available for the filename.

May need a change record but will see if the committer agrees.

quietone’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs change record

Doing RTBC issue triage.

I read the IS and used the steps to reproduce and successfully confirmed the problem. Since this is a change to the user interface there should be screenshots linked to in the Issue Summary. I read the comments and do not see any comment saying that the code and test were reviewed. I also wonder if the test fails without the fix.

Regarding a change record, yes add one. This is for views which is an interface with many, many options so we should let everyone know about it. The CR should have screen shots.

Setting back to NW.

quietone’s picture

Title: Base field filter operators do not include "Is empty (NULL)" and "Is not empty (NOT NULL)" » Add 'Is empty (NULL)' and 'Is not empty (NOT NULL)' to field filter operators

Adding link to Documentation Gate about when to add change records, https://www.drupal.org/about/core/policies/core-change-policies/core-gat...

vasike’s picture

Issue summary: View changes
Status: Needs work » Needs review
StatusFileSize
new1010 bytes

Updates:
- screenshots for issue summary
- patch with tests only for seeing ... it fails.
- Added change record draft: https://www.drupal.org/node/3370509

Status: Needs review » Needs work

The last submitted patch, 28: 3098560-test-to-fail.patch, failed testing. View results

vasike’s picture

Status: Needs work » Reviewed & tested by the community

Failed asserting that an array has the key 'allow empty'.
And the patch with tests without "fix" fails ... as intended

So it think we're back to RTBC

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 28: 3098560-test-to-fail.patch, failed testing. View results

vasike’s picture

Status: Needs work » Reviewed & tested by the community

"Remove" the patch with tests without "fix" ... so it wouldn't change the issue status ... no more

quietone’s picture

Issue summary: View changes
Status: Reviewed & tested by the community » Needs review
Issue tags: -Needs change record

@vasike, thank you for doing that last bit of work.

There are comments that this patch works, although not for boolean but that makes sense to me. What I still do not see is that anyone has reviewed the code. I scanned it but I don't know enough of views to do a proper review. I have added that to the IS in the remaining tasks.

I updated credit but, as always, it needed to be checked by the committer.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Change seems small and addresses the issue.

Am curious why this just appears on file views outside the core one.

joevagyok’s picture

StatusFileSize
new2.39 KB

Adding a patch version of the MR for composer patching. After reviewing the solution, we will use the patch on our sites to support this functionality for base fields.

xjm’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

Nice work on this! I posted a couple small nitpick suggestions on the MR.

I also think this should maybe have some UI test coverage, in addition to the kernel test coverage that's being added already?

smustgrave’s picture

Status: Needs work » Postponed (maintainer needs more info)

Can someone test if #3322402: Add 'Is empty (NULL)' and 'Is not empty (NOT NULL)' operators to boolean field filtering solves the issue here? If not what should be addressed here that isn't addressed other there.

vasike’s picture

Title: Add 'Is empty (NULL)' and 'Is not empty (NOT NULL)' to field filter operators » [UI] Add (Missing) 'Is empty (NULL)' and 'Is not empty (NOT NULL)' to field Filter Operators Options
Issue summary: View changes
Status: Postponed (maintainer needs more info) » Needs review
Issue tags: -Needs tests

@smustgrave it's not the same ... it should be clear from issue summary - User interface changes section
So this is about missing those options ... the other issue it's about their actually filtering ...

Anyway updated the MR with latest 11.x and also checked the tests ... they fail without the fix.

So I think this should be back on track.

harlor’s picture

smustgrave’s picture

Cleaned up the ticket some hiding old patches and closing an old MR.

Rebased the 11.x branch as it was 200 commits back.

Did run the test-only feature and got

1) Drupal\Tests\views\Kernel\Entity\EntityViewsDataTest::testBaseTableFields
Failed asserting that an array has the key 'allow empty'.
/builds/issue/drupal-3098560/core/modules/views/tests/src/Kernel/Entity/EntityViewsDataTest.php:433
FAILURES!
Tests: 9, Assertions: 323, Failures: 1.
Exiting with EXIT_CODE=1

Which shows test coverage.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Still green. Again just did a rebase not submit new code so think I’m good to mark

quietone’s picture

Title: [UI] Add (Missing) 'Is empty (NULL)' and 'Is not empty (NOT NULL)' to field Filter Operators Options » Add 'Is empty (NULL)' and 'Is not empty (NOT NULL)' to field Filter Operators Options

  • longwave committed a551f2b2 on 11.x
    Issue #3098560 by vasike, smustgrave, tetranz, joevagyok, pankaj.singh,...
longwave’s picture

Status: Reviewed & tested by the community » Fixed

Not eligible for backport as it's a feature request.

Committed a551f2b and pushed to 11.x. Thanks!

Also published the change record.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

xjm’s picture

Amending attribution.