public function EntityReferenceEntityFormatter::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/EntityReferenceEntityFormatter.php, line 79
Contains \Drupal\entity_reference\Plugin\field\formatter\EntityReferenceEntityFormatter.

Class

EntityReferenceEntityFormatter
Plugin implementation of the 'entity reference rendered entity' 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);
  $view_mode = $this
    ->getSetting('view_mode');
  $links = $this
    ->getSetting('links');
  $target_type = $this->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 RecursiveRenderingException(format_string('Recursive rendering detected when rendering entity @entity_type(@entity_id). Aborting rendering.', array(
        '@entity_type' => $entity_type,
        '@entity_id' => $item['target_id'],
      )));
    }
    if (!empty($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;
      }
    }
    else {

      // This is an "auto_create" item.
      $elements[$delta] = array(
        '#markup' => $entity
          ->label(),
      );
    }
    $depth = 0;
  }
  return $elements;
}