You are here

public function FileCopy::transform in Migrate Plus 8.2

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 ProcessPluginBase::transform

File

src/Plugin/migrate/process/FileCopy.php, line 78

Class

FileCopy
Copy a file from one place into another.

Namespace

Drupal\migrate_plus\Plugin\migrate\process

Code

public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {

  // If we're stubbing a file entity, return a URI of NULL so it will get
  // stubbed by the general process.
  if ($row
    ->isStub()) {
    return NULL;
  }
  list($source, $destination) = $value;

  // Ensure the source file exists, if it's a local URI or path.
  if ($this
    ->isLocalUri($source) && !file_exists($source)) {
    throw new MigrateException("File '{$source}' does not exist");
  }

  // If the start and end file is exactly the same, there is nothing to do.
  if ($this
    ->isLocationUnchanged($source, $destination)) {
    return $destination;
  }
  $replace = $this
    ->getOverwriteMode();

  // We attempt the copy/move first to avoid calling file_prepare_directory()
  // any more than absolutely necessary.
  $final_destination = $this
    ->writeFile($source, $destination, $replace);
  if ($final_destination) {
    return $final_destination;
  }

  // If writeFile didn't work, make sure there's a writable directory in
  // place.
  $dir = $this
    ->getDirectory($destination);
  if (!file_prepare_directory($dir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
    throw new MigrateException("Could not create or write to directory '{$dir}'");
  }
  $final_destination = $this
    ->writeFile($source, $destination, $replace);
  if ($final_destination) {
    return $final_destination;
  }
  throw new MigrateException("File {$source} could not be copied to {$destination}");
}