public function GlobalLinkTranslatorUi::validateConfigurationForm in GlobalLink Connect for Drupal 8.2
Same name and namespace in other branches
- 8 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 136 - 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();
$classifier = $values['pd_classifier'];
try {
$pd_config = $adapter
->getPDConfig([
'pd_url' => $values['pd_url'],
'pd_username' => $values['pd_username'],
'pd_password' => $values['pd_password'],
'pd_user_agent' => $values['pd_user_agent'],
]);
// Test connections settings.
$glexchange = $adapter
->getGlExchange($pd_config);
$classifierList = array();
//Test duplicate project ids
$projectId = $values['pd_projectid'];
$projectIdList = explode(',', $projectId);
$duplicate = false;
for ($i = 0; $i < count($projectIdList); $i++) {
$currentProject = $glexchange
->getProject($projectIdList[$i]);
$currentFileFormats = $currentProject->fileFormats;
foreach ($currentFileFormats as $key => $value) {
$classifierList[] = $value;
}
$projectIdList[$i] = trim($projectIdList[$i]);
for ($j = 0; $j < count($projectIdList); $j++) {
$projectIdList[$j] = trim($projectIdList[$j]);
if ($i != $j && $projectIdList[$i] == $projectIdList[$j]) {
$duplicate = true;
}
}
}
if ($duplicate) {
$form_state
->setError($settings, t('No duplicate project IDs are allowed.'));
}
elseif (!in_array($classifier, $classifierList)) {
$form_state
->setError($settings, t('You have selected a classifier that is not available in the PD project. Please talk to your admin to get the correct classifier.'));
}
else {
// 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.'));
}
}