3 namespace Drupal\migrate_tools\Form;
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\Core\Form\FormBase;
7 use Drupal\migrate\MigrateMessage;
8 use Drupal\migrate\Plugin\MigrationInterface;
9 use Drupal\migrate\Plugin\MigrationPluginManagerInterface;
10 use Drupal\migrate_tools\MigrateBatchExecutable;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
14 * This form is specifically for configuring process pipelines.
16 class MigrationExecuteForm extends FormBase {
19 * Plugin manager for migration plugins.
21 * @var \Drupal\migrate\Plugin\MigrationPluginManagerInterface
23 protected $migrationPluginManager;
26 * Constructs a new MigrationExecuteForm object.
28 * @param \Drupal\migrate\Plugin\MigrationPluginManagerInterface $migration_plugin_manager
29 * The plugin manager for config entity-based migrations.
31 public function __construct(MigrationPluginManagerInterface $migration_plugin_manager) {
32 $this->migrationPluginManager = $migration_plugin_manager;
38 public static function create(ContainerInterface $container) {
40 $container->get('plugin.manager.migration')
47 public function getFormId() {
48 return 'migration_execute_form';
54 public function buildForm(array $form, FormStateInterface $form_state) {
58 $form['operations'] = $this->migrateMigrateOperations();
66 private function migrateMigrateOperations() {
67 // Build the 'Update options' form.
69 '#type' => 'fieldset',
70 '#title' => t('Operations'),
73 'import' => t('Import'),
74 'rollback' => t('Rollback'),
76 'reset' => t('Reset'),
78 $form['operation'] = [
80 '#title' => t('Choose an operation to run'),
81 '#options' => $options,
82 '#default_value' => 'import',
87 '#value' => t('Execute'),
90 $definitions[] = $this->t('Import: Imports all previously unprocessed records from the source, plus any records marked for update, into destination Drupal objects.');
91 $definitions[] = $this->t('Rollback: Deletes all Drupal objects created by the import.');
92 $definitions[] = $this->t('Stop: Cleanly interrupts any import or rollback processes that may currently be running.');
93 $definitions[] = $this->t('Reset: Sometimes a process may fail to stop cleanly, and be left stuck in an Importing or Rolling Back status. Choose Reset to clear the status and permit other operations to proceed.');
94 $form['definitions'] = [
95 '#theme' => 'item_list',
96 '#title' => $this->t('Definitions'),
98 '#items' => $definitions,
102 '#type' => 'fieldset',
103 '#title' => t('Options'),
104 '#collapsible' => TRUE,
105 '#collapsed' => TRUE,
107 $form['options']['update'] = [
108 '#type' => 'checkbox',
109 '#title' => t('Update'),
110 '#description' => t('Check this box to update all previously-imported content
111 in addition to importing new content. Leave unchecked to only import
114 $form['options']['force'] = [
115 '#type' => 'checkbox',
116 '#title' => t('Ignore dependencies'),
117 '#description' => t('Check this box to ignore dependencies when running imports
118 - all tasks will run whether or not their dependent tasks have
121 // @TODO: Limit is not working. Perhaps because of batch? See
122 // https://www.drupal.org/project/migrate_tools/issues/2924298.
129 public function validateForm(array &$form, FormStateInterface $form_state) {
130 if (empty($form_state->getValue('operation'))) {
131 $form_state->setErrorByName('operation', $this->t('Please select an operation.'));
139 public function submitForm(array &$form, FormStateInterface $form_state) {
141 $operation = $form_state->getValue('operation');
143 if ($form_state->getValue('limit')) {
144 $limit = $form_state->getValue('limit');
150 if ($form_state->getValue('update')) {
151 $update = $form_state->getValue('update');
156 if ($form_state->getValue('force')) {
157 $force = $form_state->getValue('force');
163 $migration = \Drupal::routeMatch()->getParameter('migration');
165 /** @var \Drupal\migrate\Plugin\MigrationInterface $migration_plugin */
166 $migration_plugin = $this->migrationPluginManager->createInstance($migration->id(), $migration->toArray());
167 $migrateMessage = new MigrateMessage();
169 switch ($operation) {
178 $executable = new MigrateBatchExecutable($migration_plugin, $migrateMessage, $options);
179 $executable->batchImport();
191 $executable = new MigrateBatchExecutable($migration_plugin, $migrateMessage, $options);
192 $executable->rollback();
198 $migration_plugin->interruptMigration(MigrationInterface::RESULT_STOPPED);
204 $migration_plugin->setStatus(MigrationInterface::STATUS_IDLE);