I have a content type with an image field; the number of files is unlimited. I need to extract, filter using regex, and display the filename for each image in the particular node type using a view.

I tried with field--field-images--content-type.tpl.php and every other overwriting theme hook's trick and nothing even started working.

I think I miss something important on the way.


2 Answers 2


Checkout this using hook_preprocess_node()

function MYTHEME_preprocess_node(&$vars) {
  $node = $vars['node'];

  if ($node->type == 'the_type' && $vars['view_mode'] == 'full') {
    $field_items = field_get_items('node', $node, 'field_name');

    if (!empty($field_items)) {
      $filenames = array();
      foreach ($field_items as $item) {
        $filenames[] = $item['filename'];

      $vars['filenames'] = theme('item_list', array('items' => $filenames));

If you're using views to output the display of this node and want to modify it using a complex regex, your best option is to do this is to add your logic in a views template file. However, views doesn't have the filename of the file as a field option but you can load the managed file from the file id (fid) value, ex, following this example:

$file = file_load($node->field_YOUR_IMAGE_FIELD['und'][0]['fid']);

// Filename
print $file->filename;

// URI e.g. public://image.jpg
print $file->uri;

// URL e.g. /sites/default/files/image.jpg
print file_create_url($file->uri);
  • OK it's closer but it's look a like i don't have $node defined in views-view-fields--[name].tpl.php
    – maryl
    Aug 5, 2013 at 19:55
  • right - that's from the older example referenced. you need to use $fields instead, ex. $fields["your_file_field"]->fid if you're doing this inside a views fields template.
    – schnippy
    Aug 5, 2013 at 20:17
  • Unfortunately view-view-field.tpl.php doesn't have such method available
    – maryl
    Aug 5, 2013 at 20:29
  • thats not the full and complete set of attributes available, especially for complex fields like file management fields. In your view, add or edit your file field and click on 'rewrite results' than 'replacement patterns' and you'll see the full range of attributes available for fields of type file field, including "[field_image-fid] == Raw fid". You can find the same if you use devel and dsm($your field name).
    – schnippy
    Aug 5, 2013 at 20:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.