You are here

public function CredentialForm::validateForm in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/migrate_drupal_ui/src/Form/CredentialForm.php \Drupal\migrate_drupal_ui\Form\CredentialForm::validateForm()
  2. 9 core/modules/migrate_drupal_ui/src/Form/CredentialForm.php \Drupal\migrate_drupal_ui\Form\CredentialForm::validateForm()

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

core/modules/migrate_drupal_ui/src/Form/CredentialForm.php, line 207

Class

CredentialForm
Migrate Upgrade database credential form.

Namespace

Drupal\migrate_drupal_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.
  $connection = NULL;
  $error_key = $database['driver'] . '][database';
  if ($errors = $drivers[$driver]
    ->validateDatabaseSettings($database)) {
    foreach ($errors as $name => $message) {
      $this->errors[$name] = $message;
    }
  }

  // Get the Drupal version of the source database so it can be validated.
  if (!$this->errors) {
    try {
      $connection = $this
        ->getConnection($database);
    } catch (\Exception $e) {
      $msg = $this
        ->t('Failed to connect to your database server. The server reports the following message: %error.<ul><li>Is the database server running?</li><li>Does the database exist, and have you entered the correct database name?</li><li>Have you entered the correct username and password?</li><li>Have you entered the correct database hostname?</li></ul>', [
        '%error' => $e
          ->getMessage(),
      ]);
      $this->errors[$error_key] = $msg;
    }
  }

  // Get the Drupal version of the source database so it can be validated.
  if (!$this->errors) {
    $version = (string) $this
      ->getLegacyDrupalVersion($connection);
    if (!$version) {
      $this->errors[$error_key] = $this
        ->t('Source database does not contain a recognizable Drupal version.');
    }
    elseif ($version !== (string) $form_state
      ->getValue('version')) {
      $this->errors['version'] = $this
        ->t('Source database is Drupal version @version but version @selected was selected.', [
        '@version' => $version,
        '@selected' => $form_state
          ->getValue('version'),
      ]);
    }
  }

  // Setup migrations and save form data to private store.
  if (!$this->errors) {
    try {
      $this
        ->setupMigrations($connection, $version, $database, $form_state);
    } catch (BadPluginDefinitionException $e) {

      // BadPluginDefinitionException occurs if the source_module is not
      // defined, which happens during testing.
      $this->errors[$error_key] = $e
        ->getMessage();
    } catch (RequirementsException $e) {
      $this->errors[$error_key] = $e
        ->getMessage();
    }
  }

  // Display all errors as a list of items.
  if ($this->errors) {
    $form_state
      ->setError($form, $this
      ->t('<h3>Resolve all issues below to continue the upgrade.</h3>'));
    foreach ($this->errors as $name => $message) {
      $form_state
        ->setErrorByName($name, $message);
    }
  }
}