Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / migrate_tools / src / Controller / MigrationController.php
1 <?php
2
3 namespace Drupal\migrate_tools\Controller;
4
5 use Drupal\Core\Controller\ControllerBase;
6 use Drupal\Component\Utility\Xss;
7 use Drupal\Component\Utility\Html;
8 use Drupal\Core\Routing\CurrentRouteMatch;
9 use Drupal\migrate\Plugin\MigrationPluginManagerInterface;
10 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
11 use Drupal\migrate_plus\Entity\MigrationGroupInterface;
12 use Drupal\migrate_plus\Entity\MigrationInterface;
13 use Drupal\migrate_tools\MigrateBatchExecutable;
14 use Symfony\Component\DependencyInjection\ContainerInterface;
15 use Drupal\Core\Url;
16 use Drupal\migrate\MigrateMessage;
17
18 /**
19  * Returns responses for migrate_tools migration view routes.
20  */
21 class MigrationController extends ControllerBase implements ContainerInjectionInterface {
22
23   /**
24    * Plugin manager for migration plugins.
25    *
26    * @var \Drupal\migrate\Plugin\MigrationPluginManagerInterface
27    */
28   protected $migrationPluginManager;
29
30   /**
31    * The current route match.
32    *
33    * @var \Drupal\Core\Routing\CurrentRouteMatch
34    */
35   protected $currentRouteMatch;
36
37   /**
38    * Constructs a new MigrationController object.
39    *
40    * @param \Drupal\migrate\Plugin\MigrationPluginManagerInterface $migration_plugin_manager
41    *   The plugin manager for config entity-based migrations.
42    * @param \Drupal\Core\Routing\CurrentRouteMatch $currentRouteMatch
43    *   The current route match.
44    */
45   public function __construct(MigrationPluginManagerInterface $migration_plugin_manager, CurrentRouteMatch $currentRouteMatch) {
46     $this->migrationPluginManager = $migration_plugin_manager;
47     $this->currentRouteMatch = $currentRouteMatch;
48   }
49
50   /**
51    * {@inheritdoc}
52    */
53   public static function create(ContainerInterface $container) {
54     return new static(
55       $container->get('plugin.manager.migration'),
56       $container->get('current_route_match')
57     );
58   }
59
60   /**
61    * Displays an overview of a migration entity.
62    *
63    * @param \Drupal\migrate_plus\Entity\MigrationGroupInterface $migration_group
64    *   The migration group.
65    * @param \Drupal\migrate_plus\Entity\MigrationInterface $migration
66    *   The $migration.
67    *
68    * @return array
69    *   A render array as expected by drupal_render().
70    */
71   public function overview(MigrationGroupInterface $migration_group, MigrationInterface $migration) {
72     $build['overview'] = [
73       '#type' => 'fieldset',
74       '#title' => $this->t('Overview'),
75     ];
76
77     $build['overview']['group'] = [
78       '#title' => $this->t('Group:'),
79       '#markup' => Xss::filterAdmin($migration_group->label()),
80       '#type' => 'item',
81     ];
82
83     $build['overview']['description'] = [
84       '#title' => $this->t('Description:'),
85       '#markup' => Xss::filterAdmin($migration->label()),
86       '#type' => 'item',
87     ];
88     $migration_plugin = $this->migrationPluginManager->createInstance($migration->id(), $migration->toArray());
89     $migration_dependencies = $migration_plugin->getMigrationDependencies();
90     if (!empty($migration_dependencies['required'])) {
91       $build['overview']['dependencies'] = [
92         '#title' => $this->t('Migration Dependencies') ,
93         '#markup' => Xss::filterAdmin(implode(', ', $migration_dependencies['required'])),
94         '#type' => 'item',
95       ];
96     }
97     if (!empty($migration_dependencies['optional'])) {
98       $build['overview']['soft_dependencies'] = [
99         '#title' => $this->t('Soft Migration Dependencies'),
100         '#markup' => Xss::filterAdmin(implode(', ', $migration_dependencies['optional'])),
101         '#type' => 'item',
102       ];
103     }
104
105     return $build;
106   }
107
108   /**
109    * Display source information of a migration entity.
110    *
111    * @param \Drupal\migrate_plus\Entity\MigrationGroupInterface $migration_group
112    *   The migration group.
113    * @param \Drupal\migrate_plus\Entity\MigrationInterface $migration
114    *   The $migration.
115    *
116    * @return array
117    *   A render array as expected by drupal_render().
118    */
119   public function source(MigrationGroupInterface $migration_group, MigrationInterface $migration) {
120     // Source field information.
121     $build['source'] = [
122       '#type' => 'fieldset',
123       '#title' => $this->t('Source'),
124       '#group' => 'detail',
125       '#description' => $this->t('<p>These are the fields available from the source of this migration task. The machine names listed here may be used as sources in the process pipeline.</p>'),
126       '#attributes' => [
127         'id' => 'migration-detail-source',
128       ],
129     ];
130     $migration_plugin = $this->migrationPluginManager->createInstance($migration->id(), $migration->toArray());
131     $source = $migration_plugin->getSourcePlugin();
132     $build['source']['query'] = [
133       '#type' => 'item',
134       '#title' => $this->t('Query'),
135       '#markup' => '<pre>' . Xss::filterAdmin($source) . '</pre>',
136     ];
137     $header = [$this->t('Machine name'), $this->t('Description')];
138     $rows = [];
139     foreach ($source->fields($migration_plugin) as $machine_name => $description) {
140       $rows[] = [
141         ['data' => Html::escape($machine_name)],
142         ['data' => Xss::filterAdmin($description)],
143       ];
144     }
145
146     $build['source']['fields'] = [
147       '#theme' => 'table',
148       '#header' => $header,
149       '#rows' => $rows,
150       '#empty' => $this->t('No fields'),
151     ];
152
153     return $build;
154   }
155
156   /**
157    * Run a migration.
158    *
159    * @param \Drupal\migrate_plus\Entity\MigrationGroupInterface $migration_group
160    *   The migration group.
161    * @param \Drupal\migrate_plus\Entity\MigrationInterface $migration
162    *   The $migration.
163    *
164    * @return array
165    *   A render array as expected by drupal_render().
166    */
167   public function run(MigrationGroupInterface $migration_group, MigrationInterface $migration) {
168     $migrateMessage = new MigrateMessage();
169     $options = [];
170
171     $migration_plugin = $this->migrationPluginManager->createInstance($migration->id(), $migration->toArray());
172     $executable = new MigrateBatchExecutable($migration_plugin, $migrateMessage, $options);
173     $executable->batchImport();
174
175     $migration_group = $this->currentRouteMatch->getParameter('migration_group');
176     $route_parameters = [
177       'migration_group' => $migration_group,
178       'migration' => $migration->id(),
179     ];
180     return batch_process(Url::fromRoute('entity.migration.process', $route_parameters));
181   }
182
183   /**
184    * Display process information of a migration entity.
185    *
186    * @param \Drupal\migrate_plus\Entity\MigrationGroupInterface $migration_group
187    *   The migration group.
188    * @param \Drupal\migrate_plus\Entity\MigrationInterface $migration
189    *   The $migration.
190    *
191    * @return array
192    *   A render array as expected by drupal_render().
193    */
194   public function process(MigrationGroupInterface $migration_group, MigrationInterface $migration) {
195     $migration_plugin = $this->migrationPluginManager->createInstance($migration->id(), $migration->toArray());
196
197     // Process information.
198     $build['process'] = [
199       '#type' => 'fieldset',
200       '#title' => $this->t('Process'),
201     ];
202
203     $header = [
204       $this->t('Destination'),
205       $this->t('Source'),
206       $this->t('Process plugin'),
207       $this->t('Default'),
208     ];
209     $rows = [];
210     foreach ($migration_plugin->getProcess() as $destination_id => $process_line) {
211       $row = [];
212       $row[] = ['data' => Html::escape($destination_id)];
213       if (isset($process_line[0]['source'])) {
214         $row[] = ['data' => Xss::filterAdmin($process_line[0]['source'])];
215       }
216       else {
217         $row[] = '';
218       }
219       if (isset($process_line[0]['plugin'])) {
220         $row[] = ['data' => Xss::filterAdmin($process_line[0]['plugin'])];
221       }
222       else {
223         $row[] = '';
224       }
225       if (isset($process_line[0]['default_value'])) {
226         $row[] = ['data' => Xss::filterAdmin($process_line[0]['default_value'])];
227       }
228       else {
229         $row[] = '';
230       }
231       $rows[] = $row;
232     }
233
234     $build['process']['fields'] = [
235       '#theme' => 'table',
236       '#header' => $header,
237       '#rows' => $rows,
238       '#empty' => $this->t('No process defined.'),
239     ];
240
241     $build['process']['run'] = [
242       '#type' => 'link',
243       '#title' => $this->t('Run'),
244       '#url' => Url::fromRoute('entity.migration.process.run', ['migration_group' => $migration_group->id(), 'migration' => $migration->id()]),
245     ];
246
247     return $build;
248   }
249
250   /**
251    * Displays destination information of a migration entity.
252    *
253    * @param \Drupal\migrate_plus\Entity\MigrationGroupInterface $migration_group
254    *   The migration group.
255    * @param \Drupal\migrate_plus\Entity\MigrationInterface $migration
256    *   The $migration.
257    *
258    * @return array
259    *   A render array as expected by drupal_render().
260    */
261   public function destination(MigrationGroupInterface $migration_group, MigrationInterface $migration) {
262     $migration_plugin = $this->migrationPluginManager->createInstance($migration->id(), $migration->toArray());
263
264     // Destination field information.
265     $build['destination'] = [
266       '#type' => 'fieldset',
267       '#title' => $this->t('Destination'),
268       '#group' => 'detail',
269       '#description' => $this->t('<p>These are the fields available in the destination plugin of this migration task. The machine names are those available to be used as the keys in the process pipeline.</p>'),
270       '#attributes' => [
271         'id' => 'migration-detail-destination',
272       ],
273     ];
274     $destination = $migration_plugin->getDestinationPlugin();
275     $build['destination']['type'] = [
276       '#type' => 'item',
277       '#title' => $this->t('Type'),
278       '#markup' => Xss::filterAdmin($destination->getPluginId()),
279     ];
280     $header = [$this->t('Machine name'), $this->t('Description')];
281     $rows = [];
282     $destination_fields = $destination->fields() ?: [];
283     foreach ($destination_fields as $machine_name => $description) {
284       $rows[] = [
285         ['data' => Html::escape($machine_name)],
286         ['data' => Xss::filterAdmin($description)],
287       ];
288     }
289
290     $build['destination']['fields'] = [
291       '#theme' => 'table',
292       '#header' => $header,
293       '#rows' => $rows,
294       '#empty' => $this->t('No fields'),
295     ];
296
297     return $build;
298   }
299
300 }