You are here

function file_entity_uuid_entity_features_rebuild_alter in UUID Features Integration 7

Implements hook_uuid_entity_features_rebuild_alter().

File

includes/modules/file_entity.inc, line 109
Hooks for uuid_features on behalf of the file_entity module.

Code

function file_entity_uuid_entity_features_rebuild_alter($entity_type, &$entity, $data, $module) {
  list($entity_id, $revision_id, $import_bundle) = entity_extract_ids($entity_type, $entity);

  // Ensure the file entity import was run first. The function has a static
  // cache to avoid multiple runs in the same feature module.
  module_load_include('inc', 'uuid_features', 'includes/uuid_file_entity.features');
  uuid_file_entity_features_rebuild($module);

  // Handle referenced file entities in fields.
  // Get all fields from this bundle.
  $fields = field_info_instances($entity_type, $import_bundle);
  $supported_fields = array_map('trim', explode(',', variable_get('uuid_features_file_supported_fields', 'file, image')));
  foreach ($fields as $field_name => $field_instance) {
    if (isset($entity->{$field_name})) {

      // Load field info to check the type.
      $info = field_info_field($field_name);
      if (in_array($info['type'], $supported_fields) && !empty($entity->{$field_name})) {

        // Ensure file entity is properly processed for the import.
        foreach ($entity->{$field_name} as $lang_code => $field_values) {
          foreach ($field_values as $i => $field_value) {
            if (isset($field_value['file_uuid'])) {
              $fids = entity_get_id_by_uuid('file', array(
                $field_value['file_uuid'],
              ));
              $import_file_entity = file_load(reset($fids));
              unset($field_value['file_uuid']);

              // Ensure additional added array keys are preserved.
              $entity->{$field_name}[$lang_code][$i] = (array) $import_file_entity + $field_value;
            }
            else {
              $file_entity_type = 'file';
              $import_file_entity = (object) $field_value;
              drupal_alter('uuid_entity_features_rebuild', $file_entity_type, $import_file_entity);
              $entity->{$field_name}[$lang_code][$i] = (array) $import_file_entity;
            }
          }
        }
      }
    }
  }
}