By big_smile on
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
I'm getting the same error
I'm getting the same error message simply using this:
In node--page.tpl.php
Using Drupal 7
I've traced this to the node
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!
Hey
Hey did you solve this problem? How do you print a field on page.tpl.php in Drupal 7?
FIX for "Undefined index: und" error
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:
Here's the code that DOES work:
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.
Similar problem
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)
(template.php)
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 ????
Thank you!
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.)
Hi guys! Lots of helpful
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 ;)
Help! Won't a drupal Jedi master come to my rescue :)
removed
removed
Check to see if $node is
Check to see if $node is available and also check to see if the field is available to circumvent the error message.
example:
Thanks!
Your code
<?php if(isset($node) && $node->field_callout_text):?>got rid of some errors for me. Thank you.____________________
https://jay.lee.bio
Notice: Undefined index: und in include()
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).
Done....
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.
Code Snipped That Worked For Me (with explanation)
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
}