ce7f5bee5ee1c800b89be7066eeea5e8c1597014
[yaffs-website] / web / modules / contrib / migrate_plus / migrate_plus.module
1 <?php
2
3 /**
4  * @file
5  * Provides enhancements for implementing and managing migrations.
6  */
7
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;
14
15 /**
16  * Implements hook_migration_plugins_alter().
17  */
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';
23     }
24     $group = MigrationGroup::load($migration['migration_group']);
25     if (empty($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.';
33       }
34       else {
35         $group_properties['label'] = $group_properties['id'];
36         $group_properties['description'] = '';
37       }
38       $group = MigrationGroup::create($group_properties);
39       $group->save();
40     }
41     $shared_configuration = $group->get('shared_configuration');
42     if (empty($shared_configuration)) {
43       continue;
44     }
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
49       // migration).
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;
53       }
54       // Where the group provides a value the migration doesn't, use the group
55       // value.
56       elseif (is_null($migration_value)) {
57         $migrations[$id][$key] = $group_value;
58       }
59       // Otherwise, the existing migration value overrides the group value.
60     }
61   }
62 }
63
64 /**
65  * Implements hook_migrate_prepare_row().
66  */
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));
69 }