You are here

function _config_pages_features_import_data in Config Pages 7

Update field collection field on an entity.

1 call to _config_pages_features_import_data()
config_pages_features_rebuild in ./config_pages.features.inc
Implements hook_features_rebuild().

File

./config_pages.features.inc, line 151
Integrates with features.

Code

function _config_pages_features_import_data($entity_type, $entity, $import) {

  // Replace old values with new ones.
  foreach ($import as $field => $value) {

    // Embeded field collections have special treatement.
    if (strpos($field, 'field_collection_item:') === 0) {
      $key = str_replace('field_collection_item:', '', $field);
      foreach ($value as $delta => $item) {
        $field_collection_item = NULL;

        // Load existing field collection.
        if (!empty($entity->{$key}[LANGUAGE_NONE][$delta])) {
          $field_collection_item = field_collection_item_load($entity->{$key}[LANGUAGE_NONE][$delta]['value']);
        }

        // Or create a fresh one.
        if (empty($field_collection_item)) {
          $field_collection_item = entity_create('field_collection_item', array(
            'field_name' => $key,
          ));
          $field_collection_item
            ->setHostEntity($entity_type, $entity);
          $entity->{$key}[LANGUAGE_NONE][$delta]['value'] = $field_collection_item->item_id;
          $entity->{$key}[LANGUAGE_NONE][$delta]['revision_id'] = $field_collection_item->revision_id;
        }
        $field_collection_item = _config_pages_features_import_data('field_collection_item', $field_collection_item, $item);
        $field_collection_item
          ->save();
      }
    }
    else {
      $entity->{$field} = $value;
    }
  }
  return $entity;
}