View source
<?php
namespace Drupal\lingotek\Form;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Drupal\language\ConfigurableLanguageInterface;
use Drupal\lingotek\Exception\LingotekApiException;
use Drupal\lingotek\LanguageLocaleMapperInterface;
use Drupal\lingotek\LingotekConfigurationServiceInterface;
use Drupal\lingotek\LingotekInterface;
class LingotekLanguageForm {
use StringTranslationTrait;
protected $lingotek;
protected $languageLocaleMapper;
protected $lingotekConfiguration;
public function __construct(LingotekInterface $lingotek, LanguageLocaleMapperInterface $language_locale_mapper, LingotekConfigurationServiceInterface $lingotek_configuration = NULL) {
$this->lingotek = $lingotek;
$this->languageLocaleMapper = $language_locale_mapper;
if (!$lingotek_configuration) {
@trigger_error('The lingotek.configuration service must be passed to LingotekLanguageForm::__construct, it is required before Lingotek 4.0.0.', E_USER_DEPRECATED);
$lingotek_configuration = \Drupal::service('lingotek.configuration');
}
$this->lingotekConfiguration = $lingotek_configuration;
}
public function form(array &$form, FormStateInterface $form_state) {
$language = $form_state
->getFormObject()
->getEntity();
$langcode = $language
->getId();
$form['custom_language']['lingotek'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Lingotek translation'),
'#weight' => 0,
];
$form['custom_language']['lingotek']['lingotek_disabled'] = [
'#type' => 'checkbox',
'#title' => t('Disabled for Lingotek translation'),
'#default_value' => $langcode !== NULL ? !$this->lingotekConfiguration
->isLanguageEnabled($language) : FALSE,
'#description' => $this
->t('Check this if you want Lingotek to ignore this language or locale.'),
];
$form['custom_language']['lingotek']['lingotek_locale'] = [
'#type' => 'textfield',
'#title' => t('Locale'),
'#autocomplete_route_name' => 'lingotek.supported_locales_autocomplete',
'#default_value' => $langcode !== NULL ? str_replace("_", "-", $this->languageLocaleMapper
->getLocaleForLangcode($langcode)) : '',
'#description' => $this
->t('The Lingotek locale this language maps to.') . ' ' . $this
->t('Use locale codes as <a href=":w3ctags">defined by the W3C</a> for interoperability. <em>Examples: "en", "en-gb" and "zh-hant".</em>', [
':w3ctags' => 'http://www.w3.org/International/articles/language-tags/',
]),
];
$form['custom_language']['lingotek']['lingotek_locale_link'] = [
'#type' => 'link',
'#title' => $this
->t('Lingotek supported locales list'),
'#url' => Url::fromRoute('lingotek.supported_locales'),
'#ajax' => [
'class' => [
'use-ajax',
],
],
'#attributes' => [
'class' => [
'use-ajax',
],
'data-dialog-type' => 'dialog',
'data-dialog-options' => Json::encode([
'width' => 861,
'height' => 700,
'draggable' => TRUE,
'autoResize' => FALSE,
]),
],
];
if ($langcode) {
$form['actions']['submit']['#validate'][] = LingotekLanguageForm::class . '::validateLocale';
}
else {
$form['custom_language']['submit']['#validate'][] = LingotekLanguageForm::class . '::validateLocale';
}
$form['#entity_builders'][] = LingotekLanguageForm::class . '::languageEntityBuilder';
}
public static function languageEntityBuilder($entity_type, ConfigurableLanguageInterface $language, array &$form, FormStateInterface $form_state) {
if ($form_state
->hasValue([
'lingotek_locale',
])) {
$lingotek_locale = $form_state
->getValue([
'lingotek_locale',
]);
$lingotekDisabled = $form_state
->getValue([
'lingotek_disabled',
]);
$language
->setThirdPartySetting('lingotek', 'locale', str_replace("-", "_", $lingotek_locale));
$language
->setThirdPartySetting('lingotek', 'disabled', $lingotekDisabled);
}
}
public static function validateLocale(&$form, FormStateInterface $form_state) {
$form_key = [
'lingotek_locale',
];
if (!$form_state
->isValueEmpty($form_key)) {
$value = $form_state
->getValue($form_key);
try {
if (!self::isValidLocale($value)) {
$form_state
->setErrorByName('lingotek_locale', t('The Lingotek locale %locale does not exist.', [
'%locale' => $value,
]));
}
} catch (LingotekApiException $lingotekApiException) {
if ($lingotekApiException
->getCode() === 401) {
\Drupal::messenger()
->addWarning("The Lingotek locale has not been validated.", 'warning');
}
}
}
}
public static function isValidLocale($locale) {
$locales = \Drupal::service('lingotek')
->getLocales();
return in_array(str_replace("_", "-", $locale), $locales);
}
}