function UriLinkFormatter::viewElements

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/UriLinkFormatter.php \Drupal\Core\Field\Plugin\Field\FieldFormatter\UriLinkFormatter::viewElements()
  2. 10 core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/UriLinkFormatter.php \Drupal\Core\Field\Plugin\Field\FieldFormatter\UriLinkFormatter::viewElements()
  3. 11.x core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/UriLinkFormatter.php \Drupal\Core\Field\Plugin\Field\FieldFormatter\UriLinkFormatter::viewElements()

Overrides FormatterInterface::viewElements

File

core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/UriLinkFormatter.php, line 25

Class

UriLinkFormatter
Plugin implementation of the 'uri_link' formatter.

Namespace

Drupal\Core\Field\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
    $elements = [];
    foreach ($items as $delta => $item) {
        if (!$item->isEmpty()) {
            $elements[$delta] = [
                '#type' => 'link',
                '#url' => Url::fromUri($item->value),
                '#title' => $item->value,
            ];
        }
    }
    return $elements;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.