You are here

public function Migration::transform in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/migrate/src/Plugin/migrate/process/Migration.php \Drupal\migrate\Plugin\migrate\process\Migration::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

core/modules/migrate/src/Plugin/migrate/process/Migration.php, line 67
Contains \Drupal\migrate\Plugin\migrate\process\Migration.

Class

Migration
Calculates the value of a property based on a previous migration.

Namespace

Drupal\migrate\Plugin\migrate\process

Code

public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
  $migration_ids = $this->configuration['migration'];
  if (!is_array($migration_ids)) {
    $migration_ids = array(
      $migration_ids,
    );
  }
  $scalar = FALSE;
  if (!is_array($value)) {
    $scalar = TRUE;
    $value = array(
      $value,
    );
  }
  $this
    ->skipOnEmpty($value);
  $self = FALSE;

  /** @var \Drupal\migrate\Entity\MigrationInterface[] $migrations */
  $migrations = $this->migrationStorage
    ->loadMultiple($migration_ids);
  $destination_ids = NULL;
  $source_id_values = array();
  foreach ($migrations as $migration_id => $migration) {
    if ($migration_id == $this->migration
      ->id()) {
      $self = TRUE;
    }
    if (isset($this->configuration['source_ids'][$migration_id])) {
      $configuration = array(
        'source' => $this->configuration['source_ids'][$migration_id],
      );
      $source_id_values[$migration_id] = $this->processPluginManager
        ->createInstance('get', $configuration, $this->migration)
        ->transform(NULL, $migrate_executable, $row, $destination_property);
    }
    else {
      $source_id_values[$migration_id] = $value;
    }

    // Break out of the loop as soon as a destination ID is found.
    if ($destination_ids = $migration
      ->getIdMap()
      ->lookupDestinationID($source_id_values[$migration_id])) {
      break;
    }
  }
  if (!$destination_ids && ($self || isset($this->configuration['stub_id']) || count($migrations) == 1)) {

    // If the lookup didn't succeed, figure out which migration will do the
    // stubbing.
    if ($self) {
      $migration = $this->migration;
    }
    elseif (isset($this->configuration['stub_id'])) {
      $migration = $migrations[$this->configuration['stub_id']];
    }
    else {
      $migration = reset($migrations);
    }
    $destination_plugin = $migration
      ->getDestinationPlugin(TRUE);

    // Only keep the process necessary to produce the destination ID.
    $process = $migration
      ->get('process');

    // We already have the source id values but need to key them for the Row
    // constructor.
    $source_ids = $migration
      ->getSourcePlugin()
      ->getIds();
    $values = array();
    foreach (array_keys($source_ids) as $index => $source_id) {
      $values[$source_id] = $source_id_values[$migration
        ->id()][$index];
    }
    $stub_row = new Row($values + $migration
      ->get('source'), $source_ids, TRUE);

    // Do a normal migration with the stub row.
    $migrate_executable
      ->processRow($stub_row, $process);
    $destination_ids = array();
    try {
      $destination_ids = $destination_plugin
        ->import($stub_row);
    } catch (\Exception $e) {
      $migrate_executable
        ->saveMessage($e
        ->getMessage());
    }
  }
  if ($destination_ids) {
    if ($scalar) {
      if (count($destination_ids) == 1) {
        return reset($destination_ids);
      }
    }
    else {
      return $destination_ids;
    }
  }
}