Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / migrate_tools / src / Controller / MigrationController.php
index 09ae154ad0b86ecf9bdbadc5a18a2a1ad3f1caaa..15c90795ca5dc179cacd9044a0d888b5a517bfa3 100644 (file)
@@ -3,12 +3,17 @@
 namespace Drupal\migrate_tools\Controller;
 
 use Drupal\Core\Controller\ControllerBase;
-use Drupal\migrate\Plugin\MigrationInterface;
 use Drupal\Component\Utility\Xss;
 use Drupal\Component\Utility\Html;
-use Drupal\migrate_plus\Plugin\MigrationConfigEntityPluginManager;
+use Drupal\Core\Routing\CurrentRouteMatch;
+use Drupal\migrate\Plugin\MigrationPluginManagerInterface;
 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
+use Drupal\migrate_plus\Entity\MigrationGroupInterface;
+use Drupal\migrate_plus\Entity\MigrationInterface;
+use Drupal\migrate_tools\MigrateBatchExecutable;
 use Symfony\Component\DependencyInjection\ContainerInterface;
+use Drupal\Core\Url;
+use Drupal\migrate\MigrateMessage;
 
 /**
  * Returns responses for migrate_tools migration view routes.
@@ -18,18 +23,28 @@ class MigrationController extends ControllerBase implements ContainerInjectionIn
   /**
    * Plugin manager for migration plugins.
    *
-   * @var \Drupal\migrate_plus\Plugin\MigrationConfigEntityPluginManager
+   * @var \Drupal\migrate\Plugin\MigrationPluginManagerInterface
    */
-  protected $migrationConfigEntityPluginManager;
+  protected $migrationPluginManager;
+
+  /**
+   * The current route match.
+   *
+   * @var \Drupal\Core\Routing\CurrentRouteMatch
+   */
+  protected $currentRouteMatch;
 
   /**
    * Constructs a new MigrationController object.
    *
-   * @param \Drupal\migrate_plus\Plugin\MigrationConfigEntityPluginManager $migration_config_entity_plugin_manager
+   * @param \Drupal\migrate\Plugin\MigrationPluginManagerInterface $migration_plugin_manager
    *   The plugin manager for config entity-based migrations.
+   * @param \Drupal\Core\Routing\CurrentRouteMatch $currentRouteMatch
+   *   The current route match.
    */
-  public function __construct(MigrationConfigEntityPluginManager $migration_config_entity_plugin_manager) {
-    $this->migrationConfigEntityPluginManager = $migration_config_entity_plugin_manager;
+  public function __construct(MigrationPluginManagerInterface $migration_plugin_manager, CurrentRouteMatch $currentRouteMatch) {
+    $this->migrationPluginManager = $migration_plugin_manager;
+    $this->currentRouteMatch = $currentRouteMatch;
   }
 
   /**
@@ -37,26 +52,23 @@ class MigrationController extends ControllerBase implements ContainerInjectionIn
    */
   public static function create(ContainerInterface $container) {
     return new static(
-      $container->get('plugin.manager.config_entity_migration')
+      $container->get('plugin.manager.migration'),
+      $container->get('current_route_match')
     );
   }
 
   /**
    * Displays an overview of a migration entity.
    *
-   * @param string $migration_group
-   *   Machine name of the migration's group.
-   * @param string $migration
-   *   Machine name of the migration.
+   * @param \Drupal\migrate_plus\Entity\MigrationGroupInterface $migration_group
+   *   The migration group.
+   * @param \Drupal\migrate_plus\Entity\MigrationInterface $migration
+   *   The $migration.
    *
    * @return array
    *   A render array as expected by drupal_render().
    */
-  public function overview($migration_group, $migration) {
-
-    /** @var MigrationInterface $migration */
-    $migration = $this->migrationConfigEntityPluginManager->createInstance($migration);
-
+  public function overview(MigrationGroupInterface $migration_group, MigrationInterface $migration) {
     $build['overview'] = [
       '#type' => 'fieldset',
       '#title' => $this->t('Overview'),
@@ -64,7 +76,7 @@ class MigrationController extends ControllerBase implements ContainerInjectionIn
 
     $build['overview']['group'] = [
       '#title' => $this->t('Group:'),
-      '#markup' => Xss::filterAdmin($migration_group),
+      '#markup' => Xss::filterAdmin($migration_group->label()),
       '#type' => 'item',
     ];
 
@@ -73,8 +85,8 @@ class MigrationController extends ControllerBase implements ContainerInjectionIn
       '#markup' => Xss::filterAdmin($migration->label()),
       '#type' => 'item',
     ];
-
-    $migration_dependencies = $migration->getMigrationDependencies();
+    $migration_plugin = $this->migrationPluginManager->createInstance($migration->id(), $migration->toArray());
+    $migration_dependencies = $migration_plugin->getMigrationDependencies();
     if (!empty($migration_dependencies['required'])) {
       $build['overview']['dependencies'] = [
         '#title' => $this->t('Migration Dependencies') ,
@@ -96,19 +108,15 @@ class MigrationController extends ControllerBase implements ContainerInjectionIn
   /**
    * Display source information of a migration entity.
    *
-   * @param string $migration_group
-   *   Machine name of the migration's group.
-   * @param string $migration
-   *   Machine name of the migration.
+   * @param \Drupal\migrate_plus\Entity\MigrationGroupInterface $migration_group
+   *   The migration group.
+   * @param \Drupal\migrate_plus\Entity\MigrationInterface $migration
+   *   The $migration.
    *
    * @return array
    *   A render array as expected by drupal_render().
    */
-  public function source($migration_group, $migration) {
-
-    /** @var MigrationInterface $migration */
-    $migration = $this->migrationConfigEntityPluginManager->createInstance($migration);
-
+  public function source(MigrationGroupInterface $migration_group, MigrationInterface $migration) {
     // Source field information.
     $build['source'] = [
       '#type' => 'fieldset',
@@ -119,8 +127,8 @@ class MigrationController extends ControllerBase implements ContainerInjectionIn
         'id' => 'migration-detail-source',
       ],
     ];
-
-    $source = $migration->getSourcePlugin();
+    $migration_plugin = $this->migrationPluginManager->createInstance($migration->id(), $migration->toArray());
+    $source = $migration_plugin->getSourcePlugin();
     $build['source']['query'] = [
       '#type' => 'item',
       '#title' => $this->t('Query'),
@@ -128,7 +136,7 @@ class MigrationController extends ControllerBase implements ContainerInjectionIn
     ];
     $header = [$this->t('Machine name'), $this->t('Description')];
     $rows = [];
-    foreach ($source->fields($migration) as $machine_name => $description) {
+    foreach ($source->fields($migration_plugin) as $machine_name => $description) {
       $rows[] = [
         ['data' => Html::escape($machine_name)],
         ['data' => Xss::filterAdmin($description)],
@@ -146,20 +154,45 @@ class MigrationController extends ControllerBase implements ContainerInjectionIn
   }
 
   /**
-   * Display process information of a migration entity.
+   * Run a migration.
    *
-   * @param string $migration_group
-   *   Machine name of the migration's group.
-   * @param string $migration
-   *   Machine name of the migration.
+   * @param \Drupal\migrate_plus\Entity\MigrationGroupInterface $migration_group
+   *   The migration group.
+   * @param \Drupal\migrate_plus\Entity\MigrationInterface $migration
+   *   The $migration.
    *
    * @return array
    *   A render array as expected by drupal_render().
    */
-  public function process($migration_group, $migration) {
+  public function run(MigrationGroupInterface $migration_group, MigrationInterface $migration) {
+    $migrateMessage = new MigrateMessage();
+    $options = [];
+
+    $migration_plugin = $this->migrationPluginManager->createInstance($migration->id(), $migration->toArray());
+    $executable = new MigrateBatchExecutable($migration_plugin, $migrateMessage, $options);
+    $executable->batchImport();
 
-    /** @var MigrationInterface $migration */
-    $migration = $this->migrationConfigEntityPluginManager->createInstance($migration);
+    $migration_group = $this->currentRouteMatch->getParameter('migration_group');
+    $route_parameters = [
+      'migration_group' => $migration_group,
+      'migration' => $migration->id(),
+    ];
+    return batch_process(Url::fromRoute('entity.migration.process', $route_parameters));
+  }
+
+  /**
+   * Display process information of a migration entity.
+   *
+   * @param \Drupal\migrate_plus\Entity\MigrationGroupInterface $migration_group
+   *   The migration group.
+   * @param \Drupal\migrate_plus\Entity\MigrationInterface $migration
+   *   The $migration.
+   *
+   * @return array
+   *   A render array as expected by drupal_render().
+   */
+  public function process(MigrationGroupInterface $migration_group, MigrationInterface $migration) {
+    $migration_plugin = $this->migrationPluginManager->createInstance($migration->id(), $migration->toArray());
 
     // Process information.
     $build['process'] = [
@@ -174,7 +207,7 @@ class MigrationController extends ControllerBase implements ContainerInjectionIn
       $this->t('Default'),
     ];
     $rows = [];
-    foreach ($migration->getProcess() as $destination_id => $process_line) {
+    foreach ($migration_plugin->getProcess() as $destination_id => $process_line) {
       $row = [];
       $row[] = ['data' => Html::escape($destination_id)];
       if (isset($process_line[0]['source'])) {
@@ -205,23 +238,28 @@ class MigrationController extends ControllerBase implements ContainerInjectionIn
       '#empty' => $this->t('No process defined.'),
     ];
 
+    $build['process']['run'] = [
+      '#type' => 'link',
+      '#title' => $this->t('Run'),
+      '#url' => Url::fromRoute('entity.migration.process.run', ['migration_group' => $migration_group->id(), 'migration' => $migration->id()]),
+    ];
+
     return $build;
   }
 
   /**
    * Displays destination information of a migration entity.
    *
-   * @param string $migration_group
-   *   Machine name of the migration's group.
-   * @param string $migration
-   *   Machine name of the migration.
+   * @param \Drupal\migrate_plus\Entity\MigrationGroupInterface $migration_group
+   *   The migration group.
+   * @param \Drupal\migrate_plus\Entity\MigrationInterface $migration
+   *   The $migration.
    *
    * @return array
    *   A render array as expected by drupal_render().
    */
-  public function destination($migration_group, $migration) {
-    /** @var MigrationInterface $migration */
-    $migration = $this->migrationConfigEntityPluginManager->createInstance($migration);
+  public function destination(MigrationGroupInterface $migration_group, MigrationInterface $migration) {
+    $migration_plugin = $this->migrationPluginManager->createInstance($migration->id(), $migration->toArray());
 
     // Destination field information.
     $build['destination'] = [
@@ -233,7 +271,7 @@ class MigrationController extends ControllerBase implements ContainerInjectionIn
         'id' => 'migration-detail-destination',
       ],
     ];
-    $destination = $migration->getDestinationPlugin();
+    $destination = $migration_plugin->getDestinationPlugin();
     $build['destination']['type'] = [
       '#type' => 'item',
       '#title' => $this->t('Type'),