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

File

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

Class

LinkFormatter
Plugin implementation of the 'taxonomy_term_reference_link' formatter.

Namespace

Drupal\taxonomy\Plugin\field\formatter

Code

public function viewElements(EntityInterface $entity, $langcode, array $items) {
  $elements = array();

  // Terms without tid do not exist yet, theme such terms as just their name.
  foreach ($items as $delta => $item) {
    if (!$item['tid']) {
      $elements[$delta] = array(
        '#markup' => check_plain($item['entity']
          ->label()),
      );
    }
    else {
      $term = $item['entity'];
      $uri = $term
        ->uri();
      $elements[$delta] = array(
        '#type' => 'link',
        '#title' => $term
          ->label(),
        '#href' => $uri['path'],
        '#options' => $uri['options'],
      );
    }
  }
  return $elements;
}