Get the image path of a CCK file upload field in Drupal
- 8th March 2011
I often like to use template files to alter the HTML of a view to cut down on the masses of redundant markup that Drupal creates. But I just came across a problem where I was unable to get the file path of an image in my content type, despite the fact that the image was output in the view settings.
var_dump($row) gave the following:
["nid"]=> string(2) "27" ["node_data_field_contenttype_teaser_field_imagefield_fid"]=> string(1) "9" // The field names will change depending on the name of your content type and CCK image field
along with some other fields which provided no other useful information.
I stumbled across the function field_file_load which solved all my problems.
$image = field_file_load($row->node_data_field_contenttype_teaser_field_imagefield_fid);
You can then use $image[‘filepath’] to create an image element.
I also discovered that the function has been renamed in Drupal 7 - use file_load() instead.