You are here

protected function PluralVariants::getTranslationElement in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/config_translation/src/FormElement/PluralVariants.php \Drupal\config_translation\FormElement\PluralVariants::getTranslationElement()
  2. 10 core/modules/config_translation/src/FormElement/PluralVariants.php \Drupal\config_translation\FormElement\PluralVariants::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/PluralVariants.php, line 48

Class

PluralVariants
Defines form elements for plurals in configuration translation.

Namespace

Drupal\config_translation\FormElement

Code

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' => isset($values[$i]) ? $values[$i] : '',
      '#attributes' => [
        'lang' => $translation_language
          ->getId(),
      ],
    ];
  }
  return $element;
}