You are here

protected function PluralVariants::getSourceElement 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::getSourceElement()
  2. 10 core/modules/config_translation/src/FormElement/PluralVariants.php \Drupal\config_translation\FormElement\PluralVariants::getSourceElement()

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.

Parameters

\Drupal\Core\Language\LanguageInterface $source_language: Thee source language of the configuration object.

mixed $source_config: The configuration value of the element in the source language.

Return value

array A render array for the source value.

Overrides FormElementBase::getSourceElement

File

core/modules/config_translation/src/FormElement/PluralVariants.php, line 19

Class

PluralVariants
Defines form elements for plurals in configuration translation.

Namespace

Drupal\config_translation\FormElement

Code

protected function getSourceElement(LanguageInterface $source_language, $source_config) {
  $plurals = $this
    ->getNumberOfPlurals($source_language
    ->getId());
  $values = explode(PoItem::DELIMITER, $source_config);
  $element = [
    '#type' => 'fieldset',
    '#title' => new FormattableMarkup('@label <span class="visually-hidden">(@source_language)</span>', [
      // Labels originate from configuration schema and are translatable.
      '@label' => $this
        ->t($this->definition
        ->getLabel()),
      '@source_language' => $source_language
        ->getName(),
    ]),
    '#tree' => TRUE,
  ];
  for ($i = 0; $i < $plurals; $i++) {
    $element[$i] = [
      '#type' => 'item',
      // @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'),
      '#markup' => new FormattableMarkup('<span lang="@langcode">@value</span>', [
        '@langcode' => $source_language
          ->getId(),
        '@value' => isset($values[$i]) ? $values[$i] : $this
          ->t('(Empty)'),
      ]),
    ];
  }
  return $element;
}