PluralVariants
class PluralVariants extends FormElementBase (View source)
Defines form elements for plurals in configuration translation.
Traits
Wrapper methods for \Drupal\Core\StringTranslation\TranslationInterface.
Properties
| protected TranslationInterface | $stringTranslation | The string translation service. |
from StringTranslationTrait |
| protected TypedDataInterface | $element | The schema element this form is for. |
from FormElementBase |
| protected DataDefinitionInterface | $definition | The data definition of the element this form element is for. |
from FormElementBase |
Methods
Translates a string to the current language or to a given language.
Formats a string containing a count of items.
Returns the number of plurals supported by a given language.
Gets the string translation service.
Builds a render array containing the source and translation form elements.
Returns the source element for a given configuration definition.
Returns the translation form element for a given configuration definition.
Sets configuration based on a nested form value array.
Details
protected TranslatableMarkup
t(string $string, array $args = [], array $options = [])
Translates a string to the current language or to a given language.
See \Drupal\Core\StringTranslation\TranslatableMarkup::__construct() for important security information and usage guidelines.
In order for strings to be localized, make them available in one of the ways supported by the
protected
formatPlural($count, $singular, $plural, array $args = [], array $options = [])
Formats a string containing a count of items.
protected
getNumberOfPlurals($langcode = NULL)
Returns the number of plurals supported by a given language.
protected TranslationInterface
getStringTranslation()
Gets the string translation service.
$this
setStringTranslation(TranslationInterface $translation)
Sets the string translation service to use.
__construct(TypedDataInterface $element)
Constructs a FormElementBase.
static ElementInterface
create(TypedDataInterface $schema)
Creates a form element instance from a schema definition.
array
getTranslationBuild(LanguageInterface $source_language, LanguageInterface $translation_language, mixed $source_config, mixed $translation_config, array $parents, string|null $base_key = NULL)
Builds a render array containing the source and translation form elements.
protected array
getSourceElement(LanguageInterface $source_language, mixed $source_config)
Returns the source element for a given configuration definition.
This can be either a render array that actually outputs the source values directly or a read-only form element with the source values depending on what is considered to provide a more intuitive user interface for the translator.
protected array
getTranslationElement(LanguageInterface $translation_language, mixed $source_config, mixed $translation_config)
Returns the translation form element for a given configuration definition.
For complex data structures (such as mappings) that are translatable wholesale but contain non-translatable properties, the form element is responsible for checking access to the source value of those properties. In case of formatted text, for example, access to the source text format must be checked. If the translator does not have access to the text format, the textarea must be disabled and the translator may not be able to translate this particular configuration element. If the translator does have access to the text format, the element must be locked down to that particular text format; in other words, the format may not be changed by the translator (because the text format property is not itself translatable).
In addition, the form element is responsible for checking whether the value of such non-translatable properties in the translated configuration is equal to the corresponding source values. If not, that means that the source value has changed after the translation was added. In this case - again - the translation of this element must be disabled if the translator does not have access to the source value of the non-translatable property. For example, if a formatted text element, whose source format was plain text when it was first translated, gets changed to the Full HTML format, simply changing the format of the translation would lead to an XSS vulnerability as the translated text, that was intended to be escaped, would now be displayed unescaped. Thus, if the translator does not have access to the Full HTML format, the translation for this particular element may not be updated at all (the textarea must be disabled). Only if access to the Full HTML format is granted, an explicit translation taking into account the updated source value(s) may be submitted.
In the specific case of formatted text this logic is implemented by utilizing a form element of type 'text_format' and its #format and
allowed_formats properties. The access logic explained above is then
handled by the 'text_format' element itself, specifically by \Drupal\filter\Element\TextFormat::processFormat(). In case such a rich element is not available for translation of complex data, similar access logic must be implemented manually.
setConfig(Config $base_config, LanguageConfigOverride $config_translation, mixed $config_values, string|null $base_key = NULL)
Sets configuration based on a nested form value array.
If the configuration values are the same as the source configuration, the override should be removed from the translation configuration.