class LingotekLanguageForm in Lingotek Translation 8
Same name and namespace in other branches
- 8.2 src/Form/LingotekLanguageForm.php \Drupal\lingotek\Form\LingotekLanguageForm
- 4.0.x src/Form/LingotekLanguageForm.php \Drupal\lingotek\Form\LingotekLanguageForm
- 3.0.x src/Form/LingotekLanguageForm.php \Drupal\lingotek\Form\LingotekLanguageForm
- 3.1.x src/Form/LingotekLanguageForm.php \Drupal\lingotek\Form\LingotekLanguageForm
- 3.2.x src/Form/LingotekLanguageForm.php \Drupal\lingotek\Form\LingotekLanguageForm
- 3.3.x src/Form/LingotekLanguageForm.php \Drupal\lingotek\Form\LingotekLanguageForm
- 3.4.x src/Form/LingotekLanguageForm.php \Drupal\lingotek\Form\LingotekLanguageForm
- 3.5.x src/Form/LingotekLanguageForm.php \Drupal\lingotek\Form\LingotekLanguageForm
- 3.6.x src/Form/LingotekLanguageForm.php \Drupal\lingotek\Form\LingotekLanguageForm
- 3.7.x src/Form/LingotekLanguageForm.php \Drupal\lingotek\Form\LingotekLanguageForm
- 3.8.x src/Form/LingotekLanguageForm.php \Drupal\lingotek\Form\LingotekLanguageForm
Alters the Drupal language module language forms.
@package Drupal\lingotek\Form
Hierarchy
- class \Drupal\lingotek\Form\LingotekLanguageForm uses StringTranslationTrait
Expanded class hierarchy of LingotekLanguageForm
1 string reference to 'LingotekLanguageForm'
1 service uses LingotekLanguageForm
File
- src/
Form/ LingotekLanguageForm.php, line 16
Namespace
Drupal\lingotek\FormView source
class LingotekLanguageForm {
use StringTranslationTrait;
/**
* A lingotek connector object
*
* @var \Drupal\lingotek\LingotekInterface
*/
protected $lingotek;
/**
* The language-locale mapper.
*
* @var \Drupal\lingotek\LanguageLocaleMapperInterface
*/
protected $languageLocaleMapper;
/**
* Constructs a new LingotekConfigTranslationService object.
*
* @param \Drupal\lingotek\LingotekInterface $lingotek
* A lingotek object.
* @param \Drupal\lingotek\LanguageLocaleMapperInterface $language_locale_mapper
* The language-locale mapper.
*/
public function __construct(LingotekInterface $lingotek, LanguageLocaleMapperInterface $language_locale_mapper) {
$this->lingotek = $lingotek;
$this->languageLocaleMapper = $language_locale_mapper;
}
/**
* Alters the configurable language entity edit and add form.
*
* @param array $form
* The form definition array for the configurable language entity.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
public function form(array &$form, FormStateInterface $form_state) {
/** @var ConfigurableLanguageInterface $language */
$language = $form_state
->getFormObject()
->getEntity();
$langcode = $language
->getId();
$form['custom_language']['lingotek_locale'] = [
'#type' => 'textfield',
'#title' => t('Locale'),
// If we have a langcode, check if there is a locale or default to the one we can guess.
'#default_value' => $langcode !== NULL ? str_replace("_", "-", $this->languageLocaleMapper
->getLocaleForLangcode($langcode)) : '',
'#weight' => 0,
'#description' => $this
->t('The Lingotek locale this language maps to.') . ' ' . $this
->t('Use language codes as <a href=":w3ctags">defined by the W3C</a> for interoperability. <em>Examples: "en", "en-gb" and "zh-hant".</em>', array(
':w3ctags' => 'http://www.w3.org/International/articles/language-tags/',
)),
];
// Buttons are different if adding or editing a language. We need validation
// on both cases.
if ($langcode) {
$form['actions']['submit']['#validate'][] = LingotekLanguageForm::class . '::validateLocale';
}
else {
$form['custom_language']['submit']['#validate'][] = LingotekLanguageForm::class . '::validateLocale';
}
$form['#entity_builders'][] = LingotekLanguageForm::class . '::languageEntityBuilder';
}
/**
* Entity builder for the configurable language type form with lingotek options.
*
* @param string $entity_type
* The entity type.
* @param \Drupal\language\ConfigurableLanguageInterface $language
* The language object.
* @param array $form
* The form definition array for the configurable language entity.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @see lingotek_form_language_admin_add_form_alter()
* @see lingotek_form_language_admin_edit_form_alter()
*/
public static function languageEntityBuilder($entity_type, ConfigurableLanguageInterface $language, array &$form, FormStateInterface $form_state) {
$form_key = [
'lingotek_locale',
];
if ($value = $form_state
->getValue($form_key)) {
$language
->setThirdPartySetting('lingotek', 'locale', str_replace("-", "_", $value));
}
}
/**
* Validate the configurable language type form with lingotek options.
*
* @param array $form
* The form definition array for the configurable language entity.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @see lingotek_form_language_admin_add_form_alter()
* @see lingotek_form_language_admin_edit_form_alter()
*/
public static function validateLocale(&$form, FormStateInterface $form_state) {
$form_key = [
'lingotek_locale',
];
if (!$form_state
->isValueEmpty($form_key)) {
$value = $form_state
->getValue($form_key);
if (!self::isValidLocale($value)) {
$form_state
->setErrorByName('lingotek_locale', t('The Lingotek locale %locale does not exist.', [
'%locale' => $value,
]));
}
}
}
/**
* Checks if a locale is valid.
*
* @param string $locale
* The locale to validate.
* @return bool
* TRUE if it's a valid locale in Lingotek. FALSE if not.
*/
public static function isValidLocale($locale) {
$locales = \Drupal::service('lingotek')
->getLocales();
return in_array($locale, $locales);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
LingotekLanguageForm:: |
protected | property | The language-locale mapper. | |
LingotekLanguageForm:: |
protected | property | A lingotek connector object | |
LingotekLanguageForm:: |
public | function | Alters the configurable language entity edit and add form. | |
LingotekLanguageForm:: |
public static | function | Checks if a locale is valid. | |
LingotekLanguageForm:: |
public static | function | Entity builder for the configurable language type form with lingotek options. | |
LingotekLanguageForm:: |
public static | function | Validate the configurable language type form with lingotek options. | |
LingotekLanguageForm:: |
public | function | Constructs a new LingotekConfigTranslationService object. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |