Pull merge.
[yaffs-website] / web / core / modules / migrate / src / Plugin / migrate / process / MigrationLookup.php
index 835fd691c9b3bf20c14973f71af72448a1994836..be51465268d34b56234ccf49fd71d8afa46d1736 100644 (file)
@@ -34,10 +34,12 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
  *
  * Examples:
  *
- * Consider a node migration, where you want to maintain authorship. If you have
- * migrated the user accounts in a migration named "users", you would specify
- * the following:
- *
+ * Consider a node migration, where you want to maintain authorship. Let's
+ * assume that users are previously migrated in a migration named 'users'. The
+ * 'users' migration saved the mapping between the source and destination IDs in
+ * a map table. The node migration example below maps the node 'uid' property so
+ * that we first take the source 'author' value and then do a lookup for the
+ * corresponding Drupal user ID from the map table.
  * @code
  * process:
  *   uid:
@@ -46,15 +48,10 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
  *     source: author
  * @endcode
  *
- * This takes the value of the author property in the source data, and looks it
- * up in the map table associated with the users migration, returning the
- * resulting user ID and assigning it to the destination uid property.
- *
  * The value of 'migration' can be a list of migration IDs. When using multiple
  * migrations it is possible each use different source identifiers. In this
  * case one can use source_ids which is an array keyed by the migration IDs
- * and the value is a list of source properties.
- *
+ * and the value is a list of source properties. See example below.
  * @code
  * process:
  *   uid:
@@ -73,8 +70,8 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
  * map it will create a stub entity for the relationship to use. This stub is
  * generated by the migration provided. In the case of multiple migrations the
  * first value of the migration list will be used, but you can select the
- * migration you wish to use by using the stub_id configuration key:
- *
+ * migration you wish to use by using the stub_id configuration key. The example
+ * below uses 'members' migration to create stub entities.
  * @code
  * process:
  *   uid:
@@ -85,12 +82,8 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
  *     stub_id: members
  * @endcode
  *
- * In the above example, the value of stub_id selects the members migration to
- * create any stub entities.
- *
  * To prevent the creation of a stub entity when no relationship is found in the
- * migration map, use no_stub:
- *
+ * migration map, 'no_stub' configuration can be used as shown below.
  * @code
  * process:
  *   uid:
@@ -161,10 +154,6 @@ class MigrationLookup extends ProcessPluginBase implements ContainerFactoryPlugi
     if (!is_array($migration_ids)) {
       $migration_ids = [$migration_ids];
     }
-    if (!is_array($value)) {
-      $value = [$value];
-    }
-    $this->skipOnEmpty($value);
     $self = FALSE;
     /** @var \Drupal\migrate\Plugin\MigrationInterface[] $migrations */
     $destination_ids = NULL;
@@ -176,13 +165,15 @@ class MigrationLookup extends ProcessPluginBase implements ContainerFactoryPlugi
       }
       if (isset($this->configuration['source_ids'][$migration_id])) {
         $configuration = ['source' => $this->configuration['source_ids'][$migration_id]];
-        $source_id_values[$migration_id] = $this->processPluginManager
+        $value = $this->processPluginManager
           ->createInstance('get', $configuration, $this->migration)
           ->transform(NULL, $migrate_executable, $row, $destination_property);
       }
-      else {
-        $source_id_values[$migration_id] = $value;
+      if (!is_array($value)) {
+        $value = [$value];
       }
+      $this->skipOnEmpty($value);
+      $source_id_values[$migration_id] = $value;
       // Break out of the loop as soon as a destination ID is found.
       if ($destination_ids = $migration->getIdMap()->lookupDestinationId($source_id_values[$migration_id])) {
         break;
@@ -222,15 +213,16 @@ class MigrationLookup extends ProcessPluginBase implements ContainerFactoryPlugi
       // Do a normal migration with the stub row.
       $migrate_executable->processRow($stub_row, $process);
       $destination_ids = [];
+      $id_map = $migration->getIdMap();
       try {
         $destination_ids = $destination_plugin->import($stub_row);
       }
       catch (\Exception $e) {
-        $migration->getIdMap()->saveMessage($stub_row->getSourceIdValues(), $e->getMessage());
+        $id_map->saveMessage($stub_row->getSourceIdValues(), $e->getMessage());
       }
 
       if ($destination_ids) {
-        $migration->getIdMap()->saveIdMapping($stub_row, $destination_ids, MigrateIdMapInterface::STATUS_NEEDS_UPDATE);
+        $id_map->saveIdMapping($stub_row, $destination_ids, MigrateIdMapInterface::STATUS_NEEDS_UPDATE);
       }
     }
     if ($destination_ids) {
@@ -246,7 +238,7 @@ class MigrationLookup extends ProcessPluginBase implements ContainerFactoryPlugi
   /**
    * Skips the migration process entirely if the value is FALSE.
    *
-   * @param mixed $value
+   * @param array $value
    *   The incoming value to transform.
    *
    * @throws \Drupal\migrate\MigrateSkipProcessException