5 * Provides enhancements for implementing and managing migrations.
8 use Drupal\migrate\Plugin\MigrationInterface;
9 use Drupal\migrate\Plugin\MigrateSourceInterface;
10 use Drupal\migrate\Row;
11 use Drupal\migrate_plus\Entity\MigrationGroup;
12 use Drupal\migrate_plus\Event\MigrateEvents;
13 use Drupal\migrate_plus\Event\MigratePrepareRowEvent;
16 * Implements hook_migration_plugins_alter().
18 function migrate_plus_migration_plugins_alter(array &$migrations) {
19 /** @var \Drupal\migrate\Plugin\MigrationInterface $migration */
20 foreach ($migrations as $id => $migration) {
21 // Add the default class where empty.
22 if (empty($migration['class'])) {
23 $migrations[$id]['class'] = 'Drupal\migrate\Plugin\Migration';
26 // For derived configuration entity-based migrations, strip the deriver
27 // prefix so we can reference migrations by the IDs they specify (i.e.,
28 // the migration that specifies "id: temp" can be referenced as "temp"
29 // rather than "migration_config_deriver:temp").
30 $prefix = 'migration_config_deriver:';
31 if (strpos($id, $prefix) === 0) {
32 $new_id = substr($id, strlen($prefix));
33 $migrations[$new_id] = $migrations[$id];
34 unset($migrations[$id]);
38 // Integrate shared group configuration into the migration.
39 if (empty($migration['migration_group'])) {
40 $migration['migration_group'] = 'default';
42 $group = MigrationGroup::load($migration['migration_group']);
44 // If the specified group does not exist, create it. Provide a little more
45 // for the 'default' group.
46 $group_properties = [];
47 $group_properties['id'] = $migration['migration_group'];
48 if ($migration['migration_group'] == 'default') {
49 $group_properties['label'] = 'Default';
50 $group_properties['description'] = 'A container for any migrations not explicitly assigned to a group.';
53 $group_properties['label'] = $group_properties['id'];
54 $group_properties['description'] = '';
56 $group = MigrationGroup::create($group_properties);
59 $shared_configuration = $group->get('shared_configuration');
60 if (empty($shared_configuration)) {
63 foreach ($shared_configuration as $key => $group_value) {
64 $migration_value = $migration[$key];
65 // Where both the migration and the group provide arrays, replace
66 // recursively (so each key collision is resolved in favor of the
68 if (is_array($migration_value) && is_array($group_value)) {
69 $merged_values = array_replace_recursive($group_value, $migration_value);
70 $migrations[$id][$key] = $merged_values;
72 // Where the group provides a value the migration doesn't, use the group
74 elseif (is_null($migration_value)) {
75 $migrations[$id][$key] = $group_value;
77 // Otherwise, the existing migration value overrides the group value.
83 * Implements hook_migrate_prepare_row().
85 function migrate_plus_migrate_prepare_row(Row $row, MigrateSourceInterface $source, MigrationInterface $migration) {
86 \Drupal::service('event_dispatcher')->dispatch(MigrateEvents::PREPARE_ROW, new MigratePrepareRowEvent($row, $source, $migration));