You are here

function uuid_field_collection_features_rebuild in UUID Features Integration 7

Implements hook_features_rebuild().

1 call to uuid_field_collection_features_rebuild()
uuid_field_collection_features_revert in includes/uuid_field_collection.features.inc
Implements hook_features_revert().

File

includes/uuid_field_collection.features.inc, line 120
Features hooks for the uuid_field_collection features component.

Code

function uuid_field_collection_features_rebuild($module) {

  // Make sure all referenced entities exist.
  if (module_exists('taxonomy')) {

    // This module may not be installed.
    taxonomy_features_rebuild($module);
  }
  field_features_rebuild($module);
  uuid_node_features_rebuild($module);
  cache_clear_all();
  $field_collections = features_get_default('uuid_field_collection', $module);
  if (!empty($field_collections)) {
    $entity_type = 'field_collection_item';
    foreach ($field_collections as $field_collection_item) {
      try {
        $fields = field_info_instances('field_collection_item', $field_collection_item['bundle']);
        foreach ($fields as $field_name => $field_instance_config) {
          $field_config = field_info_field($field_name);
          if (isset($field_collection_item[$field_name])) {
            foreach ($field_collection_item[$field_name] as $lang => $field_data) {
              foreach ($field_data as $delta => $value) {
                if ($field_config['type'] == 'entityreference' || $field_config['type'] == 'taxonomy_term_reference') {
                  $columns = array_keys($field_config['columns']);
                  $first_column = reset($columns);
                  if (isset($field_config['settings']['target_type'])) {
                    $target_entity = $field_config['settings']['target_type'];
                  }
                  elseif ($field_config['type'] == 'taxonomy_term_reference') {
                    $target_entity = 'taxonomy_term';
                  }
                  else {
                    throw new Exception('Unknown target.');
                  }
                  $ids = entity_get_id_by_uuid($target_entity, array(
                    $value[$first_column],
                  ));
                  $field_collection_item[$field_name][$lang][$delta][$first_column] = $ids ? reset($ids) : UUID_FEATURES_DEFAULT_FALLBACK_ID;
                }
              }
            }
          }
        }
        $field_collection_item['original'] = TRUE;
        $entity = entity_import('field_collection_item', json_encode($field_collection_item));

        // Hack to ignore missing host entities.

        /* @var FieldCollectionItemEntity $entity */
        $entity->is_new = FALSE;
        drupal_alter('uuid_entity_features_rebuild', $entity_type, $entity, $field_collection_item, $module);
        uuid_features_file_field_import($entity, 'field_collection_item', $module);
        $entity
          ->save(TRUE);
      } catch (Exception $e) {
      }
    }
    module_invoke_all('uuid_entity_features_rebuild_complete', $entity_type, $field_collections, $module);
  }
  uuid_node_features_rebuild($module);
}