I created a custom image field for a banner in one of my content type. I wish to display this banner in the page.tpl.php instead of node template. Here's my code in template.php:

function mytheme_preprocess_page(&$vars, $hook) {

     $vars['banner'] = ''; 
    if (!empty($vars['node'])) {
        $node = $vars['node'];

        if (isset($node->field_banner)) {
                // Render the image as a variable in the page template
           $vars['banner'] = theme_image_style(array('path' => $node->field_banner['und']['0']['uri'], 'style_name' => 'mybannerstyle', 'height' => '200', 'width' => '700'));


and in the page.tpl.php, i coded:

    <?php if(!empty($banner)){print $banner;} ?> 

. This work perfectly in the node that has banner uploaded. But for nodes which have no banner, the following undefined messaged turn up:

Notice: Undefined index: und in mytheme_preprocess_page() (line 33 of /home/xxxxx/themes/mytheme/template.php).

I wonder how to solve this. Please advise. Thanks.

3 Answers 3


I think you should use $node->language over using 'und' when accessing your array. Second, to remove the warning you could simply do:

if (isset($node->field_banner, $node->field_banner[$node->language])) {
  $vars['banner'] = theme_image_style(array(
     'path' => $node->field_banner[$node->language]['0']['uri'], 
     'style_name' => 'mybannerstyle', 
     'height' => '200', 
     'width' => '700'

To access all images in your field you would do something like this:

foreach ($node->field_banner[$node->language] as $key => $value):
  $vars['banner']['images'][] = array(
     'path' => $node->field_banner[$node->language][$key]['uri'],
  • How come I didn't see "language" in Devel, but rather "und"?
    – Adamtan
    Nov 24, 2012 at 14:07
  • No idea really=) Nov 24, 2012 at 14:09
  • Using "$node->field_banner[$node->language]['0']['uri']" will only print 1 and the first image uploaded. What if my field is for multiple images upload. How can print them all.
    – Adamtan
    Nov 24, 2012 at 14:35
  • 1
    I guess you will have to change the index to access the rest of your images like so: $node->field_banner[$node->language][1]['uri']. You can always try checking what data the actual field contains using print_r($node->field_banner[$node->language]); or similar. Nov 24, 2012 at 14:40

For a field, $entity->$field_name['und'] is not always defined. Instead of the code you are using, you should use the following code.

if (isset($node->field_banner)) {
  // Render the image as a variable in the page template.
  $field = field_get_items('node', $node, 'field_banner');
  $vars['banner'] = theme_image_style(array('path' => $field[0]['uri'], 'style_name' => 'mybannerstyle', 'height' => '200', 'width' => '700'));

I would also consider using field_view_value(), instead of fields_get_items(), but the first function doesn't allow to customize the output, if not through the $display parameter. (See field_view_field() for a description of that parameter.)

field_get_items() and field_view_value() get the language ID from field_get_name(), which returns the correct value for the language to use for the field.

  • This question/answer would be a great candidate for the Canonical questions we were talking about the other day
    – Clive
    Nov 23, 2012 at 9:12
  • I think there should be a canonical question about accessing the field value, but it's better to create one ex-novo, to avoid wrong answers. (See the most recently added one.) If we agree on that, I can create it (and make it CW).
    – apaderno
    Nov 23, 2012 at 9:21
  • Sounds like a great idea, is it worth adding it to the question (or an answer) on the meta question to get feedback from others?
    – Clive
    Nov 23, 2012 at 9:34
  • I am sorry, I forgot about this. A meta question asking about a list of possible canonical questions would be good. We could explain what canonical questions are, what their purpose is, and ask the users to list what they think would be canonical questions (with links to existing questions). We should explain a canonical question is for a question that keeps being asked in different forms, such as the "How do I debug hooks?" which is the underlaying question on different questions where the users wonder why their hook don't output anything.
    – apaderno
    Dec 14, 2012 at 3:07

Hi Just change the line

'path' => $node->field_banner[$node->language]['0']['uri'], 


'path' => $node->field_banner[LANGUAGE_NONE]['0']['uri'], 

$node->language = LANGUAGE_NONE; is default in D7.

never use 'und' directly use LANGUAGE_NONE constant at place of und.

  • The OP is not using $node->language. Then, even using LANGUAGE_NONE, the OP would still get the message error he is getting.
    – apaderno
    Nov 23, 2012 at 9:22
  • yes you have updated your question so you need to change $node->field_banner['und']['0']['uri'] to $node->field_banner[LANGUAGE_NONE]['0']['uri']. Nov 23, 2012 at 9:26
  • 2
    Uhmmm... No, the question has not been updated. (I didn't ask the question.) $node->field_banner[LANGUAGE_NONE]['0']['uri'] would get the same error message as $node->field_banner['und']['0']['uri']: "Undefined index: und in mytheme_preprocess_page()." The error is saying that $node->field_banner doesn't contain $node->field_banner[LANGUAGE_NONE], nor $node->field_banner['und'].
    – apaderno
    Nov 23, 2012 at 9:30
  • 2
    I think you've missed @kiamlaluno's point ever so slightly there...LANGUAGE_NONE == 'und', so it doesn't matter which one you use...the $node->field_banner array in this context doesn't have an index with that key, so it doesn't matter if you use the constant value or the literal value, the result will be the same
    – Clive
    Nov 23, 2012 at 9:33

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.