Skip to content
Snippets Groups Projects
Commit 6167e75f authored by Ivan Doroshenko's avatar Ivan Doroshenko Committed by Lucas Hedding
Browse files

Issue #3217657 by welly, heddn, Matroskeen: Allow developers to expose more...

Issue #3217657 by welly, heddn, Matroskeen: Allow developers to expose more configuration in the migrate form
parent 8ca8bc7d
No related branches found
No related tags found
1 merge request!2Issue #3217657: Allow developers to expose more configuration in the migrate form
Pipeline #228174 passed with warnings
......@@ -29,3 +29,4 @@ include:
# _CURL_TEMPLATES_REF: 'main'
variables:
_SHOW_ENVIRONMENT_VARIABLES: '1'
_TARGET_CORE: $CORE_STABLE
......@@ -8,13 +8,13 @@ use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\migrate\Plugin\Migration;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Plugin\MigrationPluginManager;
use Drupal\migrate_plus\Plugin\migrate_plus\data_parser\Json;
use Drupal\migrate_plus\Plugin\migrate_plus\data_parser\Xml;
use Drupal\migrate_source_csv\Plugin\migrate\source\CSV;
use Drupal\migrate_source_ui\StubMigrationMessage;
use Drupal\migrate_source_ui\MigrateBatchExecutable;
use Drupal\migrate_tools\MigrateBatchExecutable;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\migrate\Plugin\MigrationPluginManager;
/**
* Contribute form.
......@@ -179,16 +179,28 @@ class MigrateSourceUiForm extends FormBase {
$this->messenger()->addWarning($this->t('Migration @id reset to Idle', ['@id' => $migration_id]));
}
$executable = new MigrateBatchExecutable($migration, new StubMigrationMessage(), $this->getBatchOptions($form, $form_state));
$executable->batchImport();
}
/**
* Prepares an array of migrate batch options.
*/
protected function getBatchOptions($form, FormStateInterface $form_state) {
$options = [
'file_path' => $form_state->getValue('file_path'),
'configuration' => [
'source' => [
'path' => $form_state->getValue('file_path'),
],
],
];
// Force updates or not.
if ($form_state->getValue('update_existing_records')) {
$options['update'] = 1;
}
$executable = new MigrateBatchExecutable($migration, new StubMigrationMessage(), $options);
$executable->batchImport();
return $options;
}
/**
......
<?php
namespace Drupal\migrate_source_ui;
use Drupal\migrate\MigrateMessageInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate_tools\MigrateBatchExecutable as BaseMigrateBatchExecutable;
/**
* Defines a migrate executable class for batch migrations through UI.
*/
class MigrateBatchExecutable extends BaseMigrateBatchExecutable {
/**
* The file path.
*
* @var string
*/
protected $filePath;
/**
* {@inheritdoc}
*/
public function __construct(MigrationInterface $migration, MigrateMessageInterface $message, array $options = []) {
parent::__construct($migration, $message, $options);
$this->filePath = $options['file_path'];
}
/**
* Helper to generate the batch operations for importing migrations.
*
* @param \Drupal\migrate\Plugin\MigrationInterface[] $migrations
* The migrations.
* @param string $operation
* The batch operation to perform.
* @param array $options
* The migration options.
*
* @return array
* The batch operations to perform.
*/
protected function batchOperations(array $migrations, string $operation, array $options = []): array {
$operations = [];
foreach ($migrations as $migration) {
if (!empty($options['update'])) {
$migration->getIdMap()->prepareUpdate();
}
if (!empty($options['force'])) {
$migration->set('requirements', []);
}
else {
$dependencies = $migration->getMigrationDependencies();
if (!empty($dependencies['required'])) {
$required_migrations = $this->migrationPluginManager->createInstances($dependencies['required']);
// For dependent migrations will need to be migrate all items.
$operations += $this->batchOperations($required_migrations, $operation, [
'limit' => 0,
'update' => $options['update'],
'force' => $options['force'],
]);
}
}
$operations[] = [
[get_class($this), 'batchProcessImport'],
[$migration->id(), $options + ['file_path' => $this->filePath]],
];
}
return $operations;
}
/**
* Batch 'operation' callback.
*
* @param string $migration_id
* The migration id.
* @param array $options
* The batch executable options.
* @param array|\DrushBatchContext $context
* The sandbox context.
*/
public static function batchProcessImport(string $migration_id, array $options, &$context): void {
if (empty($context['sandbox'])) {
$context['finished'] = 0;
$context['sandbox'] = [];
$context['sandbox']['total'] = 0;
$context['sandbox']['counter'] = 0;
$context['sandbox']['batch_limit'] = 0;
$context['sandbox']['operation'] = MigrateBatchExecutable::BATCH_IMPORT;
}
// Prepare the migration executable.
$message = new StubMigrationMessage();
$definition = \Drupal::getContainer()->get('plugin.manager.migration')->getDefinition($migration_id);
// Override the file path.
$definition['source']['path'] = $options['file_path'];
/** @var \Drupal\migrate\Plugin\MigrationInterface $migration */
$migration = \Drupal::getContainer()->get('plugin.manager.migration')->createStubMigration($definition);
$executable = new MigrateBatchExecutable($migration, $message, $options);
if (empty($context['sandbox']['total'])) {
$context['sandbox']['total'] = $executable->getSource()->count();
$context['sandbox']['batch_limit'] = $executable->calculateBatchLimit($context);
$context['results'][$migration->id()] = [
'@numitems' => 0,
'@created' => 0,
'@updated' => 0,
'@failures' => 0,
'@ignored' => 0,
'@name' => $migration->id(),
];
}
// Every iteration, we reset out batch counter.
$context['sandbox']['batch_counter'] = 0;
// Make sure we know our batch context.
$executable->setBatchContext($context);
// Do the import.
$result = $executable->import();
// Store the result; will need to combine the results of all our iterations.
$context['results'][$migration->id()] = [
'@numitems' => $context['results'][$migration->id()]['@numitems'] + $executable->getProcessedCount(),
'@created' => $context['results'][$migration->id()]['@created'] + $executable->getCreatedCount(),
'@updated' => $context['results'][$migration->id()]['@updated'] + $executable->getUpdatedCount(),
'@failures' => $context['results'][$migration->id()]['@failures'] + $executable->getFailedCount(),
'@ignored' => $context['results'][$migration->id()]['@ignored'] + $executable->getIgnoredCount(),
'@name' => $migration->id(),
];
// Do some housekeeping.
if (
$result != MigrationInterface::RESULT_INCOMPLETE
) {
$context['finished'] = 1;
}
else {
$context['sandbox']['counter'] = $context['results'][$migration->id()]['@numitems'];
if ($context['sandbox']['counter'] <= $context['sandbox']['total']) {
$context['finished'] = ((float) $context['sandbox']['counter'] / (float) $context['sandbox']['total']);
$context['message'] = t('Importing %migration (@percent%).', [
'%migration' => $migration->label(),
'@percent' => (int) ($context['finished'] * 100),
]);
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment