0

I'm receiving the following error message in my log

Notice: Undefined index: und in include()(line 56 of /sites/all/themes/mysite/node--recipe.tpl.php).

Here is the line off code

$node->field_recipe_skill_audio[$node->language][0]['filename']

This error message is well documented but i've been unable to get the fixes working. I have try the following request.

$node->field_recipe_skill_audio['en'][0]['filename']
$node->field_recipe_skill_audio['und'][0]['filename']
1

You can avoid the notice error by checking if that field has data, e.g

if (isset($node->field_recipe_skill_audio[$node->language][0]['filename'])) {
  /* Do stuff */
}

or

if (isset($node->field_recipe_skill_audio[LANGUAGE_NONE][0]['filename'])) {
  /* Do stuff */
}
  • You obviously know your Drupal, but you've got to stop using that method :P The proper API function to use is field_get_items(), it's language safe and future-proof. e.g. $field_items = field_get_items('node', $node, 'field_recipe_skill_audio'); if ($field_items) { // ... – Clive Jan 14 '13 at 10:57
  • @Clive That IS the answer. If you add it, I will up-vote it. :) – kiamlaluno Jan 14 '13 at 12:55
  • 1
    @Clive, thanks. I was wanting to show how isset() or !empty() can avoid undefined index errors on arrays, in the context of OPs code. However, field_get_items or entity_metadata_wrapper, is indeed a better way to get the actual field items, particularly when dealing with multi-language content. Will use that next time! – David Thomas Jan 14 '13 at 20:41

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.