Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / migrate / src / Plugin / migrate / process / MigrationLookup.php
1 <?php
2
3 namespace Drupal\migrate\Plugin\migrate\process;
4
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;
15
16 /**
17  * Looks up the value of a property based on a previous migration.
18  *
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.
25  *
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
31  *   any stub entities.
32  * - no_stub: (optional) Prevents the creation of a stub entity when no
33  *   relationship is found in the migration map.
34  *
35  * Examples:
36  *
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
39  * the following:
40  *
41  * @code
42  * process:
43  *   uid:
44  *     plugin: migration_lookup
45  *     migration: users
46  *     source: author
47  * @endcode
48  *
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.
52  *
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.
57  *
58  * @code
59  * process:
60  *   uid:
61  *     plugin: migration_lookup
62  *       migration:
63  *         - users
64  *         - members
65  *       source_ids:
66  *         users:
67  *           - author
68  *         members:
69  *           - id
70  * @endcode
71  *
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:
77  *
78  * @code
79  * process:
80  *   uid:
81  *     plugin: migration_lookup
82  *     migration:
83  *       - users
84  *       - members
85  *     stub_id: members
86  * @endcode
87  *
88  * In the above example, the value of stub_id selects the members migration to
89  * create any stub entities.
90  *
91  * To prevent the creation of a stub entity when no relationship is found in the
92  * migration map, use no_stub:
93  *
94  * @code
95  * process:
96  *   uid:
97  *     plugin: migration_lookup
98  *     migration: users
99  *     no_stub: true
100  *     source: author
101  * @endcode
102  *
103  * @see \Drupal\migrate\Plugin\MigrateProcessInterface
104  *
105  * @MigrateProcessPlugin(
106  *   id = "migration_lookup"
107  * )
108  */
109 class MigrationLookup extends ProcessPluginBase implements ContainerFactoryPluginInterface {
110
111   /**
112    * The process plugin manager.
113    *
114    * @var \Drupal\migrate\Plugin\MigratePluginManager
115    */
116   protected $processPluginManager;
117
118   /**
119    * The migration plugin manager.
120    *
121    * @var \Drupal\migrate\Plugin\MigrationPluginManagerInterface
122    */
123   protected $migrationPluginManager;
124
125   /**
126    * The migration to be executed.
127    *
128    * @var \Drupal\migrate\Plugin\MigrationInterface
129    */
130   protected $migration;
131
132   /**
133    * {@inheritdoc}
134    */
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;
140   }
141
142   /**
143    * {@inheritdoc}
144    */
145   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
146     return new static(
147       $configuration,
148       $plugin_id,
149       $plugin_definition,
150       $migration,
151       $container->get('plugin.manager.migration'),
152       $container->get('plugin.manager.migrate.process')
153     );
154   }
155
156   /**
157    * {@inheritdoc}
158    */
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];
163     }
164     if (!is_array($value)) {
165       $value = [$value];
166     }
167     $this->skipOnEmpty($value);
168     $self = FALSE;
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()) {
175         $self = TRUE;
176       }
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);
182       }
183       else {
184         $source_id_values[$migration_id] = $value;
185       }
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])) {
188         break;
189       }
190     }
191
192     if (!$destination_ids && !empty($this->configuration['no_stub'])) {
193       return NULL;
194     }
195
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
198       // stubbing.
199       if ($self) {
200         $migration = $this->migration;
201       }
202       elseif (isset($this->configuration['stub_id'])) {
203         $migration = $migrations[$this->configuration['stub_id']];
204       }
205       else {
206         $migration = reset($migrations);
207       }
208       $destination_plugin = $migration->getDestinationPlugin(TRUE);
209       // Only keep the process necessary to produce the destination ID.
210       $process = $migration->getProcess();
211
212       // We already have the source ID values but need to key them for the Row
213       // constructor.
214       $source_ids = $migration->getSourcePlugin()->getIds();
215       $values = [];
216       foreach (array_keys($source_ids) as $index => $source_id) {
217         $values[$source_id] = $source_id_values[$migration->id()][$index];
218       }
219
220       $stub_row = $this->createStubRow($values + $migration->getSourceConfiguration(), $source_ids);
221
222       // Do a normal migration with the stub row.
223       $migrate_executable->processRow($stub_row, $process);
224       $destination_ids = [];
225       try {
226         $destination_ids = $destination_plugin->import($stub_row);
227       }
228       catch (\Exception $e) {
229         $migration->getIdMap()->saveMessage($stub_row->getSourceIdValues(), $e->getMessage());
230       }
231
232       if ($destination_ids) {
233         $migration->getIdMap()->saveIdMapping($stub_row, $destination_ids, MigrateIdMapInterface::STATUS_NEEDS_UPDATE);
234       }
235     }
236     if ($destination_ids) {
237       if (count($destination_ids) == 1) {
238         return reset($destination_ids);
239       }
240       else {
241         return $destination_ids;
242       }
243     }
244   }
245
246   /**
247    * Skips the migration process entirely if the value is FALSE.
248    *
249    * @param mixed $value
250    *   The incoming value to transform.
251    *
252    * @throws \Drupal\migrate\MigrateSkipProcessException
253    */
254   protected function skipOnEmpty(array $value) {
255     if (!array_filter($value)) {
256       throw new MigrateSkipProcessException();
257     }
258   }
259
260   /**
261    * Create a stub row source for later import as stub data.
262    *
263    * This simple wrapper of the Row constructor allows sub-classing plugins to
264    * have more control over the row.
265    *
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
270    *   names.
271    *
272    * @return \Drupal\migrate\Row
273    *   The stub row.
274    */
275   protected function createStubRow(array $values, array $source_ids) {
276     return new Row($values, $source_ids, TRUE);
277   }
278
279 }