You are here

public function MediaFileCopy::transform in Migrate File Entities to Media Entities 8

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

File

src/Plugin/migrate/process/MediaFileCopy.php, line 72

Class

MediaFileCopy
Copies or local file for usage in media module.

Namespace

Drupal\migrate_file_to_media\Plugin\migrate\process

Code

public function transform($source_id, 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_folder = $this->configuration['path'] ?? 'public://media/';
  $destination = $destination_folder . $row
    ->getSourceProperty('file_name');
  $source_file = false;

  // If the source path or URI represents a remote resource, delegate to the
  // download plugin.
  if (!$this
    ->isLocalUri($source_id)) {
    $source = $this->downloadPlugin
      ->transform([
      $source_id,
      'public://download/' . $row
        ->getSourceProperty('file_name'),
    ], $migrate_executable, $row, $destination_property);
  }
  else {
    $source_file = File::load($source_id);
    $source = $source_file
      ->getFileUri();
  }
  if ($source_file) {
    $destination = $destination_folder . $source_file
      ->getFilename();
  }

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

  // Prepare destination folder.
  if (strpos($destination_folder, 'rokka') !== 0) {

    // Check if a writable directory exists, and if not try to create it.
    $dir = $this
      ->getDirectory($destination);

    // If the directory exists and is writable, avoid file_prepare_directory()
    // call and write the file to destination.
    if (!is_dir($dir) || !is_writable($dir)) {
      if (!\Drupal::service('file_system')
        ->prepareDirectory($dir, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS)) {
        throw new MigrateException("Could not create or write to directory '{$dir}'");
      }
    }
  }
  $final_destination = $this
    ->saveFile($source, $destination);
  if ($final_destination) {
    if ($this->moduleHandler
      ->moduleExists('crop')) {
      $this
        ->updateFocalPoint($source, $final_destination
        ->getFileUri(), $final_destination);
    }
    return $final_destination
      ->id();
  }
  throw new MigrateException("File {$source} could not be copied to {$destination}");
}