How can I save multiple values in a multi-value field collection using EntityMetadataWrapper ?
Here's some of my code
$field_collection = entity_create('field_collection_item', array('field_name' => 'MY_FIELD_COLLECTION'));
$field_collection->setHostEntity('commerce_line_item', $line_item);
$wrapper = entity_metadata_wrapper('field_collection_item', $field_collection);
for($delta=0; $delta<$max_delta; $delta++) {
//MISSING something like $wrapper->setDelta($delta);
$wrapper->field_example->set($new_value);
$wrapper->other_field_example->set($new_value);
$wrapper->save();
}
$field_collection->save();
I'm fighting with the field delta : I can't use $wrapper->field_example[$delta]->set($new_value)
because the wrapper itself is the multi-value field, not its children fields.
So I need to find a way to perform something like
$wrapper[$delta]->field_example->set($new_value);
But this returns "Fatal error: Cannot use object of type EntityDrupalWrapper as array".
Summary : where can I set the delta of the wrapper in the loop ?
setHostEntity()
, anyway). That function just adds to the existing data, it doesn't offer a way to set the delta. The simplest way would be to reproduce the parts of that function you need and set the delta manually in your own function