3 namespace Drupal\migrate\Plugin\migrate\process;
5 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
6 use Drupal\migrate\MigrateSkipProcessException;
7 use Drupal\migrate\Plugin\MigratePluginManagerInterface;
8 use Drupal\migrate\Plugin\MigrationPluginManagerInterface;
9 use Drupal\migrate\Plugin\MigrateIdMapInterface;
10 use Drupal\migrate\ProcessPluginBase;
11 use Drupal\migrate\Plugin\MigrationInterface;
12 use Drupal\migrate\MigrateExecutableInterface;
13 use Drupal\migrate\Row;
14 use Symfony\Component\DependencyInjection\ContainerInterface;
17 * Looks up the value of a property based on a previous migration.
19 * It is important to maintain relationships among content coming from the
20 * source site. For example, on the source site, a given user account may
21 * have an ID of 123, but the Drupal user account created from it may have
22 * a uid of 456. The migration process maintains the relationships between
23 * source and destination identifiers in map tables, and this information
24 * is leveraged by the migration_lookup process plugin.
26 * Available configuration keys
27 * - migration: A single migration ID, or an array of migration IDs.
28 * - source_ids: (optional) An array keyed by migration IDs with values that are
29 * a list of source properties.
30 * - stub_id: (optional) Identifies the migration which will be used to create
32 * - no_stub: (optional) Prevents the creation of a stub entity when no
33 * relationship is found in the migration map.
37 * Consider a node migration, where you want to maintain authorship. If you have
38 * migrated the user accounts in a migration named "users", you would specify
44 * plugin: migration_lookup
49 * This takes the value of the author property in the source data, and looks it
50 * up in the map table associated with the users migration, returning the
51 * resulting user ID and assigning it to the destination uid property.
53 * The value of 'migration' can be a list of migration IDs. When using multiple
54 * migrations it is possible each use different source identifiers. In this
55 * case one can use source_ids which is an array keyed by the migration IDs
56 * and the value is a list of source properties.
61 * plugin: migration_lookup
72 * If the migration_lookup plugin does not find the source ID in the migration
73 * map it will create a stub entity for the relationship to use. This stub is
74 * generated by the migration provided. In the case of multiple migrations the
75 * first value of the migration list will be used, but you can select the
76 * migration you wish to use by using the stub_id configuration key:
81 * plugin: migration_lookup
88 * In the above example, the value of stub_id selects the members migration to
89 * create any stub entities.
91 * To prevent the creation of a stub entity when no relationship is found in the
92 * migration map, use no_stub:
97 * plugin: migration_lookup
103 * @see \Drupal\migrate\Plugin\MigrateProcessInterface
105 * @MigrateProcessPlugin(
106 * id = "migration_lookup"
109 class MigrationLookup extends ProcessPluginBase implements ContainerFactoryPluginInterface {
112 * The process plugin manager.
114 * @var \Drupal\migrate\Plugin\MigratePluginManager
116 protected $processPluginManager;
119 * The migration plugin manager.
121 * @var \Drupal\migrate\Plugin\MigrationPluginManagerInterface
123 protected $migrationPluginManager;
126 * The migration to be executed.
128 * @var \Drupal\migrate\Plugin\MigrationInterface
130 protected $migration;
135 public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, MigrationPluginManagerInterface $migration_plugin_manager, MigratePluginManagerInterface $process_plugin_manager) {
136 parent::__construct($configuration, $plugin_id, $plugin_definition);
137 $this->migrationPluginManager = $migration_plugin_manager;
138 $this->migration = $migration;
139 $this->processPluginManager = $process_plugin_manager;
145 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
151 $container->get('plugin.manager.migration'),
152 $container->get('plugin.manager.migrate.process')
159 public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
160 $migration_ids = $this->configuration['migration'];
161 if (!is_array($migration_ids)) {
162 $migration_ids = [$migration_ids];
164 if (!is_array($value)) {
167 $this->skipOnEmpty($value);
169 /** @var \Drupal\migrate\Plugin\MigrationInterface[] $migrations */
170 $destination_ids = NULL;
171 $source_id_values = [];
172 $migrations = $this->migrationPluginManager->createInstances($migration_ids);
173 foreach ($migrations as $migration_id => $migration) {
174 if ($migration_id == $this->migration->id()) {
177 if (isset($this->configuration['source_ids'][$migration_id])) {
178 $configuration = ['source' => $this->configuration['source_ids'][$migration_id]];
179 $source_id_values[$migration_id] = $this->processPluginManager
180 ->createInstance('get', $configuration, $this->migration)
181 ->transform(NULL, $migrate_executable, $row, $destination_property);
184 $source_id_values[$migration_id] = $value;
186 // Break out of the loop as soon as a destination ID is found.
187 if ($destination_ids = $migration->getIdMap()->lookupDestinationId($source_id_values[$migration_id])) {
192 if (!$destination_ids && !empty($this->configuration['no_stub'])) {
196 if (!$destination_ids && ($self || isset($this->configuration['stub_id']) || count($migrations) == 1)) {
197 // If the lookup didn't succeed, figure out which migration will do the
200 $migration = $this->migration;
202 elseif (isset($this->configuration['stub_id'])) {
203 $migration = $migrations[$this->configuration['stub_id']];
206 $migration = reset($migrations);
208 $destination_plugin = $migration->getDestinationPlugin(TRUE);
209 // Only keep the process necessary to produce the destination ID.
210 $process = $migration->getProcess();
212 // We already have the source ID values but need to key them for the Row
214 $source_ids = $migration->getSourcePlugin()->getIds();
216 foreach (array_keys($source_ids) as $index => $source_id) {
217 $values[$source_id] = $source_id_values[$migration->id()][$index];
220 $stub_row = $this->createStubRow($values + $migration->getSourceConfiguration(), $source_ids);
222 // Do a normal migration with the stub row.
223 $migrate_executable->processRow($stub_row, $process);
224 $destination_ids = [];
226 $destination_ids = $destination_plugin->import($stub_row);
228 catch (\Exception $e) {
229 $migration->getIdMap()->saveMessage($stub_row->getSourceIdValues(), $e->getMessage());
232 if ($destination_ids) {
233 $migration->getIdMap()->saveIdMapping($stub_row, $destination_ids, MigrateIdMapInterface::STATUS_NEEDS_UPDATE);
236 if ($destination_ids) {
237 if (count($destination_ids) == 1) {
238 return reset($destination_ids);
241 return $destination_ids;
247 * Skips the migration process entirely if the value is FALSE.
249 * @param mixed $value
250 * The incoming value to transform.
252 * @throws \Drupal\migrate\MigrateSkipProcessException
254 protected function skipOnEmpty(array $value) {
255 if (!array_filter($value)) {
256 throw new MigrateSkipProcessException();
261 * Create a stub row source for later import as stub data.
263 * This simple wrapper of the Row constructor allows sub-classing plugins to
264 * have more control over the row.
266 * @param array $values
267 * An array of values to add as properties on the object.
268 * @param array $source_ids
269 * An array containing the IDs of the source using the keys as the field
272 * @return \Drupal\migrate\Row
275 protected function createStubRow(array $values, array $source_ids) {
276 return new Row($values, $source_ids, TRUE);