2

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);

1 Answer 1

2

There should be no difference between setting the field on a node versus a field collection item.

This should work

$fc_wrapper->field_signature_image->file->set($file);

This should also work

$fc_wrapper->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->display, and the rest is usually $file->description. My guess is that $file->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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.