function LinkFormatter::viewElements

Same name and namespace in other branches
  1. 9 core/modules/link/src/Plugin/Field/FieldFormatter/LinkFormatter.php \Drupal\link\Plugin\Field\FieldFormatter\LinkFormatter::viewElements()
  2. 8.9.x core/modules/link/src/Plugin/Field/FieldFormatter/LinkFormatter.php \Drupal\link\Plugin\Field\FieldFormatter\LinkFormatter::viewElements()
  3. 11.x core/modules/link/src/Plugin/Field/FieldFormatter/LinkFormatter.php \Drupal\link\Plugin\Field\FieldFormatter\LinkFormatter::viewElements()

Builds a renderable array for a field value.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.

string $langcode: The language that should be used to render the field.

Return value

array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.

Overrides FormatterInterface::viewElements

1 method overrides LinkFormatter::viewElements()
LinkSeparateFormatter::viewElements in core/modules/link/src/Plugin/Field/FieldFormatter/LinkSeparateFormatter.php
Builds a renderable array for a field value.

File

core/modules/link/src/Plugin/Field/FieldFormatter/LinkFormatter.php, line 173

Class

LinkFormatter
Plugin implementation of the 'link' formatter.

Namespace

Drupal\link\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $element = [];
  $entity = $items->getEntity();
  $settings = $this->getSettings();
  foreach ($items as $delta => $item) {
    // By default use the full URL as the link text.
    $url = $this->buildUrl($item);
    $link_title = $url->toString();
    // If the title field value is available, use it for the link text.
    if (empty($settings['url_only']) && !empty($item->title)) {
      // Unsanitized token replacement here because the entire link title
      // gets auto-escaped during link generation in
      // \Drupal\Core\Utility\LinkGenerator::generate().
      $link_title = \Drupal::token()->replace($item->title, [
        $entity->getEntityTypeId() => $entity,
      ], [
        'clear' => TRUE,
      ]);
    }
    // Trim the link text to the desired length.
    if (!empty($settings['trim_length'])) {
      $link_title = Unicode::truncate($link_title, $settings['trim_length'], FALSE, TRUE);
    }
    if (!empty($settings['url_only']) && !empty($settings['url_plain'])) {
      $element[$delta] = [
        '#plain_text' => $link_title,
      ];
      if (!empty($item->_attributes)) {
        // Piggyback on the metadata attributes, which will be placed in the
        // field template wrapper, and set the URL value in a content
        // attribute.
        $content = str_replace('internal:/', '', $item->uri);
        $item->_attributes += [
          'content' => $content,
        ];
      }
    }
    else {
      // Skip the #options to prevent duplications of query parameters.
      $element[$delta] = [
        '#type' => 'link',
        '#title' => $link_title,
        '#url' => $url,
      ];
      if (!empty($item->_attributes)) {
        $element[$delta]['#attributes'] = $item->_attributes;
        // Unset field item attributes since they have been included in the
        // formatter output and should not be rendered in the field template.
        unset($item->_attributes);
      }
    }
  }
  return $element;
}

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