function MigrationLookup::transform

Same name in other branches
  1. 8.9.x core/modules/migrate/src/Plugin/migrate/process/MigrationLookup.php \Drupal\migrate\Plugin\migrate\process\MigrationLookup::transform()
  2. 10 core/modules/migrate/src/Plugin/migrate/process/MigrationLookup.php \Drupal\migrate\Plugin\migrate\process\MigrationLookup::transform()
  3. 11.x core/modules/migrate/src/Plugin/migrate/process/MigrationLookup.php \Drupal\migrate\Plugin\migrate\process\MigrationLookup::transform()

Throws

\Drupal\migrate\MigrateSkipProcessException

\Drupal\migrate\MigrateException

Overrides ProcessPluginBase::transform

File

core/modules/migrate/src/Plugin/migrate/process/MigrationLookup.php, line 193

Class

MigrationLookup
Looks up 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) {
    $lookup_migration_ids = (array) $this->configuration['migration'];
    $self = FALSE;
    $destination_ids = NULL;
    $source_id_values = [];
    foreach ($lookup_migration_ids as $lookup_migration_id) {
        $lookup_value = $value;
        if ($lookup_migration_id == $this->migration
            ->id()) {
            $self = TRUE;
        }
        if (isset($this->configuration['source_ids'][$lookup_migration_id])) {
            $lookup_value = array_values($row->getMultiple($this->configuration['source_ids'][$lookup_migration_id]));
        }
        $lookup_value = (array) $lookup_value;
        $this->skipInvalid($lookup_value);
        $source_id_values[$lookup_migration_id] = $lookup_value;
        // Re-throw any PluginException as a MigrateException so the executable
        // can shut down the migration.
        try {
            $destination_id_array = $this->migrateLookup
                ->lookup($lookup_migration_id, $lookup_value);
        } catch (PluginNotFoundException $e) {
            $destination_id_array = [];
        } catch (MigrateException $e) {
            throw $e;
        } catch (\Exception $e) {
            throw new MigrateException(sprintf('A %s was thrown while processing this migration lookup', gettype($e)), $e->getCode(), $e);
        }
        if ($destination_id_array) {
            $destination_ids = array_values(reset($destination_id_array));
            break;
        }
    }
    if (!$destination_ids && !empty($this->configuration['no_stub'])) {
        return NULL;
    }
    if (!$destination_ids && ($self || isset($this->configuration['stub_id']) || count($lookup_migration_ids) == 1)) {
        // If the lookup didn't succeed, figure out which migration will do the
        // stubbing.
        if ($self) {
            $stub_migration = $this->migration
                ->id();
        }
        elseif (isset($this->configuration['stub_id'])) {
            $stub_migration = $this->configuration['stub_id'];
        }
        else {
            $stub_migration = reset($lookup_migration_ids);
        }
        // Rethrow any exception as a MigrateException so the executable can shut
        // down the migration.
        try {
            $destination_ids = $this->migrateStub
                ->createStub($stub_migration, $source_id_values[$stub_migration], [], FALSE);
        } catch (\LogicException $e) {
            // For BC reasons, we must allow attempting to stub a derived migration.
        } catch (PluginNotFoundException $e) {
            // For BC reasons, we must allow attempting to stub a non-existent
            // migration.
        } catch (MigrateException $e) {
            throw $e;
        } catch (MigrateSkipRowException $e) {
            throw $e;
        } catch (\Exception $e) {
            throw new MigrateException(sprintf('%s was thrown while attempting to stub: %s', get_class($e), $e->getMessage()), $e->getCode(), $e);
        }
    }
    if ($destination_ids) {
        if (count($destination_ids) == 1) {
            return reset($destination_ids);
        }
        else {
            return $destination_ids;
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.