You are here

public function EntityReferenceIdFormatter::viewElements in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceIdFormatter.php \Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceIdFormatter::viewElements()
  2. 10 core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceIdFormatter.php \Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceIdFormatter::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/EntityReferenceIdFormatter.php, line 24

Class

EntityReferenceIdFormatter
Plugin implementation of the 'entity reference ID' formatter.

Namespace

Drupal\Core\Field\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];
  foreach ($this
    ->getEntitiesToView($items, $langcode) as $delta => $entity) {
    if ($entity
      ->id()) {
      $elements[$delta] = [
        '#plain_text' => $entity
          ->id(),
        // Create a cache tag entry for the referenced entity. In the case
        // that the referenced entity is deleted, the cache for referring
        // entities must be cleared.
        '#cache' => [
          'tags' => $entity
            ->getCacheTags(),
        ],
      ];
    }
  }
  return $elements;
}