You are here

public function EntityFile::import in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/file/src/Plugin/migrate/destination/EntityFile.php \Drupal\file\Plugin\migrate\destination\EntityFile::import()

Import the row.

Derived classes must implement import(), to construct one new object (pre-populated) using ID mappings in the Migration.

Parameters

\Drupal\migrate\Row $row: The row object.

array $old_destination_id_values: The old destination ids.

Return value

mixed The entity id or an indication of success.

Overrides EntityContentBase::import

File

core/modules/file/src/Plugin/migrate/destination/EntityFile.php, line 103
Contains \Drupal\file\Plugin\migrate\destination\EntityFile.

Class

EntityFile
Every migration that uses this destination must have an optional dependency on the d6_file migration to ensure it runs first.

Namespace

Drupal\file\Plugin\migrate\destination

Code

public function import(Row $row, array $old_destination_id_values = array()) {

  // For stub rows, there is no real file to deal with, let the stubbing
  // process create the stub entity.
  if ($row
    ->isStub()) {
    return parent::import($row, $old_destination_id_values);
  }
  $file = $row
    ->getSourceProperty($this->configuration['source_path_property']);
  $destination = $row
    ->getDestinationProperty($this->configuration['destination_path_property']);
  $source = $this->configuration['source_base_path'] . $file;

  // 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 parent::import($row, $old_destination_id_values);
  }
  $replace = $this
    ->getOverwriteMode($row);
  $success = $this
    ->writeFile($source, $destination, $replace);
  if (!$success) {
    $dir = $this
      ->getDirectory($destination);
    if (file_prepare_directory($dir, FILE_CREATE_DIRECTORY)) {
      $success = $this
        ->writeFile($source, $destination, $replace);
    }
    else {
      throw new MigrateException("Could not create directory '{$dir}'");
    }
  }
  if ($success) {
    return parent::import($row, $old_destination_id_values);
  }
  else {
    throw new MigrateException("File {$source} could not be copied to {$destination}.");
  }
}