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
- Create a view for files.
- Add a filter criteria. Select filename as the field.
- 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:

After MR Solution:

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.
- Create a view for files.
- Add a filter criteria. Select filename as the field.
- 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?
| Comment | File | Size | Author |
|---|
Issue fork drupal-3098560
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:
- 3098560-11-x
changes, plain diff MR !4060
- 3098560-base-field-views-empty-operator
changes, plain diff MR !4018
Comments
Comment #2
tetranz commentedLet's see what the tests do with this.
I figured that this only makes sense if the field is not required.
Comment #3
tetranz commentedComment #5
pankaj.singh commented@tetranz thanks for the patch. Tested the patch on 8.9. It's working on my end.
Please find the SS attached for ref.
Comment #6
pankaj.singh commentedAlso, tested the patch on 9.1, it works as intended.
Comment #7
rgpublicThanks 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.
Comment #8
chaithanya.m commentedI 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.
Comment #9
lendudeThanks 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.
Comment #10
er.garg.karanI created a custom field of type boolean and this patch didn't work in that case.
Comment #11
vasiketested the patch (#2) on 8.9.15 and i confirm it's working
for entity reference Base field ...
Comment #12
vikashsoni commented@tetranz I have applied Patch
Patch is working fine for reference sharing screenshot ....
Comment #17
vasikelet's try with latest 10.1 to "gain attention"
Comment #20
vasikeRebased the PR and updated with tests for checking "allow empty" option for not required fields.
Comment #21
smustgrave commentedWill 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.
Comment #23
vasikeUpdates:
- Create new MR for 11.x branch
- Update issue summary
so i think we're back to Needs Review.
Comment #24
vasikeComment #25
smustgrave commentedConfirmed 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.
Comment #26
quietone commentedDoing 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.
Comment #27
quietone commentedAdding link to Documentation Gate about when to add change records, https://www.drupal.org/about/core/policies/core-change-policies/core-gat...
Comment #28
vasikeUpdates:
- screenshots for issue summary
- patch with tests only for seeing ... it fails.
- Added change record draft: https://www.drupal.org/node/3370509
Comment #30
vasikeFailed 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
Comment #32
vasike"Remove" the patch with tests without "fix" ... so it wouldn't change the issue status ... no more
Comment #33
quietone commented@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.
Comment #34
smustgrave commentedChange seems small and addresses the issue.
Am curious why this just appears on file views outside the core one.
Comment #35
joevagyok commentedAdding 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.
Comment #36
idebr commentedOverlaps with #3322402: Add 'Is empty (NULL)' and 'Is not empty (NOT NULL)' operators to boolean field filtering
Comment #37
xjmNice 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?
Comment #38
smustgrave commentedCan 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.
Comment #39
vasike@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.
Comment #40
harlor commentedI've tested https://git.drupalcode.org/project/drupal/-/merge_requests/4060 and it worked very well!
Comment #42
smustgrave commentedCleaned 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
Which shows test coverage.
Comment #43
smustgrave commentedStill green. Again just did a rebase not submit new code so think I’m good to mark
Comment #44
quietone commentedComment #46
longwaveNot eligible for backport as it's a feature request.
Committed a551f2b and pushed to 11.x. Thanks!
Also published the change record.
Comment #49
xjmAmending attribution.