You are here

protected function PluralVariants::getSourceElement in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 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 23
Contains \Drupal\config_translation\FormElement\PluralVariants.

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(LOCALE_PLURAL_DELIMITER, $source_config);
  $element = array(
    '#type' => 'fieldset',
    '#title' => SafeMarkup::format('@label <span class="visually-hidden">(@source_language)</span>', array(
      '@label' => $this
        ->t($this->definition
        ->getLabel()),
      '@source_language' => $source_language
        ->getName(),
    )),
    '#tree' => TRUE,
  );
  for ($i = 0; $i < $plurals; $i++) {
    $element[$i] = array(
      '#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' => SafeMarkup::format('<span lang="@langcode">@value</span>', array(
        '@langcode' => $source_language
          ->getId(),
        '@value' => isset($values[$i]) ? $values[$i] : $this
          ->t('(Empty)'),
      )),
    );
  }
  return $element;
}