Drupal investigation

migrate_plus.module 2.6KB

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