Skip to content
LinkFormatter.php 1.69 KiB
Newer Older
<?php

/**
 * @file
 * Contains \Drupal\taxonomy\Plugin\field\formatter\LinkFormatter.
 */

namespace Drupal\taxonomy\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;

/**
 * Plugin implementation of the 'taxonomy_term_reference_link' formatter.
 *
 *   id = "taxonomy_term_reference_link",
 *   label = @Translation("Link"),
 *   field_types = {
 *     "taxonomy_term_reference"
 *   }
 * )
 */
class LinkFormatter extends TaxonomyFormatterBase {

  /**
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items) {
    // Terms without target_id do not exist yet, theme such terms as just their
    // name.
    foreach ($items as $delta => $item) {
          '#markup' => check_plain($item->entity->label()),
        /** @var $term \Drupal\taxonomy\TermInterface */
        $elements[$delta] = array(
          '#type' => 'link',
          '#title' => $term->label(),
          '#route_name' => $uri['route_name'],
          '#route_parameters' => $uri['route_parameters'],

        if (!empty($item->_attributes)) {
          $elements[$delta]['#options'] += array('attributes' => array());
          $elements[$delta]['#options']['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);
        }