You are here

public function EntityReferenceEntityFormatter::viewElements in Entity reference 8

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

Overrides EntityReferenceFormatterBase::viewElements

File

lib/Drupal/entityreference/Plugin/field/formatter/EntityReferenceEntityFormatter.php, line 82
Definition of Drupal\entityreference\Plugin\field\formatter\EntityReferenceEntityFormatter.

Class

EntityReferenceEntityFormatter
Plugin implementation of the 'entity-reference rendered entity' 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;
  $view_mode = $this
    ->getSetting('view_mode');
  $links = $this
    ->getSetting('links');
  $target_type = $field['settings']['target_type'];
  $elements = array();
  foreach ($items as $delta => $item) {

    // Protect ourselves from recursive rendering.
    static $depth = 0;
    $depth++;
    if ($depth > 20) {
      throw new EntityReferenceRecursiveRenderingException(format_string('Recursive rendering detected when rendering entity @entity_type(@entity_id). Aborting rendering.', array(
        '@entity_type' => $entity_type,
        '@entity_id' => $item['target_id'],
      )));
    }
    $entity = clone $item['entity'];
    unset($entity->content);
    $elements[$delta] = entity_view($entity, $view_mode, $langcode);
    if (empty($links) && isset($result[$delta][$target_type][$item['target_id']]['links'])) {

      // Hide the element links.
      $elements[$delta][$target_type][$item['target_id']]['links']['#access'] = FALSE;
    }
    $depth = 0;
  }
  return $elements;
}