0

I'm seemingly unable to check for the existence of a value of a field if the field in question is set to filtered text. The following code returns an error - and any possible combination I've tried to find online results in the same:

if ($wrapper->__isset('body')) {
  print $wrapper->body->value->value();
}

The error:

EntityMetadataWrapperException: Unable to get the data property value as the parent data structure is not set. in EntityStructureWrapper->getPropertyValue() (line 438 of /Applications/MAMP/htdocs/arch-main/httpdocs/sites/all/modules/contrib/entity/includes/entity.wrapper.inc).

To be clear, the node does have the body field, but no content has been placed in it.

Any ideas on how to check for this?

EDIT:

Ok this seems to work, but there must be a better way

try {
          print $wrap->body->value->value();
        } catch (EntityMetadataWrapperException $e) {
          // ???
        }
0

but there must be a better way

If you find it, do share :)

Seriously though, this is what the language gives you; exception handling is what you should use to handle exceptional circumstances, and an entity not having a field that you're expecting it to have would probably class as exceptional in most people's book. It does in mine anyway.

| improve this answer | |
  • The point is, though, that the entity does have the field - just not the value. With any other type of field this would just result in nothing being output, but in the case of filtered text it results in a fatal error. It seems a bit extreme that we have to wrap every filtered text instance in a try/catch instead of the function doing it for us. And shouldn't the __isset function supposed to cover this anyway? – Chris Jun 6 '16 at 1:43

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.