Drupal - How do I iterate over values of a multivalued field with entity_metadata_wrapper?
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->dataAvailable() ? $this->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?
I can't reproduce this behaviour having used the following simple test:
- Add a new article with no Tag terms, and note the nid (
field_tags
is a taxonomy term reference field with a cardinality of -1). Run the following code:
$node = node_load($nid); $wrapper = entity_metadata_wrapper('node', $node); foreach ($wrapper->field_tags as $item) { dpm($item); }
No debug messages are printed (as you'd expect), but I get no errors either.
The only thing I can think that might be causing the problem is that the entity in question was created before you added the field_myterms
field. In that case there would be no entry in the field table for that entity, and it might cause some strange behaviour.