Version 1
[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\migrate\Plugin\MigrationInterface;
7 use Drupal\Component\Utility\Xss;
8 use Drupal\Component\Utility\Html;
9 use Drupal\migrate_plus\Plugin\MigrationConfigEntityPluginManager;
10 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12
13 /**
14  * Returns responses for migrate_tools migration view routes.
15  */
16 class MigrationController extends ControllerBase implements ContainerInjectionInterface {
17
18   /**
19    * Plugin manager for migration plugins.
20    *
21    * @var \Drupal\migrate_plus\Plugin\MigrationConfigEntityPluginManager
22    */
23   protected $migrationConfigEntityPluginManager;
24
25   /**
26    * Constructs a new MigrationController object.
27    *
28    * @param \Drupal\migrate_plus\Plugin\MigrationConfigEntityPluginManager $migration_config_entity_plugin_manager
29    *   The plugin manager for config entity-based migrations.
30    */
31   public function __construct(MigrationConfigEntityPluginManager $migration_config_entity_plugin_manager) {
32     $this->migrationConfigEntityPluginManager = $migration_config_entity_plugin_manager;
33   }
34
35   /**
36    * {@inheritdoc}
37    */
38   public static function create(ContainerInterface $container) {
39     return new static(
40       $container->get('plugin.manager.config_entity_migration')
41     );
42   }
43
44   /**
45    * Displays an overview of a migration entity.
46    *
47    * @param string $migration_group
48    *   Machine name of the migration's group.
49    * @param string $migration
50    *   Machine name of the migration.
51    *
52    * @return array
53    *   A render array as expected by drupal_render().
54    */
55   public function overview($migration_group, $migration) {
56
57     /** @var MigrationInterface $migration */
58     $migration = $this->migrationConfigEntityPluginManager->createInstance($migration);
59
60     $build['overview'] = [
61       '#type' => 'fieldset',
62       '#title' => $this->t('Overview'),
63     ];
64
65     $build['overview']['group'] = [
66       '#title' => $this->t('Group:'),
67       '#markup' => Xss::filterAdmin($migration_group),
68       '#type' => 'item',
69     ];
70
71     $build['overview']['description'] = [
72       '#title' => $this->t('Description:'),
73       '#markup' => Xss::filterAdmin($migration->label()),
74       '#type' => 'item',
75     ];
76
77     $migration_dependencies = $migration->getMigrationDependencies();
78     if (!empty($migration_dependencies['required'])) {
79       $build['overview']['dependencies'] = [
80         '#title' => $this->t('Migration Dependencies') ,
81         '#markup' => Xss::filterAdmin(implode(', ', $migration_dependencies['required'])),
82         '#type' => 'item',
83       ];
84     }
85     if (!empty($migration_dependencies['optional'])) {
86       $build['overview']['soft_dependencies'] = [
87         '#title' => $this->t('Soft Migration Dependencies'),
88         '#markup' => Xss::filterAdmin(implode(', ', $migration_dependencies['optional'])),
89         '#type' => 'item',
90       ];
91     }
92
93     return $build;
94   }
95
96   /**
97    * Display source information of a migration entity.
98    *
99    * @param string $migration_group
100    *   Machine name of the migration's group.
101    * @param string $migration
102    *   Machine name of the migration.
103    *
104    * @return array
105    *   A render array as expected by drupal_render().
106    */
107   public function source($migration_group, $migration) {
108
109     /** @var MigrationInterface $migration */
110     $migration = $this->migrationConfigEntityPluginManager->createInstance($migration);
111
112     // Source field information.
113     $build['source'] = [
114       '#type' => 'fieldset',
115       '#title' => $this->t('Source'),
116       '#group' => 'detail',
117       '#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>'),
118       '#attributes' => [
119         'id' => 'migration-detail-source',
120       ],
121     ];
122
123     $source = $migration->getSourcePlugin();
124     $build['source']['query'] = [
125       '#type' => 'item',
126       '#title' => $this->t('Query'),
127       '#markup' => '<pre>' . Xss::filterAdmin($source) . '</pre>',
128     ];
129     $header = [$this->t('Machine name'), $this->t('Description')];
130     $rows = [];
131     foreach ($source->fields($migration) as $machine_name => $description) {
132       $rows[] = [
133         ['data' => Html::escape($machine_name)],
134         ['data' => Xss::filterAdmin($description)],
135       ];
136     }
137
138     $build['source']['fields'] = [
139       '#theme' => 'table',
140       '#header' => $header,
141       '#rows' => $rows,
142       '#empty' => $this->t('No fields'),
143     ];
144
145     return $build;
146   }
147
148   /**
149    * Display process information of a migration entity.
150    *
151    * @param string $migration_group
152    *   Machine name of the migration's group.
153    * @param string $migration
154    *   Machine name of the migration.
155    *
156    * @return array
157    *   A render array as expected by drupal_render().
158    */
159   public function process($migration_group, $migration) {
160
161     /** @var MigrationInterface $migration */
162     $migration = $this->migrationConfigEntityPluginManager->createInstance($migration);
163
164     // Process information.
165     $build['process'] = [
166       '#type' => 'fieldset',
167       '#title' => $this->t('Process'),
168     ];
169
170     $header = [
171       $this->t('Destination'),
172       $this->t('Source'),
173       $this->t('Process plugin'),
174       $this->t('Default'),
175     ];
176     $rows = [];
177     foreach ($migration->getProcess() as $destination_id => $process_line) {
178       $row = [];
179       $row[] = ['data' => Html::escape($destination_id)];
180       if (isset($process_line[0]['source'])) {
181         $row[] = ['data' => Xss::filterAdmin($process_line[0]['source'])];
182       }
183       else {
184         $row[] = '';
185       }
186       if (isset($process_line[0]['plugin'])) {
187         $row[] = ['data' => Xss::filterAdmin($process_line[0]['plugin'])];
188       }
189       else {
190         $row[] = '';
191       }
192       if (isset($process_line[0]['default_value'])) {
193         $row[] = ['data' => Xss::filterAdmin($process_line[0]['default_value'])];
194       }
195       else {
196         $row[] = '';
197       }
198       $rows[] = $row;
199     }
200
201     $build['process']['fields'] = [
202       '#theme' => 'table',
203       '#header' => $header,
204       '#rows' => $rows,
205       '#empty' => $this->t('No process defined.'),
206     ];
207
208     return $build;
209   }
210
211   /**
212    * Displays destination information of a migration entity.
213    *
214    * @param string $migration_group
215    *   Machine name of the migration's group.
216    * @param string $migration
217    *   Machine name of the migration.
218    *
219    * @return array
220    *   A render array as expected by drupal_render().
221    */
222   public function destination($migration_group, $migration) {
223     /** @var MigrationInterface $migration */
224     $migration = $this->migrationConfigEntityPluginManager->createInstance($migration);
225
226     // Destination field information.
227     $build['destination'] = [
228       '#type' => 'fieldset',
229       '#title' => $this->t('Destination'),
230       '#group' => 'detail',
231       '#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>'),
232       '#attributes' => [
233         'id' => 'migration-detail-destination',
234       ],
235     ];
236     $destination = $migration->getDestinationPlugin();
237     $build['destination']['type'] = [
238       '#type' => 'item',
239       '#title' => $this->t('Type'),
240       '#markup' => Xss::filterAdmin($destination->getPluginId()),
241     ];
242     $header = [$this->t('Machine name'), $this->t('Description')];
243     $rows = [];
244     $destination_fields = $destination->fields() ?: [];
245     foreach ($destination_fields as $machine_name => $description) {
246       $rows[] = [
247         ['data' => Html::escape($machine_name)],
248         ['data' => Xss::filterAdmin($description)],
249       ];
250     }
251
252     $build['destination']['fields'] = [
253       '#theme' => 'table',
254       '#header' => $header,
255       '#rows' => $rows,
256       '#empty' => $this->t('No fields'),
257     ];
258
259     return $build;
260   }
261
262 }