You are here

function config_pages_import_update in Config Pages 7

Duplicate entity.

1 call to config_pages_import_update()
config_pages_import_form_submit in ./config_pages.admin.inc
config_pages_import_form submit handler.

File

./config_pages.admin.inc, line 168
ConfigPages editing UI.

Code

function config_pages_import_update($entity_type, $bundle, $import, $entity) {

  // Load field structure and prepare import object.
  $fields = field_info_instances($entity_type, $bundle);
  $import = get_object_vars($import);

  // Update dest with data from source.
  foreach ($import as $key => $value) {

    // Embedded field collections have special treatement.
    if (!empty($fields[$key]) && $fields[$key]['widget']['type'] == 'field_collection_embed' && !empty($value)) {

      // Nullify current field state.
      $entity->{$key}[LANGUAGE_NONE] = array();
      foreach ($import[$key][LANGUAGE_NONE] as $delta => $fcitem) {

        // Load source FCI.
        $source_collection_item = field_collection_item_load($fcitem['value']);

        // Create new field collection.
        $dest_collection_item = entity_create('field_collection_item', array(
          'field_name' => $key,
        ));
        $dest_collection_item
          ->setHostEntity($entity_type, $entity);

        // Duplicate fields from field collection.
        $fields2 = field_info_instances('field_collection_item', $key);
        $import_collection_item = (object) array();
        foreach ($fields2 as $field_name => $field_info) {
          $import_collection_item->{$field_name} = $source_collection_item->{$field_name};
        }
        $dest_collection_item = config_pages_import_update('field_collection_item', $key, $import_collection_item, $dest_collection_item);
        $dest_collection_item
          ->save();
      }
    }
    else {
      $entity->{$key} = $value;
    }
  }
  return $entity;
}