public function GlobalLinkTranslatorUi::validateConfigurationForm in GlobalLink Connect for Drupal 8
Same name and namespace in other branches
- 8.2 src/GlobalLinkTranslatorUi.php \Drupal\globallink\GlobalLinkTranslatorUi::validateConfigurationForm()
Form validation handler.
Parameters
array $form: An associative array containing the structure of the plugin form as built by static::buildConfigurationForm().
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().
Overrides TranslatorPluginUiBase::validateConfigurationForm
File
- src/
GlobalLinkTranslatorUi.php, line 124 - Contains Drupal\globallink\GlobalLinkTranslatorUi.
Class
- GlobalLinkTranslatorUi
- GlobalLink translator UI.
Namespace
Drupal\globallinkCode
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
$settings = $form['plugin_wrapper']['settings'];
$adapter = \Drupal::getContainer()
->get('globallink.gl_exchange_adapter');
$values = $form_state
->getValue('settings');
/** @var \Drupal\tmgmt\TranslatorInterface $translator */
$translator = $form_state
->getFormObject()
->getEntity();
/** @var \Drupal\tmgmt_oht\Plugin\tmgmt\Translator\OhtTranslator $translator_plugin */
$translator_plugin = $translator
->getPlugin();
try {
$pd_config = $adapter
->getPDConfig([
'pd_url' => $values['pd_url'],
'pd_username' => $values['pd_username'],
'pd_password' => $values['pd_password'],
]);
// Test connections settings.
$adapter
->getGlExchange($pd_config);
// Test language mappings.
$all_supported = [];
// Flatten the array of supported pairs.
$supported_pairs = $translator_plugin
->getSupportedLanguagePairs($translator);
foreach ($supported_pairs as $supported_pair) {
foreach ($supported_pair as $item) {
$all_supported[$item] = $item;
}
}
$unsupported = [];
$mappings = $form_state
->getValue('remote_languages_mappings');
foreach ($mappings as $mapping) {
if (!in_array($mapping, $all_supported)) {
$unsupported[] = $mapping;
}
}
if ($unsupported) {
$element = $form['plugin_wrapper']['remote_languages_mappings'];
foreach (Element::children($element) as $key) {
if (!empty($element[$key]['#value']) && in_array($element[$key]['#value'], $unsupported)) {
$form_state
->setError($element[$key], t('The following language codes are not supported by this project: %codes', [
'%codes' => implode(', ', $unsupported),
]));
}
}
}
// Validate email addresses.
if (!empty($values['pd_notify_emails'])) {
$emails = explode(' ', $values['pd_notify_emails']);
$email_validator = \Drupal::service('email.validator');
$invalid_emails = [];
foreach ($emails as $email) {
trim($email);
if (!$email_validator
->isValid($email)) {
$invalid_emails[] = $email;
}
}
if ($invalid_emails) {
$form_state
->setError($settings['pd_notify_emails'], t('Invalid email address(es) found: %emails', [
'%emails' => implode(' ', $invalid_emails),
]));
}
}
} catch (\Exception $e) {
$form_state
->setError($settings, t('Login credentials are incorrect.'));
}
}