You are here

public function SourceSelectForm::validateForm in Drupal-to-Drupal data migration 8.3

Form validation handler.

Parameters

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

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

Overrides FormBase::validateForm

File

migrate_d2d_ui/src/Form/SourceSelectForm.php, line 124

Class

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

Namespace

Drupal\migrate_d2d_ui\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {

  // 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.
  $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;
  }
  try {
    $connection = $this
      ->getConnection($database);
    $version = $this
      ->getLegacyDrupalVersion($connection);
    if (!$version) {
      $form_state
        ->setErrorByName($database['driver'] . '][0', $this
        ->t('Source database does not contain a recognizable Drupal version.'));
    }
    else {
      $cached_values = $form_state
        ->getTemporaryValue('wizard');
      $cached_values['version'] = $version;
      $cached_values['database'] = $database;
      $form_state
        ->setTemporaryValue('wizard', $cached_values);
    }
  } 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));
  }
}