View source
<?php
namespace Drupal\migrate\Plugin\migrate\process;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\MigrateSkipProcessException;
use Drupal\migrate\Plugin\MigratePluginManager;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Entity\MigrationInterface;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Migration extends ProcessPluginBase implements ContainerFactoryPluginInterface {
protected $processPluginManager;
protected $migrationStorage;
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EntityStorageInterface $storage, MigratePluginManager $process_plugin_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->migrationStorage = $storage;
$this->migration = $migration;
$this->processPluginManager = $process_plugin_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
return new static($configuration, $plugin_id, $plugin_definition, $migration, $container
->get('entity.manager')
->getStorage('migration'), $container
->get('plugin.manager.migrate.process'));
}
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;
$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;
}
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 ($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);
$process = $migration
->get('process');
$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);
$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;
}
}
}
protected function skipOnEmpty($value) {
if (!array_filter($value)) {
throw new MigrateSkipProcessException();
}
}
}