2

I don't understand why I put $node->field_test['und'][0]['value'] appears the next error:

Notice: Undefined index: und in eval()

The field "field_test" is a boolean field. The code is the following one:

$node = node_load(153);    
if ($node->field_test['und'][0]['value'] == 1) { 
  echo "test";
}

What is my error?

3 Answers 3

6

You must pay attention to the node's language. 'und' is for nodes which don't have a language set. If you set one, then the values will be in 'en' or 'de' or whatever the node's language code is.

2
  • 5
    +1 usually you'd get it like this: $node->field_test[$node->language][0]['value']
    – Clive
    Commented Feb 24, 2012 at 17:43
  • I tested this option and the error continue.
    – cabita
    Commented Feb 24, 2012 at 20:01
2

Don't assume that 'und' is for fields of entities without language, it is also for fields that are not translatable, and without the entity translation module that is all fields. Moreover, there are differences in this between the various 7.x versions of Drupal.

Better to use the field_get_items() function which will sort out for you under what language code the data is stored.

1

I found the answer.

I had to change the code:

 <?php
    $node = node_load(153);    
    if ($node->field_test[$node->language][0]['value'] == 1) { 
    echo "test";
    }

I replaced it with the following one:

    $node = node_load(153);    
if (isset($node->field_test[$node->language][0]['value']) && $node->field_test[$node->language][0]['value'] == 1) { 
  echo "test";
}

This code solved the error and works for me.

2
  • It can work, but you'll be in trouble at times. This code won't output anything if your node's language is set, hiding information which should be displayed. You can know what's going on by installing the Devel module and doing a dpm($node) to check whay's inside.
    – Countzero
    Commented Feb 25, 2012 at 7:45
  • Ok, I will change the answer. HOwever when I changed by node's language is not enough for solve my problem, the message of error was, then I added isset($node->field_test['und'][0]['value']) && and the message of error dissapeared. This message of error doesn't appear only for the node's language, also was because the variable is not exist, then the function eval can not to evaluate. Thanks very much , I updated my answer and the original code in Drupal.
    – cabita
    Commented Feb 25, 2012 at 12:55

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.