Migration Group Lookup process plugin
I wrote a custom process plugin that extends the core migration_lookup
class so that you can look up an ID from all the migrations in a particular group. We will be using this in farmOS. See relevant comment here: https://www.drupal.org/project/farm/issues/3176066#comment-13855692
Here is the code - I still need to figure out how to open a merge request on GitLab... :-)
src/Plugin/migrate/process/MigrationGroupLookup.php
:
<?php
namespace Drupal\migrate_plus\Plugin\migrate\process;
use Drupal\migrate\Plugin\migrate\process\MigrationLookup;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Row;
/**
* Looks up the value of a property based on a previous migration that is part
* of a specified migration group.
*
* @MigrateProcessPlugin(
* id = "migration_group_lookup"
* )
*
* This extends from the core migration_lookup process plugin, loads a list of
* all migrations in the specified migration_group, then passes that to the
* parent class, along with all other configuration keys.
*
* Example:
*
* @code
* process:
* uid:
* plugin: migration_group_lookup
* migration_group: users
* source: author
* @endcode
*/
class MigrationGroupLookup extends MigrationLookup {
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
// Get the migration group ID from the process configuration.
$lookup_migration_group_id = $this->configuration['migration_group'];
// Load all migrations.
$manager = \Drupal::service('plugin.manager.migration');
$migrations = $manager->createInstances([]);
// Filter by group.
$group_migrations = [];
foreach ($migrations as $id => $migration) {
$definition = $migration->getPluginDefinition();
if ($definition['migration_group'] == $lookup_migration_group_id) {
$group_migrations[] = $id;
}
}
// Set the migration configuration and delegate processing to the parent
// MigrationLookup::transform() method.
$this->configuration['migration'] = $group_migrations;
return parent::transform($value, $migrate_executable, $row, $destination_property);
}
}