public function EntityReferenceLabelFormatter::viewElements

Overrides \Drupal\field\Plugin\Type\Formatter\FormatterBase::viewElements().

Overrides EntityReferenceFormatterBase::viewElements

See also

\Drupal\entity_reference\Plugin\field\formatter\EntityReferenceFormatterBase::viewElements().

File

drupal/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/formatter/EntityReferenceLabelFormatter.php, line 58
Contains \Drupal\entity_reference\Plugin\field\formatter\EntityReferenceLabelFormatter.

Class

EntityReferenceLabelFormatter
Plugin implementation of the 'entity reference label' formatter.

Namespace

Drupal\entity_reference\Plugin\field\formatter

Code

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

  // Remove un-accessible items.
  parent::viewElements($entity, $langcode, $items);
  $elements = array();
  foreach ($items as $delta => $item) {
    if ($entity = $item['entity']) {
      $label = $entity
        ->label();

      // If the link is to be displayed and the entity has a uri,
      // display a link.
      if ($this
        ->getSetting('link') && ($uri = $entity
        ->uri())) {
        $elements[$delta] = array(
          '#type' => 'link',
          '#title' => $label,
          '#href' => $uri['path'],
          '#options' => $uri['options'],
        );
      }
      else {
        $elements[$delta] = array(
          '#markup' => check_plain($label),
        );
      }
    }
  }
  return $elements;
}