function EntityReferenceIdFormatter::viewElements

Same name 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()
  3. 11.x core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceIdFormatter.php \Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceIdFormatter::viewElements()

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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.