You are here

public function EntityReferenceLabelFormatter::viewElements in Entity reference 8

Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::viewElements().

Overrides EntityReferenceFormatterBase::viewElements

File

lib/Drupal/entityreference/Plugin/field/formatter/EntityReferenceLabelFormatter.php, line 60
Definition of Drupal\entityreference\Plugin\field\formatter\EntityReferenceLabelFormatter.

Class

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

Namespace

Drupal\entityreference\Plugin\field\formatter

Code

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

  // Remove un-accessible items.
  parent::viewElements($entity, $langcode, $items);
  $instance = $this->instance;
  $field = $this->field;
  $elements = array();
  foreach ($items as $delta => $item) {
    $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(
        '#markup' => l($label, $uri['path'], $uri['options']),
      );
    }
    else {
      $elements[$delta] = array(
        '#markup' => check_plain($label),
      );
    }
  }
  return $elements;
}