You are here

public function MigrateUpgradeForm::submitConfirmForm in Migrate Upgrade 8

Submission handler for the confirmation form.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

File

src/Form/MigrateUpgradeForm.php, line 1120
Contains \Drupal\migrate_upgrade\Form\MigrateUpgradeForm.

Class

MigrateUpgradeForm
Defines a multi-step form for performing direct site upgrades.

Namespace

Drupal\migrate_upgrade\Form

Code

public function submitConfirmForm(array &$form, FormStateInterface $form_state) {
  $storage = $form_state
    ->getStorage();
  if (isset($storage['upgrade_option']) && $storage['upgrade_option'] == static::MIGRATE_UPGRADE_ROLLBACK) {
    $query = $this->entityStorage
      ->getQuery();
    $names = $query
      ->execute();

    // Order the migrations according to their dependencies.

    /** @var \Drupal\migrate\Entity\MigrationInterface[] $migrations */
    $migrations = $this->entityStorage
      ->loadMultiple($names);

    // Assume we want all those tagged 'Drupal %'.
    foreach ($migrations as $migration_id => $migration) {
      $keep = FALSE;
      $tags = $migration
        ->get('migration_tags');
      foreach ($tags as $tag) {
        if (strpos($tag, 'Drupal ') === 0) {
          $keep = TRUE;
          break;
        }
      }
      if (!$keep) {
        unset($migrations[$migration_id]);
      }
    }

    // Roll back in reverse order.
    $migrations = array_reverse($migrations);
    $batch = [
      'title' => $this
        ->t('Rolling back upgrade'),
      'progress_message' => '',
      'operations' => [
        [
          [
            MigrateUpgradeRunBatch::class,
            'run',
          ],
          [
            array_keys($migrations),
            'rollback',
          ],
        ],
      ],
      'finished' => [
        MigrateUpgradeRunBatch::class,
        'finished',
      ],
    ];
    batch_set($batch);
    $form_state
      ->setRedirect('migrate_upgrade.upgrade');
    $this->state
      ->delete('migrate_upgrade.performed');
  }
  else {
    $migration_template = $storage['migration_template'];
    $migration_ids = $this
      ->createMigrations($migration_template);
    $batch = [
      'title' => $this
        ->t('Running upgrade'),
      'progress_message' => '',
      'operations' => [
        [
          [
            MigrateUpgradeRunBatch::class,
            'run',
          ],
          [
            $migration_ids,
            'import',
          ],
        ],
      ],
      'finished' => [
        MigrateUpgradeRunBatch::class,
        'finished',
      ],
    ];
    batch_set($batch);
    $form_state
      ->setRedirect('<front>');
    $this->state
      ->set('migrate_upgrade.performed', REQUEST_TIME);
  }
}