Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / migrate_tools / src / Form / MigrationExecuteForm.php
1 <?php
2
3 namespace Drupal\migrate_tools\Form;
4
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;
12
13 /**
14  * This form is specifically for configuring process pipelines.
15  */
16 class MigrationExecuteForm extends FormBase {
17
18   /**
19    * Plugin manager for migration plugins.
20    *
21    * @var \Drupal\migrate\Plugin\MigrationPluginManagerInterface
22    */
23   protected $migrationPluginManager;
24
25   /**
26    * Constructs a new MigrationExecuteForm object.
27    *
28    * @param \Drupal\migrate\Plugin\MigrationPluginManagerInterface $migration_plugin_manager
29    *   The plugin manager for config entity-based migrations.
30    */
31   public function __construct(MigrationPluginManagerInterface $migration_plugin_manager) {
32     $this->migrationPluginManager = $migration_plugin_manager;
33   }
34
35   /**
36    * {@inheritdoc}
37    */
38   public static function create(ContainerInterface $container) {
39     return new static(
40       $container->get('plugin.manager.migration')
41     );
42   }
43
44   /**
45    * {@inheritdoc}
46    */
47   public function getFormId() {
48     return 'migration_execute_form';
49   }
50
51   /**
52    * {@inheritdoc}
53    */
54   public function buildForm(array $form, FormStateInterface $form_state) {
55
56     $form = [];
57
58     $form['operations'] = $this->migrateMigrateOperations();
59
60     return $form;
61   }
62
63   /**
64    * Get Operations.
65    */
66   private function migrateMigrateOperations() {
67     // Build the 'Update options' form.
68     $form = [
69       '#type' => 'fieldset',
70       '#title' => t('Operations'),
71     ];
72     $options = [
73       'import' => t('Import'),
74       'rollback' => t('Rollback'),
75       'stop' => t('Stop'),
76       'reset' => t('Reset'),
77     ];
78     $form['operation'] = [
79       '#type' => 'select',
80       '#title' => t('Choose an operation to run'),
81       '#options' => $options,
82       '#default_value' => 'import',
83       '#required' => TRUE,
84     ];
85     $form['submit'] = [
86       '#type' => 'submit',
87       '#value' => t('Execute'),
88     ];
89     $definitions = [];
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'),
97       '#list_type' => 'ul',
98       '#items' => $definitions,
99     ];
100
101     $form['options'] = [
102       '#type' => 'fieldset',
103       '#title' => t('Options'),
104       '#collapsible' => TRUE,
105       '#collapsed' => TRUE,
106     ];
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
112       new content'),
113     ];
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
119       completed.'),
120     ];
121     // @TODO: Limit is not working. Perhaps because of batch? See
122     // https://www.drupal.org/project/migrate_tools/issues/2924298.
123     return $form;
124   }
125
126   /**
127    * {@inheritdoc}
128    */
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.'));
132       return;
133     }
134   }
135
136   /**
137    * {@inheritdoc}
138    */
139   public function submitForm(array &$form, FormStateInterface $form_state) {
140
141     $operation = $form_state->getValue('operation');
142
143     if ($form_state->getValue('limit')) {
144       $limit = $form_state->getValue('limit');
145     }
146     else {
147       $limit = 0;
148     }
149
150     if ($form_state->getValue('update')) {
151       $update = $form_state->getValue('update');
152     }
153     else {
154       $update = 0;
155     }
156     if ($form_state->getValue('force')) {
157       $force = $form_state->getValue('force');
158     }
159     else {
160       $force = 0;
161     }
162
163     $migration = \Drupal::routeMatch()->getParameter('migration');
164     if ($migration) {
165       /** @var \Drupal\migrate\Plugin\MigrationInterface $migration_plugin */
166       $migration_plugin = $this->migrationPluginManager->createInstance($migration->id(), $migration->toArray());
167       $migrateMessage = new MigrateMessage();
168
169       switch ($operation) {
170         case 'import':
171
172           $options = [
173             'limit' => $limit,
174             'update' => $update,
175             'force' => $force,
176           ];
177
178           $executable = new MigrateBatchExecutable($migration_plugin, $migrateMessage, $options);
179           $executable->batchImport();
180
181           break;
182
183         case 'rollback':
184
185           $options = [
186             'limit' => $limit,
187             'update' => $update,
188             'force' => $force,
189           ];
190
191           $executable = new MigrateBatchExecutable($migration_plugin, $migrateMessage, $options);
192           $executable->rollback();
193
194           break;
195
196         case 'stop':
197
198           $migration_plugin->interruptMigration(MigrationInterface::RESULT_STOPPED);
199
200           break;
201
202         case 'reset':
203
204           $migration_plugin->setStatus(MigrationInterface::STATUS_IDLE);
205
206           break;
207
208       }
209     }
210   }
211
212 }