You are here

function uuid_file_entity_features_rebuild_files in UUID Features Integration 7

Runs the file import multiple times to resolve dependencies.

We might need several runs of ths function to resolve the dependencies created by reference fields. Those can only be resolved if the target file already exists.

Parameters

array $files: The files to process.

string $module: The module to rebuild for.

int $max_nesting: Maximal nesting level.

int $nesting_level: Current nesting level.

Return value

bool TRUE if all files could be restored.

1 call to uuid_file_entity_features_rebuild_files()
uuid_file_entity_features_rebuild in includes/uuid_file_entity.features.inc
Implements hook_features_rebuild().

File

includes/uuid_file_entity.features.inc, line 187
Features hooks for the uuid_file_entity features component.

Code

function uuid_file_entity_features_rebuild_files($files, $module, $max_nesting = 5, $nesting_level = 0) {

  // Max nesting level hit.
  if ($max_nesting < $nesting_level) {
    watchdog('UUID Features', 'Unable to restore files. Max nesting level reached.', array(), WATCHDOG_ERROR);
    return FALSE;
  }
  $second_run_files = array();
  foreach ($files as $data) {
    try {
      $file = (object) $data;
      $file->uid = 0;

      // Find the matching UUID, with a fresh cache.
      $fids = entity_get_id_by_uuid('file', array(
        $file->uuid,
      ));
      if (isset($fids[$file->uuid])) {
        $fid = array_key_exists($file->uuid, $fids) ? $fids[$file->uuid] : FALSE;
        $existing = file_load($fid);
        if (!empty($existing)) {
          $file->fid = $existing->fid;
        }
      }
      $entity_type = 'file';
      drupal_alter('uuid_entity_features_rebuild', $entity_type, $file, $data, $module);
      drupal_alter('uuid_file_entity_features_rebuild', $file, $module);

      // Already calls file_save().
      _uuid_features_file_field_import_file($file, $module);
    } catch (Exception $e) {
      $second_run_files[] = $data;
    }
  }
  if (!empty($second_run_files)) {
    return uuid_file_entity_features_rebuild_files($second_run_files, $module, $max_nesting, ++$nesting_level);
  }
  return TRUE;
}