migrate_plus.module 3.3 KB

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