<?php
namespace Drupal\paragraphs\Plugin\migrate\process;
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\migrate\MigrateException;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\MigrateLookupInterface;
use Drupal\migrate\MigrateStubInterface;
use Drupal\migrate\Plugin\migrate\process\MigrationLookup;
use Drupal\migrate\Plugin\MigrateIdMapInterface;
use Drupal\migrate\Plugin\MigratePluginManagerInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Plugin\MigrationPluginManagerInterface;
use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ParagraphsLookup extends MigrationLookup {
protected $migrationPluginManager;
protected $processPluginManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, MigrateLookupInterface $migrate_lookup, MigrateStubInterface $migrate_stub, MigrationPluginManagerInterface $migration_plugin_manager, MigratePluginManagerInterface $process_plugin_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $migrate_lookup, $migrate_stub);
$this->migrationPluginManager = $migration_plugin_manager;
$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('migrate.lookup'), $container
->get('migrate.stub'), $container
->get('plugin.manager.migration'), $container
->get('plugin.manager.migrate.process'));
}
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
$source_id_values = [];
$destination_ids = NULL;
$migrations = [];
if (isset($this->configuration['tags'])) {
$tags = (array) $this->configuration['tags'];
foreach ($tags as $tag) {
$tag_migrations = $this->migrationPluginManager
->createInstancesByTag($tag);
$migrations += $tag_migrations;
if (isset($this->configuration['tag_ids'][$tag])) {
$configuration = [
'source' => $this->configuration['tag_ids'][$tag],
];
try {
$get_process_plugin = $this->processPluginManager
->createInstance('get', $configuration, $this->migration);
} catch (PluginException $e) {
continue;
}
$value = $get_process_plugin
->transform(NULL, $migrate_executable, $row, $destination_property);
}
foreach ($tag_migrations as $migration_id => $migration) {
$source_id_values[$migration_id] = (array) $value;
$destination_ids = $this
->lookupDestination($migration, $value);
if ($destination_ids) {
break 2;
}
}
}
}
elseif (!empty($this->configuration['migration'])) {
$destination_ids = parent::transform($value, $migrate_executable, $row, $destination_property);
$migration_ids = $this->configuration['migration'];
if (!is_array($migration_ids)) {
$migration_ids = (array) $migration_ids;
}
$migrations = $this->migrationPluginManager
->createInstances($migration_ids);
foreach ($migrations as $migration_id => $migration) {
if (isset($this->configuration['source_ids'][$migration_id])) {
$configuration = [
'source' => $this->configuration['source_ids'][$migration_id],
];
$value = $this->processPluginManager
->createInstance('get', $configuration, $this->migration)
->transform(NULL, $migrate_executable, $row, $destination_property);
}
$source_id_values[$migration_id] = (array) $value;
$destination_ids = $this
->lookupDestination($migration, $value);
if ($destination_ids) {
break;
}
}
}
else {
throw new MigrateException("Either Migration or Tags must be defined.");
}
if (!$destination_ids && !empty($this->configuration['no_stub'])) {
return NULL;
}
if (!$destination_ids) {
if (isset($this->configuration['stub_id'])) {
$migration = $this->migrationPluginManager
->createInstance($this->configuration['stub_id']);
assert($migration instanceof MigrationInterface);
}
else {
$migration = reset($migrations);
}
$destination_plugin = $migration
->getDestinationPlugin(TRUE);
$process = $migration
->getProcess();
$source_ids = $migration
->getSourcePlugin()
->getIds();
$values = [];
foreach (array_keys($source_ids) as $index => $source_id) {
$values[$source_id] = $source_id_values[$migration
->getPluginId()][$index];
}
$stub_row = new Row($values + $migration
->getSourceConfiguration(), $source_ids, TRUE);
$migrate_executable
->processRow($stub_row, $process);
$destination_ids = [];
$id_map = $migration
->getIdMap();
try {
$destination_ids = $destination_plugin
->import($stub_row);
} catch (\Exception $e) {
$id_map
->saveMessage($stub_row
->getSourceIdValues(), $e
->getMessage());
}
if ($destination_ids) {
$id_map
->saveIdMapping($stub_row, $destination_ids, MigrateIdMapInterface::STATUS_NEEDS_UPDATE);
}
}
if ($destination_ids) {
if (count($destination_ids) == 1) {
return reset($destination_ids);
}
else {
return $destination_ids;
}
}
throw new MigrateException("Paragraphs lookup wasn't able to find the corresponding property for paragraph with source ID {$value} for the destination property {$destination_property}.");
}
protected function lookupDestination(MigrationInterface $migration, $value) {
$value = (array) $value;
$this
->skipInvalid($value);
if ($destination_ids = $migration
->getIdMap()
->lookupDestinationIds($value)) {
$destination_ids = array_combine(array_keys($migration
->getDestinationPlugin()
->getIds()), reset($destination_ids));
return $destination_ids;
}
return FALSE;
}
}