I need to print the URL of an image in a content type TPL file. I can do this with the following code:

<?php $file = file_load($node->FIELD_NAME['und'][0]['fid']); ?>
<?php print file_create_url($file->uri); ?>

However if the field is empty then I get errors on the screen. I tried only printing the URL if $file had a value:

<?php if (!empty($file)): ?>
  <?php print file_create_url($file->uri); ?>
<?php endif; ?>

However im still getting the errors if the field is empty. I understand that it is the first line of the code which creates the variable that is causing the issue. How can I rewrite this to not cause errors?

By the way im a themer not a back-end developer so if there is a more 'themer friendly' solution that would be my preference.

2 Answers 2


Found the answer here: http://www.drupalden.co.uk/rendering-drupal-7-fields-custom-node.tpl.php-templates

// field_my_image is the name of the image field

  // using field_get_items() you can get the field values (respecting multilingual setup)
  $field_my_image = field_get_items('node', $node, 'field_my_image');

  // after you have the values, you can get the image URL (you can use foreach here)
  $my_image_url = file_create_url($field_my_image[0]['uri']);

  print $my_image_url;

It sounds like your code is bugging out with $node->FIELD_NAME['und'][0]['fid']; the 'fid' element (and its parent elements) don't exist so it breaks before you even get to file_load().

Here's a themer friendly solution. Save yourself later headache if you just stick with render logic in your node--TYPE.tpl.php template. Place the 'fid' null check in a YOURTHEME_preprocess_node(&$variables) inside your theme's template.php:

function YOURTHEME_preprocess_node(&$variables) {
  $file = isset($variables['node']->FIELD_NAME['und'][0]['fid']) ? file_load($variables['node']->FIELD_NAME['und'][0]['fid']) : null; 
  $variables['file_url'] = !isset($file->uri) ? file_create_url($file->uri) : '';

Replace YOURTHEME with the machine name of your theme and empty your cache. At that point you should have a variable $file_url in your node--TYPE.tpl.php template that will either have the full URL or an empty string.

  • Should it start function YOURTHEME ?
    – Evanss
    Apr 22, 2015 at 10:53
  • This breaks my site. I get a white page with some of my content being shown but no styling.
    – Evanss
    Apr 22, 2015 at 10:58
  • No, you need to replace YOURTHEME with the machine name of your theme.
    – Shawn Conn
    Apr 22, 2015 at 16:47
  • the function definition was missed. Its been fixed.
    – Shawn Conn
    Apr 22, 2015 at 16:54
  • Notice: Trying to get property of non-object in MYTHEME_preprocess_node() (line 179 of /home/drupalpro/websites/MYSITE/docroot/sites/all/themes/MYTHEME/template.php). =>
    – Evanss
    Apr 23, 2015 at 11: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.