How do you populate an image field in a field collection programmatically? 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' =&gt; 'field_signature_media'));
    $fc_item-&gt;setHostEntity('node', $node);
    $fc_wrapper = entity_metadata_wrapper('field_collection_item', $fc_item);

    $field = array('alt' =&gt; '',
          'title' =&gt; '',
          'fid' =&gt; $file-&gt;fid,
          'display' =&gt; 1,
          'width' =&gt; $file-&gt;image_dimensions['width'],
          'height' =&gt; $file-&gt;image_dimensions['height'],
          'image_field_caption' =&gt; array
              (
                  'value' =&gt; '',
                  'format' =&gt; 'full_html',
              ),              
    );

    $fc_wrapper-&gt;field_signature_image-&gt;set($field);
    $fc_wrapper-&gt;save(true);  // &lt;-- 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' =&gt; 'field_signature_media'));
    $fc_item-&gt;field_signature_image[LANGUAGE_NONE][] = 
                        array('alt' =&gt; '',
                              'title' =&gt; '',
                              'fid' =&gt; $file-&gt;fid,
                              'display' =&gt; 1,
                              'width' =&gt; $file-&gt;image_dimensions['width'],
                              'height' =&gt; $file-&gt;image_dimensions['height'],
                              'image_field_caption' =&gt; array
                                  (
                                      'value' =&gt; '',
                                      'format' =&gt; 'full_html',
                                  ),
                        );
    $fc_item-&gt;setHostEntity('node', $node);
    $fc_item-&gt;save(TRUE);
    field_attach_update('node', $node); // &lt;-- 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' =&gt; 'field_signature_media'));
    $fc_item-&gt;setHostEntity('node', $node);
    $fc_wrapper = entity_metadata_wrapper('field_collection_item', $fc_item);  
    $fc_wrapper-&gt;field_signature_image-&gt;set($file-&gt;fid);
    $fc_wrapper-&gt;save(true);  // &lt;-- at this point it enters infinite recursion.
    field_attach_update('node', $node);
There should be no difference between setting the field on a node versus a field collection item.

This should work

$fc_wrapper-&gt;field_signature_image-&gt;file-&gt;set($file);


This should also work

$fc_wrapper-&gt;field_signature_image = (array) $file;


assuming $file is a valid file object.  When I have trouble with adding files, it is usually because I have a property missing.  In that case, I will make a node and field collection item via the UI, and then dpm() to see what us missing.  Half the time, it is $file-&gt;display, and the rest is usually $file-&gt;description.  My guess is that $file-&gt;description is your problem.

What I don't recall is whether using File Entity changes this.  Both line above are based on import scripts I have where this module is disabled.