class PluralVariants
Same name in other branches
- 8.9.x core/modules/config_translation/src/FormElement/PluralVariants.php \Drupal\config_translation\FormElement\PluralVariants
- 10 core/modules/config_translation/src/FormElement/PluralVariants.php \Drupal\config_translation\FormElement\PluralVariants
- 11.x core/modules/config_translation/src/FormElement/PluralVariants.php \Drupal\config_translation\FormElement\PluralVariants
Defines form elements for plurals in configuration translation.
Hierarchy
- class \Drupal\config_translation\FormElement\FormElementBase implements \Drupal\config_translation\FormElement\ElementInterface uses \Drupal\Core\StringTranslation\StringTranslationTrait
- class \Drupal\config_translation\FormElement\PluralVariants extends \Drupal\config_translation\FormElement\FormElementBase
Expanded class hierarchy of PluralVariants
File
-
core/
modules/ config_translation/ src/ FormElement/ PluralVariants.php, line 14
Namespace
Drupal\config_translation\FormElementView source
class PluralVariants extends FormElementBase {
/**
* {@inheritdoc}
*/
protected function getSourceElement(LanguageInterface $source_language, $source_config) {
$plurals = $this->getNumberOfPlurals($source_language->getId());
$values = explode(PoItem::DELIMITER, $source_config);
$element = [
'#type' => 'fieldset',
'#title' => new FormattableMarkup('@label <span class="visually-hidden">(@source_language)</span>', [
// Labels originate from configuration schema and are translatable.
'@label' => $this->t($this->definition
->getLabel()),
'@source_language' => $source_language->getName(),
]),
'#tree' => TRUE,
];
for ($i = 0; $i < $plurals; $i++) {
$element[$i] = [
'#type' => 'item',
// @todo Should use better labels https://www.drupal.org/node/2499639
'#title' => $i == 0 ? $this->t('Singular form') : $this->formatPlural($i, 'First plural form', '@count. plural form'),
'#markup' => new FormattableMarkup('<span lang="@langcode">@value</span>', [
'@langcode' => $source_language->getId(),
'@value' => $values[$i] ?? $this->t('(Empty)'),
]),
];
}
return $element;
}
/**
* {@inheritdoc}
*/
protected function getTranslationElement(LanguageInterface $translation_language, $source_config, $translation_config) {
$plurals = $this->getNumberOfPlurals($translation_language->getId());
$values = explode(PoItem::DELIMITER, $translation_config);
$element = [
'#type' => 'fieldset',
'#title' => new FormattableMarkup('@label <span class="visually-hidden">(@translation_language)</span>', [
// Labels originate from configuration schema and are translatable.
'@label' => $this->t($this->definition
->getLabel()),
'@translation_language' => $translation_language->getName(),
]),
'#tree' => TRUE,
];
for ($i = 0; $i < $plurals; $i++) {
$element[$i] = [
'#type' => 'textfield',
// @todo Should use better labels https://www.drupal.org/node/2499639
'#title' => $i == 0 ? $this->t('Singular form') : $this->formatPlural($i, 'First plural form', '@count. plural form'),
'#default_value' => $values[$i] ?? '',
'#attributes' => [
'lang' => $translation_language->getId(),
],
];
}
return $element;
}
/**
* {@inheritdoc}
*/
public function setConfig(Config $base_config, LanguageConfigOverride $config_translation, $config_values, $base_key = NULL) {
$config_values = implode(PoItem::DELIMITER, $config_values);
parent::setConfig($base_config, $config_translation, $config_values, $base_key);
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|
FormElementBase::$definition | protected | property | The data definition of the element this form element is for. | ||
FormElementBase::$element | protected | property | The schema element this form is for. | ||
FormElementBase::create | public static | function | Creates a form element instance from a schema definition. | Overrides ElementInterface::create | |
FormElementBase::getTranslationBuild | public | function | Builds a render array containing the source and translation form elements. | Overrides ElementInterface::getTranslationBuild | |
FormElementBase::__construct | public | function | Constructs a FormElementBase. | ||
PluralVariants::getSourceElement | protected | function | Returns the source element for a given configuration definition. | Overrides FormElementBase::getSourceElement | |
PluralVariants::getTranslationElement | protected | function | Returns the translation form element for a given configuration definition. | Overrides FormElementBase::getTranslationElement | |
PluralVariants::setConfig | public | function | Sets configuration based on a nested form value array. | Overrides FormElementBase::setConfig | |
StringTranslationTrait::$stringTranslation | protected | property | The string translation service. | 3 | |
StringTranslationTrait::formatPlural | protected | function | Formats a string containing a count of items. | ||
StringTranslationTrait::getNumberOfPlurals | protected | function | Returns the number of plurals supported by a given language. | ||
StringTranslationTrait::getStringTranslation | protected | function | Gets the string translation service. | ||
StringTranslationTrait::setStringTranslation | public | function | Sets the string translation service to use. | 2 | |
StringTranslationTrait::t | protected | function | Translates a string to the current language or to a given language. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.