You are here

public function EntityReferenceEntityFormatter::viewElements in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceEntityFormatter.php \Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceEntityFormatter::viewElements()

Builds a renderable array for a field value.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.

string $langcode: The language that should be used to render the field.

Return value

array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.

Overrides FormatterInterface::viewElements

File

core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceEntityFormatter.php, line 122
Contains \Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceEntityFormatter.

Class

EntityReferenceEntityFormatter
Plugin implementation of the 'entity reference rendered entity' formatter.

Namespace

Drupal\Core\Field\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $view_mode = $this
    ->getSetting('view_mode');
  $elements = array();
  foreach ($this
    ->getEntitiesToView($items, $langcode) as $delta => $entity) {

    // Protect ourselves from recursive rendering.
    static $depth = 0;
    $depth++;
    if ($depth > 20) {
      $this->loggerFactory
        ->get('entity')
        ->error('Recursive rendering detected when rendering entity @entity_type @entity_id. Aborting rendering.', array(
        '@entity_type' => $entity
          ->getEntityTypeId(),
        '@entity_id' => $entity
          ->id(),
      ));
      return $elements;
    }
    if ($entity
      ->id()) {
      $elements[$delta] = entity_view($entity, $view_mode, $entity
        ->language()
        ->getId());

      // Add a resource attribute to set the mapping property's value to the
      // entity's url. Since we don't know what the markup of the entity will
      // be, we shouldn't rely on it for structured data such as RDFa.
      if (!empty($items[$delta]->_attributes)) {
        $items[$delta]->_attributes += array(
          'resource' => $entity
            ->url(),
        );
      }
    }
    else {

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