public function LinkFormatter::viewElements

Same name in this branch
  1. 8.x drupal/core/modules/link/lib/Drupal/link/Plugin/field/formatter/LinkFormatter.php \Drupal\link\Plugin\field\formatter\LinkFormatter::viewElements()
  2. 8.x drupal/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/LinkFormatter.php \Drupal\taxonomy\Plugin\field\formatter\LinkFormatter::viewElements()

Builds a renderable array for a field value.

Parameters

Drupal\Core\Entity\EntityInterface $entity: The entity being displayed.

string $langcode: The language associated with $items.

array $items: Array of values for this field.

Return value

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

Overrides FormatterInterface::viewElements

1 method overrides LinkFormatter::viewElements()
LinkSeparateFormatter::viewElements in drupal/core/modules/link/lib/Drupal/link/Plugin/field/formatter/LinkSeparateFormatter.php
Builds a renderable array for a field value.

File

drupal/core/modules/link/lib/Drupal/link/Plugin/field/formatter/LinkFormatter.php, line 148
Contains \Drupal\link\Plugin\field\formatter\LinkFormatter.

Class

LinkFormatter
Plugin implementation of the 'link' formatter.

Namespace

Drupal\link\Plugin\field\formatter

Code

public function viewElements(EntityInterface $entity, $langcode, array $items) {
  $element = array();
  $settings = $this
    ->getSettings();
  foreach ($items as $delta => $item) {

    // By default use the full URL as the link text.
    $link_title = $item['url'];

    // If the title field value is available, use it for the link text.
    if (empty($settings['url_only']) && !empty($item['title'])) {

      // Unsanitizied token replacement here because $options['html'] is FALSE
      // by default in theme_link().
      $link_title = \Drupal::token()
        ->replace($item['title'], array(
        $entity
          ->entityType() => $entity,
      ), array(
        'sanitize' => FALSE,
        'clear' => TRUE,
      ));
    }

    // Trim the link text to the desired length.
    if (!empty($settings['trim_length'])) {
      $link_title = truncate_utf8($link_title, $settings['trim_length'], FALSE, TRUE);
    }
    if (!empty($settings['url_only']) && !empty($settings['url_plain'])) {
      $element[$delta] = array(
        '#type' => 'markup',
        '#markup' => check_plain($link_title),
      );
    }
    else {
      $element[$delta] = array(
        '#type' => 'link',
        '#title' => $link_title,
        '#href' => $item['path'],
        '#options' => $item['options'],
      );
    }
  }
  return $element;
}