I know that back in the old (5.x) days, FileField (or at least ImageField) used to have a Views filter that let you filter on whether the node has a file or not. I even used it, worked fine.

But it doesn't look like this is working now, using Views 6.x-2.2 and FileField 6.x-3.0-alpha7. I can see the code in the FileField module (filefield_field_settings_views_data in filefield_field.inc) that has comments that look like it is trying to tell Views to have a filter on whether the file is there or not, but it doesn't appear to work.

Or is it just me? I'm just not seeing it come up in the available list of Filters inside the Views UI.

CommentFileSizeAuthor
#11 372241-11-filter.diff587 bytesgeerlingguy
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jhodgdon’s picture

I was able to get the Views filter to work by commenting out line 63 of filefield_field.inc in version 6.x-3.0-alpha7. It's this line in the filefield_field_settings_views_data() function:

unset($data[$table_alias][$field['field_name'] .'_fid']['filter']);

The comments indicate this is unnecessary. I think it's necessary. The relationship stuff below referred to in the comments seems to pertain to making a file-based view, rather than a node-based view. But the point is to have the "has a file" filter available in node views.

jhodgdon’s picture

Although, it would be better to have a filter handler that just looks for empty or not empty. The default one coming from the CCK module is letting you choose integer values. Sigh.

zeropaper’s picture

Commenting unset($data[$table_alias][$field['field_name'] .'_fid']['filter']); helped here too.

quicksketch’s picture

Category: bug » support

You can imitate a "has file" filter by adding a relationship (yes, on "node" type views).

Just click the + (plus) icon in the Relationships section, add a relationship for "Content: [Label of field] (field_[name_of_field])", then check "Require this relationship".

Doing an inner join like this is much more effective than doing a left join and then filtering out the NULL data, there's no point in selecting it in the JOIN if we're just going to filter it out.

quicksketch’s picture

Title: FileField no longer has Views filter? » Make a "has file" Views Filter
quicksketch’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

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

frankcarey’s picture

a note: this worked for me, and with the "delta" setting on the relationship, I could pull the just the first image. if you don't choose a delta (which file) you will get "duplicate" rows for each different file. I got this to work on a content_profile node , with images attached to it, but i couldn't get it to work by using users as the base table, then a relationship to node, then a relationship to the files.

garethsprice’s picture

I needed it to display all nodes with one or more files attached, and hide nodes with 0 files attached.

By adding a required relationship to the filefield and setting the delta to "1", it removes duplicates but as the Filefield field display is set to show all files attached to the node, all files are still visible.

geerlingguy’s picture

Version: 6.x-3.0-alpha7 » 6.x-3.x-dev
Status: Closed (fixed) » Active

Reopening this issue... I have been trying to find a way to find if a file is attached, not to display only nodes with files, but to display only nodes without files. It seems, based on an hour of head scratching, that this is impossible using a relationship.

If there were a simple filter that allowed me to specify, "Does this field have a file attached?", then this would be an easy thing to do. I would just say 'No' for that filter, and get a list of all filefields without files attached.

geerlingguy’s picture

Status: Active » Needs review
FileSize
587 bytes

The attached patch fixes this problem by removing the line that takes away a views fid filter.

I really think this patch should be applied, as it allows people who want to filter out nodes without file attachments to do so. For those looking for better performance when filtering to show nodes with files, the recommended route would be to add a relationship.

But, seeing as there's no way to 'require this relationship not be required' or something like that, this patch is essential to a few of my views.

quicksketch’s picture

Status: Needs review » Closed (duplicate)

Let's consolidate with the identical request at #480508: Create a "has file" filter/sort (then expose it to the user). The patch here is not a sufficient to be included in the project.