You are here

public function MigrateUpgradeForm::validateCredentialForm in Migrate Upgrade 8

Validation handler for the credentials 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 897
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 validateCredentialForm(array &$form, FormStateInterface $form_state) {

  // Skip if rollback was chosen.
  if ($form_state
    ->getValue('upgrade_option') == static::MIGRATE_UPGRADE_ROLLBACK) {
    return;
  }

  // Retrieve the database driver from the form, use reflection to get the
  // namespace and then construct a valid database array the same as in
  // settings.php.
  if ($driver = $form_state
    ->getValue('driver')) {
    $drivers = $this
      ->getDatabaseTypes();
    $reflection = new \ReflectionClass($drivers[$driver]);
    $install_namespace = $reflection
      ->getNamespaceName();
    $database = $form_state
      ->getValue($driver);

    // Cut the trailing \Install from namespace.
    $database['namespace'] = substr($install_namespace, 0, strrpos($install_namespace, '\\'));
    $database['driver'] = $driver;

    // Validate the driver settings and just end here if we have any issues.
    if ($errors = $drivers[$driver]
      ->validateDatabaseSettings($database)) {
      foreach ($errors as $name => $message) {
        $form_state
          ->setErrorByName($name, $message);
      }
      return;
    }
  }
  else {
    $database = [];

    // Find a migration which has database credentials and use those.
    $query = $this->entityStorage
      ->getQuery('OR');
    $ids = $query
      ->execute();
    foreach ($ids as $id) {

      /** @var \Drupal\migrate\Entity\MigrationInterface $migration */
      $migration = Migration::load($id);
      $is_drupal_migration = FALSE;
      foreach ($migration
        ->get('migration_tags') as $migration_tag) {
        if (substr($migration_tag, 0, 7) === 'Drupal ') {
          $is_drupal_migration = TRUE;
          break;
        }
      }
      if ($is_drupal_migration) {
        $source = $migration
          ->get('source');
        if ($database = $this->state
          ->get($source['database_state_key'])['database']) {
          break;
        }
      }
    }
  }
  try {

    // Get the template for migration.
    $migration_template = $this
      ->getMigrationTemplates($database, $form_state
      ->getValue('source_base_path'));

    // Get a copy of all the relevant migrations so we run them in next step.
    $migrations = $this
      ->getMigrations($migration_template);

    // Get the system data from source database.
    $system_data = $this
      ->getSystemData($database);

    // Convert the migration object into array
    // so that it can be stored in form storage.
    $migration_array = [];
    foreach ($migrations as $migration) {
      $migration_array[] = $migration
        ->toArray();
    }

    // Store the retrieved migration templates in form storage.
    $form_state
      ->set('migration_template', $migration_template);

    // Store the retrieved migration ids in form storage.
    $form_state
      ->set('migration', $migration_array);

    // Store the retrived system data in from storage.
    $form_state
      ->set('system_data', $system_data);
  } catch (\Exception $e) {
    $error_message = [
      '#type' => 'inline_template',
      '#template' => '{% trans %}Resolve the issue below to continue the upgrade.{% endtrans%}{{ errors }}',
      '#context' => [
        'errors' => [
          '#theme' => 'item_list',
          '#items' => [
            $e
              ->getMessage(),
          ],
        ],
      ],
    ];
    $form_state
      ->setErrorByName($database['driver'] . '][0', $this->renderer
      ->renderPlain($error_message));
  }
}