Mercurial > hg > isophonics-drupal-site
diff modules/contrib/migrate_plus/migrate_plus.module @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/modules/contrib/migrate_plus/migrate_plus.module Wed Nov 29 16:09:58 2017 +0000 @@ -0,0 +1,69 @@ +<?php + +/** + * @file + * Provides enhancements for implementing and managing migrations. + */ + +use Drupal\migrate\Plugin\MigrationInterface; +use Drupal\migrate\Plugin\MigrateSourceInterface; +use Drupal\migrate\Row; +use Drupal\migrate_plus\Entity\MigrationGroup; +use Drupal\migrate_plus\Event\MigrateEvents; +use Drupal\migrate_plus\Event\MigratePrepareRowEvent; + +/** + * Implements hook_migration_plugins_alter(). + */ +function migrate_plus_migration_plugins_alter(array &$migrations) { + /** @var \Drupal\migrate\Plugin\MigrationInterface $migration */ + foreach ($migrations as $id => $migration) { + if (empty($migration['migration_group'])) { + $migration['migration_group'] = 'default'; + } + $group = MigrationGroup::load($migration['migration_group']); + if (empty($group)) { + // If the specified group does not exist, create it. Provide a little more + // for the 'default' group. + $group_properties = []; + $group_properties['id'] = $migration['migration_group']; + if ($migration['migration_group'] == 'default') { + $group_properties['label'] = 'Default'; + $group_properties['description'] = 'A container for any migrations not explicitly assigned to a group.'; + } + else { + $group_properties['label'] = $group_properties['id']; + $group_properties['description'] = ''; + } + $group = MigrationGroup::create($group_properties); + $group->save(); + } + $shared_configuration = $group->get('shared_configuration'); + if (empty($shared_configuration)) { + continue; + } + foreach ($shared_configuration as $key => $group_value) { + $migration_value = $migration[$key]; + // Where both the migration and the group provide arrays, replace + // recursively (so each key collision is resolved in favor of the + // migration). + if (is_array($migration_value) && is_array($group_value)) { + $merged_values = array_replace_recursive($group_value, $migration_value); + $migrations[$id][$key] = $merged_values; + } + // Where the group provides a value the migration doesn't, use the group + // value. + elseif (is_null($migration_value)) { + $migrations[$id][$key] = $group_value; + } + // Otherwise, the existing migration value overrides the group value. + } + } +} + +/** + * Implements hook_migrate_prepare_row(). + */ +function migrate_plus_migrate_prepare_row(Row $row, MigrateSourceInterface $source, MigrationInterface $migration) { + \Drupal::service('event_dispatcher')->dispatch(MigrateEvents::PREPARE_ROW, new MigratePrepareRowEvent($row, $source, $migration)); +}