Drupal investigation

migrate_plus.module 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. if (empty($migration['migration_group'])) {
  23. $migration['migration_group'] = 'default';
  24. }
  25. $group = MigrationGroup::load($migration['migration_group']);
  26. if (empty($group)) {
  27. // If the specified group does not exist, create it. Provide a little more
  28. // for the 'default' group.
  29. $group_properties = [];
  30. $group_properties['id'] = $migration['migration_group'];
  31. if ($migration['migration_group'] == 'default') {
  32. $group_properties['label'] = 'Default';
  33. $group_properties['description'] = 'A container for any migrations not explicitly assigned to a group.';
  34. }
  35. else {
  36. $group_properties['label'] = $group_properties['id'];
  37. $group_properties['description'] = '';
  38. }
  39. $group = MigrationGroup::create($group_properties);
  40. $group->save();
  41. }
  42. $shared_configuration = $group->get('shared_configuration');
  43. if (empty($shared_configuration)) {
  44. continue;
  45. }
  46. foreach ($shared_configuration as $key => $group_value) {
  47. $migration_value = $migration[$key];
  48. // Where both the migration and the group provide arrays, replace
  49. // recursively (so each key collision is resolved in favor of the
  50. // migration).
  51. if (is_array($migration_value) && is_array($group_value)) {
  52. $merged_values = array_replace_recursive($group_value, $migration_value);
  53. $migrations[$id][$key] = $merged_values;
  54. }
  55. // Where the group provides a value the migration doesn't, use the group
  56. // value.
  57. elseif (is_null($migration_value)) {
  58. $migrations[$id][$key] = $group_value;
  59. }
  60. // Otherwise, the existing migration value overrides the group value.
  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. }