Can you use file_create_url() inside node.tpl.php? Should I assume you can only use it in the template.php?

$img_src = render($content['field_image']);
$bgimage = file_create_url($img_src);

Using the following code, I am only getting public://science-of-rock_background.png from $img_src. Is there any reason why it would be blank?

print $img_src;
print $bgimage;

Edit made to avoid confusion:

In my tpl page for this field I am generating the uri.


<?php foreach ($items as $delta => $item) : ?>
  print $item['#item']['uri']; 
<?php endforeach; ?>

I'd first suggest to dpm($img_src) -- You'll need the devel module for that to work. See what is actually inside that field. It's odd that it would be a file path though.

I'd never put logic like that inside a template file anyway. Use a preprocess function anyway:

template_preprocess_node(&$variables) {


inside template.php (replace template with your theme name)

  • Interesting. Silly question. How would I directly pull the field into this? render($content['field_image']); doesn't work. Would it be the full array? – tdm Apr 24 '14 at 21:33
  • Another question, on using dpm(). Can you 'only' use it in a function in the template.php? – tdm Apr 24 '14 at 21:35
  • (Cliché -- there are no silly questions!) The field values will be inside that $variables array. Use dpm($variables) and hopefully you'll see a yellow box that contains the values of that array. 2. You can use dpm anywhere there is a messages region in your theme. An alternative is kpr. That basically does the same thing but injects the debug text at the top of the page. – kevin.coyle Apr 24 '14 at 23:23
  • Got it. 1 and 2 both worked. Thanks a ton for the help! – tdm Apr 25 '14 at 21:25

file_create_url takes a uri, such as public://science-of-rock_background.png

The revised:

$img_src = $content['field_image']; $bgimage = file_create_url($img_src);

Would work if your field is only returning the URI.

And, for best practices, should set that up in preprocess_node, otherwise will work in node.tpl.php

  • The content of the field is available as $field_image; $content['field_image'] is always a rendering array, which is not what the OP wants. – kiamlaluno Apr 24 '14 at 2:26
  • The file's URI is available within the render array, and the code was shortened based on OP stating: I am only getting public://science-of-rock_background.png from $img_src – Matt Glaman Apr 24 '14 at 2:32
  • Sure, it is available, but you need to access it more directly than with $content['field_image']. It is then an error to use the render array when the field content is already available. – kiamlaluno Apr 24 '14 at 3:47
  • And you are completely correct. Was trying to keep in context of original post - I know this is exactly how i started, and then moved into proper format – Matt Glaman Apr 24 '14 at 3:48

The error is that render() returns HTML output, while file_create_url() is expecting a URI or a file path. In node.tpl.php, the content of a field is available as variable; so, the field you are trying to access is available as $field_image, basing on the following note.

Field variables: for each field instance attached to the node a corresponding variable is defined; for example, $node->body becomes $body. When needing to access a field's raw values, developers/themers are strongly encouraged to use these variables. Otherwise they will have to explicitly specify the desired field language; for example, $node->body['en'], thus overriding any language negotiation rule that was previously applied.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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