I ran into an issue with views not hiding empty rows in a filefield with multiple attachments. When uploading multiple files with filefield, Views will only honor the "hide if empty" setting if there are absolutely no files attached. When, for instance, four (of the possible eight) files are attached to a given filefield, views will display the four empty rows with their labels.

It would be nice if this problem were fixed. In the meantime, I've come up with a views workaround script using views customfield that will cycle through all of the attachments and print a custom output for each not-null row.

This script mimics the normal generic file output setting for a filefield row (icon, title linked to file, filesize):

<?php

//print var_export($data, TRUE); //used to find all available variables in views $data array

foreach ($data->[add relevant filefield variable] as $key => $value){

    if ($value['fid'] != NULL){
      //print_r($file); //used to find all available variables in filefield array
      $file = field_file_load($value['fid']);
      $filepath = base_path().$file['filepath'];
      $filename = $file['filename'];
      $icon = theme('filefield_icon', $file);
      $filesize = format_size($file['filesize']);     
      $link = "<a href='$filepath'>$filename</a>";
       
      echo "<div class='file-attachment'>".$icon." ".$link." ".$filesize."</div>";
      
    } 
}

?>

Hope that helps someone. There was very little info online about hiding select rows of a filefield multiple attachment field in views.

Comments

quicksketch’s picture

Unfortunately this functionality is provided by CCK, not FileField. The same thing will happen for any field type (such as text or integers), because of CCK's Views integration. If you want the rows to be pulled in individually, you can uncheck the "Group multiple values" checkbox and the empty fields should work properly, but unfortunately each file will end up in an entirely different row and you'll get duplicate node titles.

quicksketch’s picture

Category: bug » support
Status: Active » Closed (fixed)
Abeaudrian’s picture

Hi,

Thanks for posting this snippet of code. I would like to utilise it to solve a similar problem.

I have a content type called 'company profile' and another content type called 'current promotions' which both share a file field called "comp_photo"

I want to output the company photo field which has multiple images attached to this field, in a view of style 'grid'. I am currently getting a few blank fields in between the out put when the query is executed. This output from the query is returned based on a an argument which is based on a 'user id from the url' which evaluates based on the node author.

I am using an image cache preset called 'recent_items_photos' to resize the images returned from the query. My challenge is that I do not know how to plug these requirements into the views custom field to get the necessary results.

Can you please assist me?

The out put from the current query in view is:

SELECT node.nid AS nid, node_data_field_comp_photo.field_comp_photo_fid AS node_data_field_comp_photo_field_comp_photo_fid, node_data_field_comp_photo.field_comp_photo_list AS node_data_field_comp_photo_field_comp_photo_list, node_data_field_comp_photo.field_comp_photo_data AS node_data_field_comp_photo_field_comp_photo_data, node_data_field_comp_photo.delta AS node_data_field_comp_photo_delta, node.type AS node_type, node.vid AS node_vid FROM node node INNER JOIN users users ON node.uid = users.uid LEFT JOIN content_field_comp_photo node_data_field_comp_photo ON node.vid = node_data_field_comp_photo.vid WHERE (node.type in ('company_profile')) AND (node.status <> 0) AND (users.uid = 119)

Please note that the user id will not be always 119. However this will be determined based on who the node author of the node being viewed is.

Regards,

Adrian Millington