How do I iterate over values of a multivalued field with entity_metadata_wrapper? For instance, I have a taxonomy_term_reference field with cardinality -1. If one or more terms are selected, I can use this code:

  foreach ($wrapper-&gt;field_myterms as $t) {
    $terms[] = $t-&gt;name-&gt;value();
  }


But if the field has zero values selected, this code throws:

EntityMetadataWrapperException: Unable to get the data property name as the parent data structure is not set. in EntityStructureWrapper-&gt;getPropertyValue() (line 442 of sites/all/modules/entity/includes/entity.wrapper.inc).


This happens in a preview form step while creating a custom entity. The entity has not yet been saved but I am showing a preview of the properties and field data by creating a temporary entity with this code:

$entity = entity_get_controller('ams_survey')-&gt;create($form_state['values']);
$wrapper = entity_metadata_wrapper('ams_survey', $entity);
The problem is in this file: /sites/all/modules/entity/includes/entity.wrapper.inc

Whenever a field_foobar->value() is called, and the field isn't set, then the class throws an Exception.  That is just silly, because having an empty field isn't an error.

Inside that field, when checking to see if a field is empty, it uses this code:

$data = $this-&gt;dataAvailable() ? $this-&gt;value() : NULL;


But using "if ($wrapper->field->foobar->dataAvailable())" before each echo of the value is aggravating.  Sigh, why can't things in Drupal ever be simple?