Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /**
|
Chris@0
|
4 * @file
|
Chris@0
|
5 * Provides enhancements for implementing and managing migrations.
|
Chris@0
|
6 */
|
Chris@0
|
7
|
Chris@0
|
8 use Drupal\migrate\Plugin\MigrationInterface;
|
Chris@0
|
9 use Drupal\migrate\Plugin\MigrateSourceInterface;
|
Chris@0
|
10 use Drupal\migrate\Row;
|
Chris@0
|
11 use Drupal\migrate_plus\Entity\MigrationGroup;
|
Chris@0
|
12 use Drupal\migrate_plus\Event\MigrateEvents;
|
Chris@0
|
13 use Drupal\migrate_plus\Event\MigratePrepareRowEvent;
|
Chris@0
|
14
|
Chris@0
|
15 /**
|
Chris@0
|
16 * Implements hook_migration_plugins_alter().
|
Chris@0
|
17 */
|
Chris@0
|
18 function migrate_plus_migration_plugins_alter(array &$migrations) {
|
Chris@0
|
19 /** @var \Drupal\migrate\Plugin\MigrationInterface $migration */
|
Chris@0
|
20 foreach ($migrations as $id => $migration) {
|
Chris@0
|
21 if (empty($migration['migration_group'])) {
|
Chris@0
|
22 $migration['migration_group'] = 'default';
|
Chris@0
|
23 }
|
Chris@0
|
24 $group = MigrationGroup::load($migration['migration_group']);
|
Chris@0
|
25 if (empty($group)) {
|
Chris@0
|
26 // If the specified group does not exist, create it. Provide a little more
|
Chris@0
|
27 // for the 'default' group.
|
Chris@0
|
28 $group_properties = [];
|
Chris@0
|
29 $group_properties['id'] = $migration['migration_group'];
|
Chris@0
|
30 if ($migration['migration_group'] == 'default') {
|
Chris@0
|
31 $group_properties['label'] = 'Default';
|
Chris@0
|
32 $group_properties['description'] = 'A container for any migrations not explicitly assigned to a group.';
|
Chris@0
|
33 }
|
Chris@0
|
34 else {
|
Chris@0
|
35 $group_properties['label'] = $group_properties['id'];
|
Chris@0
|
36 $group_properties['description'] = '';
|
Chris@0
|
37 }
|
Chris@0
|
38 $group = MigrationGroup::create($group_properties);
|
Chris@0
|
39 $group->save();
|
Chris@0
|
40 }
|
Chris@0
|
41 $shared_configuration = $group->get('shared_configuration');
|
Chris@0
|
42 if (empty($shared_configuration)) {
|
Chris@0
|
43 continue;
|
Chris@0
|
44 }
|
Chris@0
|
45 foreach ($shared_configuration as $key => $group_value) {
|
Chris@0
|
46 $migration_value = $migration[$key];
|
Chris@0
|
47 // Where both the migration and the group provide arrays, replace
|
Chris@0
|
48 // recursively (so each key collision is resolved in favor of the
|
Chris@0
|
49 // migration).
|
Chris@0
|
50 if (is_array($migration_value) && is_array($group_value)) {
|
Chris@0
|
51 $merged_values = array_replace_recursive($group_value, $migration_value);
|
Chris@0
|
52 $migrations[$id][$key] = $merged_values;
|
Chris@0
|
53 }
|
Chris@0
|
54 // Where the group provides a value the migration doesn't, use the group
|
Chris@0
|
55 // value.
|
Chris@0
|
56 elseif (is_null($migration_value)) {
|
Chris@0
|
57 $migrations[$id][$key] = $group_value;
|
Chris@0
|
58 }
|
Chris@0
|
59 // Otherwise, the existing migration value overrides the group value.
|
Chris@0
|
60 }
|
Chris@0
|
61 }
|
Chris@0
|
62 }
|
Chris@0
|
63
|
Chris@0
|
64 /**
|
Chris@0
|
65 * Implements hook_migrate_prepare_row().
|
Chris@0
|
66 */
|
Chris@0
|
67 function migrate_plus_migrate_prepare_row(Row $row, MigrateSourceInterface $source, MigrationInterface $migration) {
|
Chris@0
|
68 \Drupal::service('event_dispatcher')->dispatch(MigrateEvents::PREPARE_ROW, new MigratePrepareRowEvent($row, $source, $migration));
|
Chris@0
|
69 }
|