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 if (empty($migration['migration_group'])) {
22 $migration['migration_group'] = 'default';
24 $group = MigrationGroup::load($migration['migration_group']);
26 // If the specified group does not exist, create it. Provide a little more
27 // for the 'default' group.
28 $group_properties = [];
29 $group_properties['id'] = $migration['migration_group'];
30 if ($migration['migration_group'] == 'default') {
31 $group_properties['label'] = 'Default';
32 $group_properties['description'] = 'A container for any migrations not explicitly assigned to a group.';
35 $group_properties['label'] = $group_properties['id'];
36 $group_properties['description'] = '';
38 $group = MigrationGroup::create($group_properties);
41 $shared_configuration = $group->get('shared_configuration');
42 if (empty($shared_configuration)) {
45 foreach ($shared_configuration as $key => $group_value) {
46 $migration_value = $migration[$key];
47 // Where both the migration and the group provide arrays, replace
48 // recursively (so each key collision is resolved in favor of the
50 if (is_array($migration_value) && is_array($group_value)) {
51 $merged_values = array_replace_recursive($group_value, $migration_value);
52 $migrations[$id][$key] = $merged_values;
54 // Where the group provides a value the migration doesn't, use the group
56 elseif (is_null($migration_value)) {
57 $migrations[$id][$key] = $group_value;
59 // Otherwise, the existing migration value overrides the group value.
65 * Implements hook_migrate_prepare_row().
67 function migrate_plus_migrate_prepare_row(Row $row, MigrateSourceInterface $source, MigrationInterface $migration) {
68 \Drupal::service('event_dispatcher')->dispatch(MigrateEvents::PREPARE_ROW, new MigratePrepareRowEvent($row, $source, $migration));