MailToFormatter.php 777 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. namespace Drupal\Core\Field\Plugin\Field\FieldFormatter;
  3. use Drupal\Core\Field\FormatterBase;
  4. use Drupal\Core\Field\FieldItemListInterface;
  5. use Drupal\Core\Url;
  6. /**
  7. * Plugin implementation of the 'email_mailto' formatter.
  8. *
  9. * @FieldFormatter(
  10. * id = "email_mailto",
  11. * label = @Translation("Email"),
  12. * field_types = {
  13. * "email"
  14. * }
  15. * )
  16. */
  17. class MailToFormatter extends FormatterBase {
  18. /**
  19. * {@inheritdoc}
  20. */
  21. public function viewElements(FieldItemListInterface $items, $langcode) {
  22. $elements = [];
  23. foreach ($items as $delta => $item) {
  24. $elements[$delta] = [
  25. '#type' => 'link',
  26. '#title' => $item->value,
  27. '#url' => Url::fromUri('mailto:' . $item->value),
  28. ];
  29. }
  30. return $elements;
  31. }
  32. }