You are here

protected function WebformMapping::formatHtmlItem in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Plugin/WebformElement/WebformMapping.php \Drupal\webform\Plugin\WebformElement\WebformMapping::formatHtmlItem()

Format an element's value as HTML.

Parameters

array $element: An element.

\Drupal\webform\WebformSubmissionInterface $webform_submission: A webform submission.

array $options: An array of options.

Return value

array|string The element's value formatted as HTML or a render array.

Overrides WebformElementBase::formatHtmlItem

File

src/Plugin/WebformElement/WebformMapping.php, line 126

Class

WebformMapping
Provides a 'mapping' element.

Namespace

Drupal\webform\Plugin\WebformElement

Code

protected function formatHtmlItem(array $element, WebformSubmissionInterface $webform_submission, array $options = []) {
  $value = $this
    ->getValue($element, $webform_submission, $options);
  $element += [
    '#destination' => [],
    '#arrow' => '→',
  ];
  $arrow = htmlentities($element['#arrow']);
  $format = $this
    ->getItemFormat($element);
  switch ($format) {
    case 'raw':
      $items = [];
      foreach ($element['#source'] as $source_key => $source_title) {
        $destination_value = isset($value[$source_key]) ? $value[$source_key] : NULL;
        $items[$source_key] = [
          '#markup' => "{$source_key} {$arrow} {$destination_value}",
        ];
      }
      return [
        '#theme' => 'item_list',
        '#items' => $items,
      ];
    case 'table':
      $element += [
        '#source__title' => $this
          ->t('Source'),
        '#destination__title' => $this
          ->t('Destination'),
      ];
      $header = [
        [
          'data' => $element['#source__title'],
          'width' => '50%',
        ],
        [
          'data' => $element['#destination__title'],
          'width' => '50%',
        ],
      ];
      $rows = [];
      foreach ($element['#source'] as $source_key => $source_text) {
        list($source_title) = WebformOptionsHelper::splitOption($source_text);
        $destination_value = isset($value[$source_key]) ? $value[$source_key] : NULL;
        $destination_title = $destination_value ? WebformOptionsHelper::getOptionText($destination_value, $element['#destination']) : $this
          ->t('[blank]');
        $rows[$source_key] = [
          $source_title,
          [
            'data' => [
              '#markup' => "{$arrow} {$destination_title}",
            ],
          ],
        ];
      }
      return [
        '#type' => 'table',
        '#header' => $header,
        '#rows' => $rows,
        '#attributes' => [
          'class' => [
            'webform-mapping-table',
          ],
        ],
      ];
    default:
    case 'value':
    case 'list':
      $items = [];
      foreach ($element['#source'] as $source_key => $source_text) {
        list($source_title) = WebformOptionsHelper::splitOption($source_text);
        $destination_value = isset($value[$source_key]) ? $value[$source_key] : NULL;
        $destination_title = $destination_value ? WebformOptionsHelper::getOptionText($destination_value, $element['#destination']) : $this
          ->t('[blank]');
        $items[$source_key] = [
          '#markup' => "{$source_title} {$arrow} {$destination_title}",
        ];
      }
      return [
        '#theme' => 'item_list',
        '#items' => $items,
      ];
  }
}