X-Git-Url: http://aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Fmigrate%2Fsrc%2FPlugin%2Fmigrate%2Fprocess%2FMigrationLookup.php;h=be51465268d34b56234ccf49fd71d8afa46d1736;hb=5b8bb166bfa98770daef9de5c127fc2e6ef02340;hp=f387df5233fdb7613aa3c806faf5e541401afc0f;hpb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;p=yaffs-website diff --git a/web/core/modules/migrate/src/Plugin/migrate/process/MigrationLookup.php b/web/core/modules/migrate/src/Plugin/migrate/process/MigrationLookup.php index f387df523..be5146526 100644 --- a/web/core/modules/migrate/src/Plugin/migrate/process/MigrationLookup.php +++ b/web/core/modules/migrate/src/Plugin/migrate/process/MigrationLookup.php @@ -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; @@ -217,20 +208,21 @@ class MigrationLookup extends ProcessPluginBase implements ContainerFactoryPlugi $values[$source_id] = $source_id_values[$migration->id()][$index]; } - $stub_row = new Row($values + $migration->getSourceConfiguration(), $source_ids, TRUE); + $stub_row = $this->createStubRow($values + $migration->getSourceConfiguration(), $source_ids); // 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 @@ -257,4 +249,23 @@ class MigrationLookup extends ProcessPluginBase implements ContainerFactoryPlugi } } + /** + * Create a stub row source for later import as stub data. + * + * This simple wrapper of the Row constructor allows sub-classing plugins to + * have more control over the row. + * + * @param array $values + * An array of values to add as properties on the object. + * @param array $source_ids + * An array containing the IDs of the source using the keys as the field + * names. + * + * @return \Drupal\migrate\Row + * The stub row. + */ + protected function createStubRow(array $values, array $source_ids) { + return new Row($values, $source_ids, TRUE); + } + }