EntityMetadataWrapperException: unknown data property for field

drupal, drupal-7, php

Solution

Yep, you can just use existing features of the PHP language

try {
  print($wrapper->field_property_sample->value());
}
catch (EntityMetadataWrapperException $e) {
  // Recover
}

Or, since `EntityMetadataWrapper` implements `__isset()` you can use that:

print isset($wrapper->field_property_sample) ? $wrapper->field_property_sample->value() : '';

Problem

I have recently been trying to update my code to use entity wrappers to access field values. Now I have this: ``` $wrapper = entity_metadata_wrapper("node", $nid); print($wrapper->field_property_sample()->value()); ``` instead of this: ``` print($node->field_property_sample[LANGUAGE_NONE][0]["value"]); ``` The problem is sometimes I encounter this: EntityMetadataWrapperException: unknown data property field_property_sample. Is there a way for me to workaround this? I have about 10 of these fields that can throw this exception and it is really getting ugly ``` $wrapper = entity_metadata_wrapper("node", $nid); try { print($wrapper->field_property_sample()->value()); } catch (EntityMetadataWrapperException &e){ print(""); } /** repeat 10 times **/ ``` Is there some function that I can more or less call like this? ``` $wrapper = entity_metadata_wrapper("node", $nid); print($wrapper->field_property_sample->exists() ? $wrapper->field_property_sample->value() : "" ); /** repeat 10 times **/ ```

Original source