Version 1
[yaffs-website] / web / core / lib / Drupal / Core / Field / Plugin / Field / FieldFormatter / UriLinkFormatter.php
1 <?php
2
3 namespace Drupal\Core\Field\Plugin\Field\FieldFormatter;
4
5 use Drupal\Core\Field\FormatterBase;
6 use Drupal\Core\Field\FieldItemListInterface;
7 use Drupal\Core\Url;
8
9 /**
10  * Plugin implementation of the 'uri_link' formatter.
11  *
12  * @FieldFormatter(
13  *   id = "uri_link",
14  *   label = @Translation("Link to URI"),
15  *   field_types = {
16  *     "uri",
17  *   }
18  * )
19  */
20 class UriLinkFormatter extends FormatterBase {
21
22   /**
23    * {@inheritdoc}
24    */
25   public function viewElements(FieldItemListInterface $items, $langcode) {
26     $elements = [];
27
28     foreach ($items as $delta => $item) {
29       if (!$item->isEmpty()) {
30         $elements[$delta] = [
31           '#type' => 'link',
32           '#url' => Url::fromUri($item->value),
33           '#title' => $item->value,
34         ];
35       }
36     }
37
38     return $elements;
39   }
40
41 }