You are here

protected function WebformElementBase::formatTextItems in Webform 8.5

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

Format an element's items as text.

Parameters

array $element: An element.

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

array $options: An array of options.

Return value

string The element's items as text.

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

File

src/Plugin/WebformElementBase.php, line 1579

Class

WebformElementBase
Provides a base class for a webform element.

Namespace

Drupal\webform\Plugin

Code

protected function formatTextItems(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
      ->formatTextItem($element, $webform_submission, [
      'delta' => $delta,
    ] + $options);
    if ($item) {
      $items[] = $item;
    }
  }
  if (empty($items)) {
    return '';
  }
  $format = $this
    ->getItemsFormat($element);
  switch ($format) {
    case 'ol':
      $list = [];
      $index = 1;
      foreach ($items as $item) {
        $prefix = $index++ . '. ';
        $list[] = $prefix . str_replace(PHP_EOL, PHP_EOL . str_repeat(' ', strlen($prefix)), $item);
      }
      return implode(PHP_EOL, $list);
    case 'ul':
      $list = [];
      foreach ($items as $index => $item) {
        $list[] = '- ' . str_replace(PHP_EOL, PHP_EOL . '  ', $item);
      }
      return implode(PHP_EOL, $list);
    case 'and':
      return WebformArrayHelper::toString($items);
    default:
    case 'br':
    case 'semicolon':
    case 'comma':
    case 'space':
    case 'hr':
      $delimiters = [
        'hr' => PHP_EOL . '---' . PHP_EOL,
        'br' => PHP_EOL,
        'semicolon' => '; ',
        'comma' => ', ',
        'space' => ' ',
      ];
      $delimiter = isset($delimiters[$format]) ? $delimiters[$format] : $format;
      return implode($delimiter, $items);
  }
}