You are here

protected function WebformEmailMultiple::formatHtmlItem in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Plugin/WebformElement/WebformEmailMultiple.php \Drupal\webform\Plugin\WebformElement\WebformEmailMultiple::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 Email::formatHtmlItem

File

src/Plugin/WebformElement/WebformEmailMultiple.php, line 38

Class

WebformEmailMultiple
Provides a 'email_multiple' element.

Namespace

Drupal\webform\Plugin\WebformElement

Code

protected function formatHtmlItem(array $element, WebformSubmissionInterface $webform_submission, array $options = []) {
  $value = $this
    ->getValue($element, $webform_submission, $options);
  if (empty($value)) {
    return '';
  }
  $format = $this
    ->getItemFormat($element);
  switch ($format) {
    case 'link':
      $emails = preg_split('/\\s*,\\s*/', $value);
      $total = count($emails);
      $i = 0;
      $links = [];
      foreach ($emails as $email) {
        $links[] = [
          '#type' => 'link',
          '#title' => $email,
          '#url' => \Drupal::pathValidator()
            ->getUrlIfValid('mailto:' . $email),
          '#suffix' => ++$i !== $total ? ', ' : '',
        ];
      }
      return $links;
    default:
      return parent::formatHtmlItem($element, $webform_submission, $options);
  }
}