1

I have created a custom profile field called "field_protection". I am using the following code to reference that field but am getting error:

Notice: Undefined index: und

$is_protected = 0;
   $is_protected  = (int)$act->field_protection['und']['0']['value'];
    if ($is_protected == 1) {
      form_set_error('from', t("Protected !!!"));
      return;
    }
  • Seems like $act->field_protection does not have a 'und' key. Also, the constant 'LANGUAGE_NONE' can be used instead of 'und'. Use the devel module to call a dpm() on $act->field_protection to see it's value. – 2pha Oct 11 '14 at 8:32
2

I like to use EntityAPI module for working with fields. You don't need to worry about some key exists or not. here is an example:

$entityWrapper = entity_metadata_wrapper('ENTITY_TYPE (eg. node, user..)', ENTITY_KEY_OR_ENTITY);

$protectionFieldValue = $entityWrapper->field_protection->value();

if (null != $protectionFieldValue /* put your condition */) {
  form_set_error('from', t("Protected !!!"));
  return;
}

or here is a way without an EntityAPI:

if (isset($act->field_protection['und']) && ($is_protected  = (int)$act->field_protection['und']['0']['value'])) {
    if ($is_protected == 1) {
      form_set_error('from', t("Protected !!!"));
      return;
    }
} 

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.