You are here

public function YamlFormEmailMultiple::formatHtml in YAML Form 8

Format an element's value as HTML.

Parameters

array $element: An element.

array|mixed $value: A value.

array $options: An array of options.

Return value

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

Overrides Email::formatHtml

File

src/Plugin/YamlFormElement/YamlFormEmailMultiple.php, line 19

Class

YamlFormEmailMultiple
Provides a 'email_multiple' element.

Namespace

Drupal\yamlform\Plugin\YamlFormElement

Code

public function formatHtml(array &$element, $value, array $options = []) {
  if (empty($value)) {
    return '';
  }
  $format = $this
    ->getFormat($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::formatHtml($element, $value, $options);
  }
}