You are here

protected function WebformMapping::formatTextItem in Webform 6.x

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

Format an element's value 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 value formatted as text.

Overrides WebformElementBase::formatTextItem

See also

_webform_token_get_submission_value()

File

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

Class

WebformMapping
Provides a 'mapping' element.

Namespace

Drupal\webform\Plugin\WebformElement

Code

protected function formatTextItem(array $element, WebformSubmissionInterface $webform_submission, array $options = []) {
  if ($this
    ->hasValue($element, $webform_submission, $options)) {
    return '';
  }
  $value = $this
    ->getValue($element, $webform_submission, $options);
  $element += [
    '#destination' => [],
    '#arrow' => '→',
  ];
  $arrow = $element['#arrow'];
  $format = $this
    ->getItemFormat($element);
  switch ($format) {
    case 'raw':
      $list = [];
      foreach ($element['#source'] as $source_key => $source_title) {
        $destination_value = isset($value[$source_key]) ? $value[$source_key] : NULL;
        $list[$source_key] = "{$source_key} {$arrow} {$destination_value}";
      }
      return implode(PHP_EOL, $list);
    default:
    case 'value':
    case 'table':
    case 'list':
      $list = [];
      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]');
        $list[] = "{$source_title} {$arrow} {$destination_title}";
      }
      return implode(PHP_EOL, $list);
  }
}