I am trying to print out an image field to my node.tpl.php and page.tpl.php files, but I keep on getting this error message:

    * Notice: Undefined index: und in include() (line 6 of /home/dgtgr0/public_html/aarc/sites/all/themes/aarctheme/templates/node--core-page.tpl.php).
    * Notice: Undefined index: und in include() (line 12 of /home/dgtgr0/public_html/aarc/sites/all/themes/aarctheme/templates/node--core-page.tpl.php).

The error message only appears randomly and doesn't appear every time. Although there is an error, nothing is broken and everything works fine.

What can I do to stop the error message from appear?

Here is a link to my website with the error message: Link

Here is the code I am using to print out the image field :
(Code in page.tpl.php file)

<?php if (isset ($node->field_large_page_image['und'][0]['filename'])): ?>
<?php global $base_url; echo '<img alt="brochure image" id="page_image" src="'.$base_url.'/sites/default/files/large_page_image/'.$node->field_large_page_image['und'][0]['filename'].'" />'; ?>
<?php endif; ?>

(Code in node--custom.tpl.php file)

  <?php if ($node->field_image['und'][0]['uri']): ?>
    <img src="<?php print image_style_url('body_images', $node->field_image['und'][0]['uri']); ?>" 
    <?php if ($node->field_image['und'][0]['alt']): ?> alt="<?php print $node->field_image['und'][0]['alt']; ?>" <?php endif; ?>
    />
    <?php endif; ?>

Thanks for any help.

Comments

hedley’s picture

I'm getting the same error message simply using this:

hide($content['field_header_image'])

In node--page.tpl.php

Using Drupal 7

hedley’s picture

I've traced this to the node not having an image uploaded in the image field I was attempting to hide. The image was mysteriously deleted which must be another bug, so watch out!

rcastera’s picture

Hey did you solve this problem? How do you print a field on page.tpl.php in Drupal 7?

kccmcck’s picture

I also encountered this problem when trying to print an image field via a custom node template. Some nodes had an image uploaded to them and others did not. The error occurred because I was writing code in such a way that automatically assumed every node had an image, when in fact, they didn't. Below is the original, faulty code and the adjustments that fixed it.

Here's the code that DIDN'T work:

<?php 
	$image = $node->field_image['und'][0]['filename'];
	if (isset($image)) {
		print '<img src="/sites/default/files/'.$image.'" />';
	}
?>

Here's the code that DOES work:

<?php 
	$image = $node->field_image;
	if (isset($image['und'][0]['filename'])) {
		print '<img src="/sites/default/files/'.$image['und'][0]['filename'].'" />';
	}
?>

The difference is that in the second example, I didn't set my original $image variable as specifically. This fixed the problem because for nodes that didn't have an image uploaded, the 'und' string wasn't available, hence the "undefined index" error. I recommend the Devel module for debugging. Hope this helps.

LeoPx’s picture

I also have a similar problem ! In the file "node.tpl.php", I have a value that exists or not, so I have a function in "template.php" which tests if it exists. Like that :

(node.tpl.php)

<?php 
print is($node->field_name[$node->language][0]['value'],'<strong>','</strong>'); 
?>

(template.php)

<?php 
function is($val,$prefix='',$suffix=''){
	if(!empty($val)) return $prefix.$val.$suffix;
	else return '';
}
?>

But I have this Notice :
Notice : Undefined index: und in include()..........

If I take the "isset" test in the node.tpl.php, I don't have Notice.
Any ideas ????

balancestuff’s picture

This was extremely helpful. My code sucks significantly less now thanks to you!

(isset() would be a great candidate for an introductory article with API references most useful to beginners.)

asinnema’s picture

Hi guys!

Lots of helpful comments here, I hope you guys don't mind me bumping up this old thread as this is the right topic.

I was really proud I got this code together even though I'm really new to PHP, but I'm stumped on how to get the isset check in here. I recognize the check needs to be in here to check if the field is filled and should be displayed, and it's obvious the und-error I got was because the suffix field in the code below wasn't filled. So I think I understand the why, just not the how ;)

     <?php
      global $user;
      $user_fields = user_load($user->uid);

      $userprefix = $user_fields->field_title_prefix['und']['0']['value'];
      $usersuffix = $user_fields->field_title_suffix['und']['0']['value'];

      print t("Submitted by " . $userprefix . ' !username ' . $usersuffix . " on !datetime",
      array('!username' => $author, '!datetime' => '<time datetime="' . $datetime . '">' . $created . '</time>'));
    ?>

Help! Won't a drupal Jedi master come to my rescue :)

azabeti’s picture

removed

azabeti’s picture

Check to see if $node is available and also check to see if the field is available to circumvent the error message.
example:

<?php if(isset($node) && $node->field_callout_text):?>	
  <div class="calloutText"><?php print $node->field_callout_text['und'][0]['value']; ?></div>
<?php endif; ?>
jay.lee.bio’s picture

Your code <?php if(isset($node) && $node->field_callout_text):?> got rid of some errors for me. Thank you.

____________________

https://jay.lee.bio

adepentane’s picture

Good day Guys, please am getting this error on my drupal site. Please kindly assist. i have read through all the post here and a newbie in php. I don't want to break everything forever. Thanks.
Below is the code block having this error. While this is the error line message....
Notice: Undefined index: und in include() (line 202 of \sites\all\themes\myTheme\templates\node\product\node--view--product-block--block-all.tpl.php).

if($node->body['und'][0]['summary'] != Null){
                    $summary = $node->body['und'][0]['summary']; 
                }
adepentane’s picture

I only commented out the error codes and I errors disappeared. I know this is not a good practice but still searching for the effect of that. thanks all for the assistace.

graham leach’s picture

Hello,

The error results from performing a logical test on an attribute of an object that "doesn't exist" according to Drupal/PHP

In Drupal, object definitions are not regular, despite what their Content Type definition states.

In English:  If you add a custom field to a Content Type, but don't populate that field in ALL INSTANCES of that Content Type, that custom field may not be available later, when instances of that object are being processed in code.  So, if you have even ONE instance of an unpopulated Content Type custom field, that "undefined index" error will occur.  This may result in sporadic error messages and confusion, especially when you believe that by defining that custom field for ALL instances of that Content Type you created a "stub" in the resulting objects that simply reads NULL.  

NOPE!  DEFENSIVE Drupal CODING IS ADVISED AT ALL TIMES

    

        # GL  2022-07-03
        #
        #     Problem:      I am seeing an "undefined index" error with
        #                        respect to the field_youtube custom attribute?
        #
        #     Answer:       This is because I was using is_null() instead of
        #                        issset().  the is_null() function assumes that
        #                        the variable exists, but is blank.  The isset()
        #                        function tests to see if the variable even
        #                        exists.
        #
        #                   These tests should be cascaded...
        #
        if (isset($content['field_youtube'])) {

          if (is_null($content['field_youtube'])) {

            print "Coming Soon!";

          }
          else {

            echo "<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/watch?v=YsogHeNf998\" title=\"YouTube video player\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>\n";

          }
        }
        else {

        # Field not set - do nothing

        }