You are here

public function FileBlob::transform in Migrate Plus 8.5

Same name and namespace in other branches
  1. 8.2 src/Plugin/migrate/process/FileBlob.php \Drupal\migrate_plus\Plugin\migrate\process\FileBlob::transform()
  2. 8.3 src/Plugin/migrate/process/FileBlob.php \Drupal\migrate_plus\Plugin\migrate\process\FileBlob::transform()
  3. 8.4 src/Plugin/migrate/process/FileBlob.php \Drupal\migrate_plus\Plugin\migrate\process\FileBlob::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 ProcessPluginBase::transform

File

src/Plugin/migrate/process/FileBlob.php, line 119

Class

FileBlob
Copy a file from a blob into a file.

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;
  }
  [
    $destination,
    $blob,
  ] = $value;

  // Determine if we going to overwrite existing files or not touch them.
  $replace = $this
    ->getOverwriteMode();

  // Attempt to save the file to avoid calling file_prepare_directory() any
  // more than absolutely necessary.
  if ($this
    ->putFile($destination, $blob, $replace)) {
    return $destination;
  }
  $dir = $this
    ->getDirectory($destination);
  $success = $this->fileSystem
    ->prepareDirectory($dir, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
  if (!$success) {
    throw new MigrateSkipProcessException("Could not create directory '{$dir}'");
  }
  if ($this
    ->putFile($destination, $blob, $replace)) {
    return $destination;
  }
  throw new MigrateSkipProcessException("Blob data could not be copied to {$destination}.");
}