3 namespace Drupal\migrate_tools\Controller;
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;
16 use Drupal\migrate\MigrateMessage;
19 * Returns responses for migrate_tools migration view routes.
21 class MigrationController extends ControllerBase implements ContainerInjectionInterface {
24 * Plugin manager for migration plugins.
26 * @var \Drupal\migrate\Plugin\MigrationPluginManagerInterface
28 protected $migrationPluginManager;
31 * The current route match.
33 * @var \Drupal\Core\Routing\CurrentRouteMatch
35 protected $currentRouteMatch;
38 * Constructs a new MigrationController object.
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.
45 public function __construct(MigrationPluginManagerInterface $migration_plugin_manager, CurrentRouteMatch $currentRouteMatch) {
46 $this->migrationPluginManager = $migration_plugin_manager;
47 $this->currentRouteMatch = $currentRouteMatch;
53 public static function create(ContainerInterface $container) {
55 $container->get('plugin.manager.migration'),
56 $container->get('current_route_match')
61 * Displays an overview of a migration entity.
63 * @param \Drupal\migrate_plus\Entity\MigrationGroupInterface $migration_group
64 * The migration group.
65 * @param \Drupal\migrate_plus\Entity\MigrationInterface $migration
69 * A render array as expected by drupal_render().
71 public function overview(MigrationGroupInterface $migration_group, MigrationInterface $migration) {
72 $build['overview'] = [
73 '#type' => 'fieldset',
74 '#title' => $this->t('Overview'),
77 $build['overview']['group'] = [
78 '#title' => $this->t('Group:'),
79 '#markup' => Xss::filterAdmin($migration_group->label()),
83 $build['overview']['description'] = [
84 '#title' => $this->t('Description:'),
85 '#markup' => Xss::filterAdmin($migration->label()),
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'])),
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'])),
109 * Display source information of a migration entity.
111 * @param \Drupal\migrate_plus\Entity\MigrationGroupInterface $migration_group
112 * The migration group.
113 * @param \Drupal\migrate_plus\Entity\MigrationInterface $migration
117 * A render array as expected by drupal_render().
119 public function source(MigrationGroupInterface $migration_group, MigrationInterface $migration) {
120 // Source field information.
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>'),
127 'id' => 'migration-detail-source',
130 $migration_plugin = $this->migrationPluginManager->createInstance($migration->id(), $migration->toArray());
131 $source = $migration_plugin->getSourcePlugin();
132 $build['source']['query'] = [
134 '#title' => $this->t('Query'),
135 '#markup' => '<pre>' . Xss::filterAdmin($source) . '</pre>',
137 $header = [$this->t('Machine name'), $this->t('Description')];
139 foreach ($source->fields($migration_plugin) as $machine_name => $description) {
141 ['data' => Html::escape($machine_name)],
142 ['data' => Xss::filterAdmin($description)],
146 $build['source']['fields'] = [
148 '#header' => $header,
150 '#empty' => $this->t('No fields'),
159 * @param \Drupal\migrate_plus\Entity\MigrationGroupInterface $migration_group
160 * The migration group.
161 * @param \Drupal\migrate_plus\Entity\MigrationInterface $migration
165 * A render array as expected by drupal_render().
167 public function run(MigrationGroupInterface $migration_group, MigrationInterface $migration) {
168 $migrateMessage = new MigrateMessage();
171 $migration_plugin = $this->migrationPluginManager->createInstance($migration->id(), $migration->toArray());
172 $executable = new MigrateBatchExecutable($migration_plugin, $migrateMessage, $options);
173 $executable->batchImport();
175 $migration_group = $this->currentRouteMatch->getParameter('migration_group');
176 $route_parameters = [
177 'migration_group' => $migration_group,
178 'migration' => $migration->id(),
180 return batch_process(Url::fromRoute('entity.migration.process', $route_parameters));
184 * Display process information of a migration entity.
186 * @param \Drupal\migrate_plus\Entity\MigrationGroupInterface $migration_group
187 * The migration group.
188 * @param \Drupal\migrate_plus\Entity\MigrationInterface $migration
192 * A render array as expected by drupal_render().
194 public function process(MigrationGroupInterface $migration_group, MigrationInterface $migration) {
195 $migration_plugin = $this->migrationPluginManager->createInstance($migration->id(), $migration->toArray());
197 // Process information.
198 $build['process'] = [
199 '#type' => 'fieldset',
200 '#title' => $this->t('Process'),
204 $this->t('Destination'),
206 $this->t('Process plugin'),
210 foreach ($migration_plugin->getProcess() as $destination_id => $process_line) {
212 $row[] = ['data' => Html::escape($destination_id)];
213 if (isset($process_line[0]['source'])) {
214 $row[] = ['data' => Xss::filterAdmin($process_line[0]['source'])];
219 if (isset($process_line[0]['plugin'])) {
220 $row[] = ['data' => Xss::filterAdmin($process_line[0]['plugin'])];
225 if (isset($process_line[0]['default_value'])) {
226 $row[] = ['data' => Xss::filterAdmin($process_line[0]['default_value'])];
234 $build['process']['fields'] = [
236 '#header' => $header,
238 '#empty' => $this->t('No process defined.'),
241 $build['process']['run'] = [
243 '#title' => $this->t('Run'),
244 '#url' => Url::fromRoute('entity.migration.process.run', ['migration_group' => $migration_group->id(), 'migration' => $migration->id()]),
251 * Displays destination information of a migration entity.
253 * @param \Drupal\migrate_plus\Entity\MigrationGroupInterface $migration_group
254 * The migration group.
255 * @param \Drupal\migrate_plus\Entity\MigrationInterface $migration
259 * A render array as expected by drupal_render().
261 public function destination(MigrationGroupInterface $migration_group, MigrationInterface $migration) {
262 $migration_plugin = $this->migrationPluginManager->createInstance($migration->id(), $migration->toArray());
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>'),
271 'id' => 'migration-detail-destination',
274 $destination = $migration_plugin->getDestinationPlugin();
275 $build['destination']['type'] = [
277 '#title' => $this->t('Type'),
278 '#markup' => Xss::filterAdmin($destination->getPluginId()),
280 $header = [$this->t('Machine name'), $this->t('Description')];
282 $destination_fields = $destination->fields() ?: [];
283 foreach ($destination_fields as $machine_name => $description) {
285 ['data' => Html::escape($machine_name)],
286 ['data' => Xss::filterAdmin($description)],
290 $build['destination']['fields'] = [
292 '#header' => $header,
294 '#empty' => $this->t('No fields'),