You are here

protected function WebformTranslationConfigManager::buildConfigWebformFormOptionsPropertyElement in Webform 6.x

Build config webform form options property element.

Parameters

array $element: The Webform element.

array $translation_element: The Webform element's translated properties.

array $source_element: The Webform element's source properties.

array $parents: The Webform element's parents.

Return value

array A render array containing config webform form options property element.

1 call to WebformTranslationConfigManager::buildConfigWebformFormOptionsPropertyElement()
WebformTranslationConfigManager::buildConfigWebformFormElements in src/WebformTranslationConfigManager.php
Build config webform form elements.

File

src/WebformTranslationConfigManager.php, line 495

Class

WebformTranslationConfigManager
Defines a class to translate webform config.

Namespace

Drupal\webform

Code

protected function buildConfigWebformFormOptionsPropertyElement(array $element, array $translation_element, array $source_element, $property_parents) {
  $property_key = end($property_parents);
  $property_name = '#' . $property_key;
  $webform_element = $this->elementManager
    ->getElementInstance($element);
  $element_property = $this
    ->getWebformElementProperty($webform_element
    ->getPluginId(), $property_name);
  $property_value = $translation_element[$property_name];
  $property_title = isset($element_property['#title']) ? $element_property['#title'] : $property_name;

  // Options (key/value pairs).
  $translation_options = $property_value;
  $source_options = $source_element[$property_name];
  $t_args = [
    '@label' => isset($element['#label']) ? Unicode::ucfirst($element['#label']) : t('Options'),
  ];
  if (!empty($element['#options_description'])) {
    $options_title = t('@label text', $t_args);
  }
  else {
    $options_title = t('@label text -- description', $t_args);
  }

  // Header.
  $header = [
    'source' => [
      'data' => $options_title,
      'width' => '50%',
    ],
    'translation' => [
      'data' => $options_title,
      'width' => '50%',
    ],
  ];

  // Rows.
  $rows = [];
  foreach ($translation_options as $option_value => $option_text) {
    $t_args = [
      '@value' => $option_value,
      '@text' => $source_options[$option_value],
    ];
    $row = [
      'source' => [
        '#markup' => $this
          ->t('@text (@value)', $t_args),
      ],
      'translation' => [
        '#type' => 'textfield',
        '#title' => $this
          ->t('@text (@value)', $t_args),
        '#title_display' => 'invisible',
        '#maxlength' => NULL,
        '#default_value' => $option_text,
        '#parents' => array_merge($property_parents, [
          $option_value,
        ]),
      ],
    ];
    $rows[] = $row;
  }
  return [
    '#type' => 'item',
    '#title' => $property_title,
    'options' => [
      '#type' => 'table',
      '#header' => $header,
    ] + $rows,
  ];
}