You are here

public function DateFormat::getTranslationElement in Drupal 8

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

Class

DateFormat
Defines the date format element for the configuration translation interface.

Namespace

Drupal\config_translation\FormElement

Code

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

  /** @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */
  $date_formatter = \Drupal::service('date.formatter');
  $description = $this
    ->t('A user-defined date format. See the <a href="http://php.net/manual/function.date.php">PHP manual</a> for available options.');
  $format = $this
    ->t('Displayed as %date_format', [
    '%date_format' => $date_formatter
      ->format(REQUEST_TIME, 'custom', $translation_config),
  ]);
  return [
    '#type' => 'textfield',
    '#description' => $description,
    '#field_suffix' => ' <small data-drupal-date-formatter="preview">' . $format . '</small>',
    '#attributes' => [
      'data-drupal-date-formatter' => 'source',
    ],
    '#attached' => [
      'drupalSettings' => [
        'dateFormats' => $date_formatter
          ->getSampleDateFormats($translation_language
          ->getId()),
      ],
      'library' => [
        'system/drupal.system.date',
      ],
    ],
  ] + parent::getTranslationElement($translation_language, $source_config, $translation_config);
}