You are here

protected function WebformElementBase::formatHtmlItems in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Plugin/WebformElementBase.php \Drupal\webform\Plugin\WebformElementBase::formatHtmlItems()

Format an element's items as HTML.

Parameters

array $element: An element.

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

array $options: An array of options.

Return value

string|array The element's items as HTML.

2 calls to WebformElementBase::formatHtmlItems()
OptionsBase::formatHtmlItems in src/Plugin/WebformElement/OptionsBase.php
Format an element's items as HTML.
WebformCompositeBase::formatHtmlItems in src/Plugin/WebformElement/WebformCompositeBase.php
Format an element's items as HTML.
2 methods override WebformElementBase::formatHtmlItems()
OptionsBase::formatHtmlItems in src/Plugin/WebformElement/OptionsBase.php
Format an element's items as HTML.
WebformCompositeBase::formatHtmlItems in src/Plugin/WebformElement/WebformCompositeBase.php
Format an element's items as HTML.

File

src/Plugin/WebformElementBase.php, line 1488

Class

WebformElementBase
Provides a base class for a webform element.

Namespace

Drupal\webform\Plugin

Code

protected function formatHtmlItems(array &$element, WebformSubmissionInterface $webform_submission, array $options = []) {
  $value = $this
    ->getValue($element, $webform_submission, $options);

  // Get items.
  $items = [];
  foreach (array_keys($value) as $delta) {
    $item = $this
      ->formatHtmlItem($element, $webform_submission, [
      'delta' => $delta,
    ] + $options);
    if ($item) {
      $items[] = $item;
    }
  }
  if (empty($items)) {
    return [];
  }
  $format = $this
    ->getItemsFormat($element);
  switch ($format) {
    case 'ol':
    case 'ul':
      return [
        '#theme' => 'item_list',
        '#items' => $items,
        '#list_type' => $format,
      ];
    case 'and':
      $total = count($items);
      if ($total === 1) {
        $item = current($items);
        return is_array($item) ? $item : [
          '#markup' => $item,
        ];
      }
      $build = [];
      foreach ($items as $index => &$item) {
        $build[] = is_array($item) ? $item : [
          '#markup' => $item,
        ];
        if ($total === 2 && $index === 0) {
          $build[] = [
            '#markup' => ' ' . $this
              ->t('and') . ' ',
          ];
        }
        elseif ($index !== $total - 1) {
          if ($index === $total - 2) {
            $build[] = [
              '#markup' => ', ' . $this
                ->t('and') . ' ',
            ];
          }
          else {
            $build[] = [
              '#markup' => ', ',
            ];
          }
        }
      }
      return $build;
    default:
    case 'br':
    case 'semicolon':
    case 'comma':
    case 'space':
    case 'hr':
      $delimiters = [
        'hr' => '<hr class="webform-horizontal-rule" />',
        'br' => '<br />',
        'semicolon' => '; ',
        'comma' => ', ',
        'space' => ' ',
      ];
      $delimiter = isset($delimiters[$format]) ? $delimiters[$format] : $format;
      $total = count($items);
      $build = [];
      foreach ($items as $index => &$item) {
        $build[] = is_array($item) ? $item : [
          '#markup' => $item,
        ];
        if ($index !== $total - 1) {
          $build[] = [
            '#markup' => $delimiter,
          ];
        }
      }
      return $build;
  }
}