MailToFormatter.php 833 Bytes
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\email\Plugin\Field\FieldFormatter\MailToFormatter.
6 7
 */

8
namespace Drupal\email\Plugin\Field\FieldFormatter;
9

10
use Drupal\Core\Field\FormatterBase;
11
use Drupal\Core\Entity\Field\FieldItemListInterface;
12 13 14 15

/**
 * Plugin implementation of the 'email_mailto' formatter.
 *
16
 * @FieldFormatter(
17 18 19 20 21 22 23 24 25 26
 *   id = "email_mailto",
 *   label = @Translation("Email"),
 *   field_types = {
 *     "email"
 *   }
 * )
 */
class MailToFormatter extends FormatterBase {

  /**
27
   * {@inheritdoc}
28
   */
29
  public function viewElements(FieldItemListInterface $items) {
30 31 32 33 34
    $elements = array();

    foreach ($items as $delta => $item) {
      $elements[$delta] = array(
        '#type' => 'link',
35 36
        '#title' => $item->value,
        '#href' => 'mailto:' . $item->value,
37 38 39 40 41 42 43
      );
    }

    return $elements;
  }

}