You are here

public function FileImport::transform in Migrate Files (extended) 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/migrate/process/FileImport.php \Drupal\migrate_file\Plugin\migrate\process\FileImport::transform()

Performs the associated process.

Parameters

mixed $value: The value to be transformed.

\Drupal\migrate\MigrateExecutableInterface $migrate_executable: The migration in which this process is being executed.

\Drupal\migrate\Row $row: The row from the source to process. Normally, just transforming the value is adequate but very rarely you might need to change two columns at the same time or something like that.

string $destination_property: The destination property currently worked on. This is only used together with the $row above.

Return value

string|array The newly transformed value.

Overrides FileCopy::transform

1 call to FileImport::transform()
ImageImport::transform in src/Plugin/migrate/process/ImageImport.php
Performs the associated process.
1 method overrides FileImport::transform()
ImageImport::transform in src/Plugin/migrate/process/ImageImport.php
Performs the associated process.

File

src/Plugin/migrate/process/FileImport.php, line 167

Class

FileImport
Imports a file from an local or external source.

Namespace

Drupal\migrate_file\Plugin\migrate\process

Code

public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
  if (!$value) {
    return NULL;
  }

  // Get our file entity values.
  $source = $value;
  $destination = $this
    ->getPropertyValue($this->configuration['destination'], $row) ?: 'public://';
  $uid = $this
    ->getPropertyValue($this->configuration['uid'], $row) ?: 0;
  $id_only = $this->configuration['id_only'];

  // If there's no we skip.
  if (!$source) {
    return NULL;
  }
  elseif ($this->configuration['skip_on_missing_source'] && !$this
    ->sourceExists($source)) {

    // If we have a source file path, but it doesn't exist, and we're meant
    // to just skip processing, we do so, but we log the message.
    $migrate_executable
      ->saveMessage("Source file {$source} does not exist. Skipping.");
    return NULL;
  }

  // Build the destination file uri (in case only a directory was provided).
  $destination = $this
    ->getDestinationFilePath($source, $destination);
  if (!$this->fileSystem
    ->uriScheme($destination)) {
    if (empty($destination)) {
      $destination = file_default_scheme() . '://' . preg_replace('/^\\//', '', $destination);
    }
  }
  $final_destination = '';

  // If we're in re-use mode, reuse the file if it exists.
  if ($this
    ->getOverwriteMode() == FILE_EXISTS_ERROR && $this
    ->isLocalUri($destination) && is_file($destination)) {

    // Look for a file entity with the destination uri.
    if ($files = \Drupal::entityTypeManager()
      ->getStorage('file')
      ->loadByProperties([
      'uri' => $destination,
    ])) {

      // Grab the first file entity with a matching uri.
      // @todo: Any logic for preference when there are multiple?
      $file = reset($files);

      // Set to permanent if the file in the database is set to temporary.
      // This means that the file was probably set to be removed during
      // garbage collection, which we don't want to happen anymore since we're
      // using it.
      if (!$file
        ->isTemporary()) {
        $file
          ->setPermanent();
        $file
          ->save();
      }
      return $id_only ? $file
        ->id() : [
        'target_id' => $file
          ->id(),
      ];
    }
    else {
      $final_destination = $destination;
    }
  }
  else {

    // The parent method will take care of our download/move/copy/rename.
    // We just need to final destination to create the file object.
    try {
      $final_destination = parent::transform([
        $source,
        $destination,
      ], $migrate_executable, $row, $destination_property);
    } catch (MigrateException $e) {

      // Check if we're skipping on error
      if ($this->configuration['skip_on_error']) {
        $migrate_executable
          ->saveMessage("File {$source} could not be imported to {$destination}. Operation failed with message: " . $e
          ->getMessage());
        throw new MigrateSkipProcessException($e
          ->getMessage());
      }
      else {

        // Pass the error back on again.
        throw new MigrateException($e
          ->getMessage());
      }
    }
  }
  if ($final_destination) {

    // Create a file entity.
    $file = File::create([
      'uri' => $final_destination,
      'uid' => $uid,
      'status' => FILE_STATUS_PERMANENT,
    ]);
    $file
      ->save();
    return $id_only ? $file
      ->id() : [
      'target_id' => $file
        ->id(),
    ];
  }
  throw new MigrateException("File {$source} could not be imported to {$destination}");
}