http://drupal.org/node/348204
I found this thread extremely helpful, and I'm almost to where I need to be, but I'm not sure how to work through the further complications that a multi-value file field causes with arrays. Like the poster in that thread, I know just enough to be dangerous, so I'm sure I'm missing something obvious.

I have an imagefield where the user can upload multiple images, and I am trying to get to the filepaths for each of the images. I've tried variations on the code below, and I just seem to be trying to pull elements out of the array(s) in the wrong places. Any help would be greatly appreciated.

I've been trying variations on this theme:

<?php
$galleryImages = $data->node_data_field_kitchen_gallery_field_kitchen_gallery_fid;

foreach ($galleryImages as $galleryImage) {
    $galleryImage = field_file_load($galleryImage);
    echo $galleryImage['filepath'] . '<br />';
}
?>

Comments

oxyc’s picture

Thanks for assisting me,
your post and the link helped me out as well and I figured out how to solve the problem (yes it's a tiny error).

Replace line 5 with:
$galleryImage = field_file_load($galleryImage['fid']);

Hope that works

grasmash’s picture

Nice script. I ran into the need for a very similar script. Here's the issue I was having:

When uploading multiple files with filefield, Views would only honor the "hide if empty" setting if there were absolutely no files attached. When, for instance, four (of the possible eight) files were attached it would display the four empty rows with their labels. I had to add a php script using views customfield that would 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->node_data_field_rfp_exhibits_field_rfp_exhibits_fid 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.

KerriO’s picture

Thanks, you two, for your comments. The structure ended up changing for this so I didn't need to do it through views in the end, but these comments are very helpful, as I'm sure something similar will pop up in the future! I see now that the "fid" was definitely what I was missing!

KerriO’s picture

Status: Active » Closed (fixed)
ginga8’s picture

I am wondering if someone here maybe able to help me, I too am trying to do the same thing as above but with files and it looks like exactly what I am looking for. Here is what I have so far:

$tenders= $data->node_data_field_type_field_file_fid;

foreach ($tenders as $tender) {
    
$tender = field_file_load($tender['fid']);
    echo $tender['filepath'] . '<br />';
}

warning: Invalid argument supplied for foreach() in /home/swdev2/public_html/sites/all/modules/views_customfield/includes/views_customfield_handler_field_phpcode.inc(118) : eval()'d code on line 4.

In the SQL returned by the view, I see no mention of the field_file, am I to structure my view a certain way. Not every Tender Content type will have files and if they do they can have one to many.

Any help would be greatly appreciated.

Thanks

ginga8’s picture

Status: Closed (fixed) » Active

Meant to change status to active

guysaban’s picture

Hi GingaB,

Not sure I exactly understand what you need. I may be wrong but I understand the fid is a unique File ID - so one fid per file.

In your code you have a foreach loop. I would leave that up to the Views query which will output multiple rows (each row with the fields you specified for the query (i.e. in the Views interface) - in this case the fid field.)

Based on this I would change your views_customfield as follows:

<?php

$file_id= $data->node_data_field_type_field_file_fid;

$tender = field_file_load($file_id);
    echo $tender['filepath'];

?>
leovw’s picture

Thanks for this - It was just what I was looking for (#2)

grasmash’s picture

Glad I could help! I still use that snippet on many sites : )

Abeaudrian’s picture

Hi

How are you? Will this script allow me to still get Lightbox2 functionality for my images?