102 lines
3.8 KiB
PHP
102 lines
3.8 KiB
PHP
<?php
|
|
|
|
/**
|
|
* @file Module file for Migrate Tools.
|
|
*/
|
|
|
|
declare(strict_types = 1);
|
|
|
|
/**
|
|
* @file
|
|
* Provides tools for implementing and managing migrations.
|
|
*/
|
|
|
|
use Drupal\Core\StringTranslation\TranslatableMarkup;
|
|
use Drupal\migrate\Plugin\MigrateSourceInterface;
|
|
use Drupal\migrate\Plugin\MigrationInterface;
|
|
use Drupal\migrate\Row;
|
|
use Drupal\migrate_tools\Controller\MigrationGroupListBuilder;
|
|
use Drupal\migrate_tools\Controller\MigrationListBuilder;
|
|
use Drupal\migrate_tools\Form\MigrationDeleteForm;
|
|
use Drupal\migrate_tools\Form\MigrationEditForm;
|
|
use Drupal\migrate_tools\Form\MigrationGroupAddForm;
|
|
use Drupal\migrate_tools\Form\MigrationGroupDeleteForm;
|
|
use Drupal\migrate_tools\Form\MigrationGroupEditForm;
|
|
use Drupal\migrate_tools\MigrateTools;
|
|
|
|
/**
|
|
* Implements hook_entity_type_build().
|
|
*/
|
|
function migrate_tools_entity_type_build(array &$entity_types): void {
|
|
// Inject our UI into the general migration and migration group config
|
|
// entities.
|
|
/** @var \Drupal\Core\Config\Entity\ConfigEntityType[] $entity_types */
|
|
if (array_key_exists('migration', $entity_types)) {
|
|
$entity_types['migration']
|
|
->set('admin_permission', 'administer migrations')
|
|
->setHandlerClass('list_builder', MigrationListBuilder::class)
|
|
->setFormClass('edit', MigrationEditForm::class)
|
|
->setFormClass('delete', MigrationDeleteForm::class)
|
|
->setLinkTemplate('list-form', '/admin/structure/migrate/manage/{migration_group}/migrations');
|
|
}
|
|
|
|
if (array_key_exists('migration_group', $entity_types)) {
|
|
$entity_types['migration_group']
|
|
->set('admin_permission', 'administer migrations')
|
|
->setHandlerClass('list_builder', MigrationGroupListBuilder::class)
|
|
->setFormClass('add', MigrationGroupAddForm::class)
|
|
->setFormClass('edit', MigrationGroupEditForm::class)
|
|
->setFormClass('delete', MigrationGroupDeleteForm::class)
|
|
->setLinkTemplate('edit-form', '/admin/structure/migrate/manage/{migration_group}')
|
|
->setLinkTemplate('delete-form', '/admin/structure/migrate/manage/{migration_group}/delete');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implements hook_menu_links_discovered_alter().
|
|
*/
|
|
function migrate_tools_menu_links_discovered_alter(&$links): void {
|
|
if (\Drupal::moduleHandler()->moduleExists('migrate_plus')) {
|
|
$links['migrate_tools.menu'] = [
|
|
'title' => 'Migrations',
|
|
'route_name' => 'entity.migration_group.list',
|
|
'description' => new TranslatableMarkup('Manage migration processes.'),
|
|
'parent' => 'system.admin_structure',
|
|
];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implements hook_migration_plugins_alter().
|
|
*/
|
|
function migrate_tools_migration_plugins_alter(array &$migrations): void {
|
|
foreach (array_keys($migrations) as $id) {
|
|
// Process all includes.
|
|
if (!array_key_exists('include', $migrations[$id])) {
|
|
continue;
|
|
}
|
|
\Drupal::service('migrate_tools.shared_config_include_handler')->include($migrations[$id]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implements hook_migrate_prepare_row().
|
|
*
|
|
* @throws \Exception
|
|
*
|
|
* @see \Drupal\migrate_tools\EventSubscriber\MigrationImportSync
|
|
* @see \Drupal\migrate\Plugin\migrate\source\SourcePluginBase::next()
|
|
*/
|
|
function migrate_tools_migrate_prepare_row(Row $row, MigrateSourceInterface $source, MigrationInterface $migration): void {
|
|
/** @var MigrateTools $migrateTools */
|
|
$migrateTools = \Drupal::service('migrate_tools.migrate_tools');
|
|
// Act on migrations that have a Sync source, and that are currently in the
|
|
// phase of Syncing their IDs.
|
|
if (!empty($migration->syncSource) && $migrateTools->isMigrationSyncing($migration->getPluginId())) {
|
|
// Keep track of all source rows here, as SourcePluginBase::next() might
|
|
// skip some rows, and we need them all to detect missing items in source to
|
|
// delete in destination.
|
|
$migrateTools->addToSyncSourceIds($migration->getPluginId(), $row->getSourceIdValues());
|
|
}
|
|
}
|