class PluralVariants in Zircon Profile 8.0
Same name and namespace in other branches
- 8 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 ElementInterface uses StringTranslationTrait
- class \Drupal\config_translation\FormElement\PluralVariants
Expanded class hierarchy of PluralVariants
File
- core/
modules/ config_translation/ src/ FormElement/ PluralVariants.php, line 18 - Contains \Drupal\config_translation\FormElement\PluralVariants.
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(LOCALE_PLURAL_DELIMITER, $source_config);
$element = array(
'#type' => 'fieldset',
'#title' => SafeMarkup::format('@label <span class="visually-hidden">(@source_language)</span>', array(
'@label' => $this
->t($this->definition
->getLabel()),
'@source_language' => $source_language
->getName(),
)),
'#tree' => TRUE,
);
for ($i = 0; $i < $plurals; $i++) {
$element[$i] = array(
'#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' => SafeMarkup::format('<span lang="@langcode">@value</span>', array(
'@langcode' => $source_language
->getId(),
'@value' => isset($values[$i]) ? $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(LOCALE_PLURAL_DELIMITER, $translation_config);
$element = array(
'#type' => 'fieldset',
'#title' => SafeMarkup::format('@label <span class="visually-hidden">(@translation_language)</span>', array(
'@label' => $this
->t($this->definition
->getLabel()),
'@translation_language' => $translation_language
->getName(),
)),
'#tree' => TRUE,
);
for ($i = 0; $i < $plurals; $i++) {
$element[$i] = array(
'#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' => isset($values[$i]) ? $values[$i] : '',
'#attributes' => array(
'lang' => $translation_language
->getId(),
),
);
}
return $element;
}
/**
* {@inheritdoc}
*/
public function setConfig(Config $base_config, LanguageConfigOverride $config_translation, $config_values, $base_key = NULL) {
$config_values = implode(LOCALE_PLURAL_DELIMITER, $config_values);
parent::setConfig($base_config, $config_translation, $config_values, $base_key);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FormElementBase:: |
protected | property | The data definition of the element this form element is for. | |
FormElementBase:: |
protected | property | The schema element this form is for. | |
FormElementBase:: |
public static | function |
Creates a form element instance from a schema definition. Overrides ElementInterface:: |
|
FormElementBase:: |
public | function |
Builds a render array containg the source and translation form elements. Overrides ElementInterface:: |
|
FormElementBase:: |
public | function | Constructs a FormElementBase. | |
PluralVariants:: |
protected | function |
Returns the source element for a given configuration definition. Overrides FormElementBase:: |
|
PluralVariants:: |
protected | function |
Returns the translation form element for a given configuration definition. Overrides FormElementBase:: |
|
PluralVariants:: |
public | function |
Sets configuration based on a nested form value array. Overrides FormElementBase:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | |
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. |