Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
In Views, it’s currently not possible to filter File Usage fields (module, type, id, and count) by either ‘Is empty (NULL)’ or ‘Is not empty (NOT NULL)’.
Proposed resolution
Add ‘allow empty’ => TRUE to these definitions for Views.
For example:
$data['file_usage']['module'] = [
'title' => $this->t('Module'),
'help' => $this->t('The module managing this file relationship.'),
'field' => [
'id' => 'standard',
],
'filter' => [
'id' => 'string',
+ 'allow empty' => TRUE,
],
'argument' => [
'id' => 'string',
],
'sort' => [
'id' => 'standard',
],
];
Remaining tasks
Submit patch- Do we need to update some tests for this?
User interface changes
None
API changes
None
Data model changes
None
Comment | File | Size | Author |
---|---|---|---|
#2 | 2886903-2.patch | 1.01 KB | Chris Burge |
Comments
Comment #2
Chris Burge CreditAttribution: Chris Burge commentedPatch attached.
Comment #4
yang_yi_cn CreditAttribution: yang_yi_cn commentedyeah I need this patch because I'm creating a view to list all the Files that has zero usage count.
It's going to be a quite common use case as the new direction in Change record: https://www.drupal.org/node/2891902s is: "Files that have no remaining usages are no longer deleted by default".
However, when you do the file view with filter "Use count" = "0", the actual sql query is something like
As it's a LEFT JOIN, the count in result is probably NULL and you won't get the result you want. So this patch is necessary.
I'm not familiar with how to write a test for this though.
Comment #7
Chris Burge CreditAttribution: Chris Burge commentedComment #8
Chris Burge CreditAttribution: Chris Burge commentedTests still pass on 8.8.x.
Comment #16
smustgrave CreditAttribution: smustgrave at Mobomo commentedThis issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.
Tried replicating in D10 with a standard install
Uploaded 2 images, 1 attached to a node the other not.
Edited the files view to filter by file usage with setting not null
I see 1 file that's attached to node
Edit the files view to filter by file usage with setting null
I see 1 files that's not attached to node
This appears to work could someone else verify.
If still a valid feature request it will need a test case to show the issue.