I'm trying to do a large import that's more complex than feeds can play nicely with, so I'm in the process of writing a custom import script. Everything works except loading images that are associated through a field collection. I have a field collection that contains node and taxonomy references just fine, but I've had no luck on the image field.
I have a field on the node called field_signature_media which references a field collection that can be a variety of media types. In the field collection I have field_signature_image for handling the images.
Attempt One: using entity_metadata_wrapper:
// Image file created in earlier part of the process and loaded into $file
// The node that's being worked on has been created in $node
// Create the field collection
$fc_item = entity_create('field_collection_item', array('field_name' => 'field_signature_media'));
$fc_item->setHostEntity('node', $node);
$fc_wrapper = entity_metadata_wrapper('field_collection_item', $fc_item);
$field = array('alt' => '',
'title' => '',
'fid' => $file->fid,
'display' => 1,
'width' => $file->image_dimensions['width'],
'height' => $file->image_dimensions['height'],
'image_field_caption' => array
(
'value' => '',
'format' => 'full_html',
),
);
$fc_wrapper->field_signature_image->set($field);
$fc_wrapper->save(true); // <-- at this point it enters infinite recursion.
field_attach_update('node', $node);
Attempt Two without the wrapper:
$fc_item = entity_create('field_collection_item', array('field_name' => 'field_signature_media'));
$fc_item->field_signature_image[LANGUAGE_NONE][] =
array('alt' => '',
'title' => '',
'fid' => $file->fid,
'display' => 1,
'width' => $file->image_dimensions['width'],
'height' => $file->image_dimensions['height'],
'image_field_caption' => array
(
'value' => '',
'format' => 'full_html',
),
);
$fc_item->setHostEntity('node', $node);
$fc_item->save(TRUE);
field_attach_update('node', $node); // <-- At this point field_collection cannot find an array it was expecting.
Attempt Three with wrapper and not creating field data by hand:
$fc_item = entity_create('field_collection_item', array('field_name' => 'field_signature_media'));
$fc_item->setHostEntity('node', $node);
$fc_wrapper = entity_metadata_wrapper('field_collection_item', $fc_item);
$fc_wrapper->field_signature_image->set($file->fid);
$fc_wrapper->save(true); // <-- at this point it enters infinite recursion.
field_attach_update('node', $node);