You are here

public function Textarea::getTranslationElement in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/config_translation/src/FormElement/Textarea.php \Drupal\config_translation\FormElement\Textarea::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.

Overrides FormElementBase::getTranslationElement

See also

\Drupal\config_translation\FormElement\TextFormat

\Drupal\filter\Element\TextFormat::processFormat()

File

core/modules/config_translation/src/FormElement/Textarea.php, line 15

Class

Textarea
Defines the textarea element for the configuration translation interface.

Namespace

Drupal\config_translation\FormElement

Code

public function getTranslationElement(LanguageInterface $translation_language, $source_config, $translation_config) {

  // Estimate a comfortable size of the input textarea.
  $rows_words = ceil(str_word_count($translation_config) / 5);
  $rows_newlines = substr_count($translation_config, "\n") + 1;
  $rows = max($rows_words, $rows_newlines);
  return [
    '#type' => 'textarea',
    '#rows' => $rows,
  ] + parent::getTranslationElement($translation_language, $source_config, $translation_config);
}