function FormElementBase::getTranslationElement
Same name in other branches
- 9 core/modules/config_translation/src/FormElement/FormElementBase.php \Drupal\config_translation\FormElement\FormElementBase::getTranslationElement()
- 8.9.x core/modules/config_translation/src/FormElement/FormElementBase.php \Drupal\config_translation\FormElement\FormElementBase::getTranslationElement()
- 11.x core/modules/config_translation/src/FormElement/FormElementBase.php \Drupal\config_translation\FormElement\FormElementBase::getTranslationElement()
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.
Parameters
\Drupal\Core\Language\LanguageInterface $translation_language: The language to display the translation form for.
mixed $source_config: The configuration value of the element in the source language.
mixed $translation_config: The configuration value of the element in the language to translate to.
Return value
array Form API array to represent the form element.
See also
\Drupal\config_translation\FormElement\TextFormat
\Drupal\filter\Element\TextFormat::processFormat()
5 calls to FormElementBase::getTranslationElement()
- DateFormat::getTranslationElement in core/
modules/ config_translation/ src/ FormElement/ DateFormat.php - Returns the translation form element for a given configuration definition.
- FormElementBase::getTranslationBuild in core/
modules/ config_translation/ src/ FormElement/ FormElementBase.php - Builds a render array containing the source and translation form elements.
- Textarea::getTranslationElement in core/
modules/ config_translation/ src/ FormElement/ Textarea.php - Returns the translation form element for a given configuration definition.
- Textfield::getTranslationElement in core/
modules/ config_translation/ src/ FormElement/ Textfield.php - Returns the translation form element for a given configuration definition.
- TextFormat::getTranslationElement in core/
modules/ config_translation/ src/ FormElement/ TextFormat.php - Returns the translation form element for a given configuration definition.
5 methods override FormElementBase::getTranslationElement()
- DateFormat::getTranslationElement in core/
modules/ config_translation/ src/ FormElement/ DateFormat.php - Returns the translation form element for a given configuration definition.
- PluralVariants::getTranslationElement in core/
modules/ config_translation/ src/ FormElement/ PluralVariants.php - Returns the translation form element for a given configuration definition.
- Textarea::getTranslationElement in core/
modules/ config_translation/ src/ FormElement/ Textarea.php - Returns the translation form element for a given configuration definition.
- Textfield::getTranslationElement in core/
modules/ config_translation/ src/ FormElement/ Textfield.php - Returns the translation form element for a given configuration definition.
- TextFormat::getTranslationElement in core/
modules/ config_translation/ src/ FormElement/ TextFormat.php - Returns the translation form element for a given configuration definition.
File
-
core/
modules/ config_translation/ src/ FormElement/ FormElementBase.php, line 158
Class
- FormElementBase
- Provides a common base class for form elements.
Namespace
Drupal\config_translation\FormElementCode
protected function getTranslationElement(LanguageInterface $translation_language, $source_config, $translation_config) {
// Add basic properties that apply to all form elements.
return [
'#title' => $this->t('@label <span class="visually-hidden">(@source_language)</span>', [
// Labels originate from configuration schema and are translatable.
'@label' => $this->t($this->definition
->getLabel()),
'@source_language' => $translation_language->getName(),
]),
'#default_value' => $translation_config,
'#attributes' => [
'lang' => $translation_language->getId(),
],
];
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.