function CredentialForm::validateForm
Same name in other branches
- 9 core/modules/migrate_drupal_ui/src/Form/CredentialForm.php \Drupal\migrate_drupal_ui\Form\CredentialForm::validateForm()
- 8.9.x core/modules/migrate_drupal_ui/src/Form/CredentialForm.php \Drupal\migrate_drupal_ui\Form\CredentialForm::validateForm()
- 10 core/modules/migrate_drupal_ui/src/Form/CredentialForm.php \Drupal\migrate_drupal_ui\Form\CredentialForm::validateForm()
Overrides FormBase::validateForm
File
-
core/
modules/ migrate_drupal_ui/ src/ Form/ CredentialForm.php, line 261
Class
- CredentialForm
- Migrate Upgrade database credential form.
Namespace
Drupal\migrate_drupal_ui\FormCode
public function validateForm(array &$form, FormStateInterface $form_state) {
$connection = NULL;
$source_connection = $form_state->getValue('source_connection');
if ($source_connection) {
$info = Database::getConnectionInfo($source_connection);
$database = reset($info);
}
else {
// 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) {
$this->errors[$name] = $message;
}
}
}
// Check the connection with the source database.
$error_key = $database['driver'] . '][database';
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;
}
}
// Check that the source database is not the site database.
if (!$this->errors) {
$options = array_flip([
'database',
'host',
'port',
'prefix',
'namespace',
]);
$source = array_map('strval', array_intersect_key($connection->getConnectionOptions(), $options));
$destination = array_map('strval', array_intersect_key(Database::getConnection()->getConnectionOptions(), $options));
ksort($source);
ksort($destination);
if ($source === $destination) {
$this->errors[$error_key] = $this->t('Enter credentials for the database of the Drupal site you want to upgrade, not the new site.');
}
}
// 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);
}
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.