You are here

protected function EntityReferenceFormatterBase::getEntitiesToView in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceFormatterBase.php \Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase::getEntitiesToView()
  2. 10 core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceFormatterBase.php \Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase::getEntitiesToView()

Returns the referenced entities for display.

The method takes care of:

  • checking entity access,
  • placing the entities in the language expected for display.

It is thus strongly recommended that formatters use it in their implementation of viewElements($items) rather than dealing with $items directly.

For each entity, the EntityReferenceItem by which the entity is referenced is available in $entity->_referringItem. This is useful for field types that store additional values next to the reference itself.

Parameters

\Drupal\Core\Field\EntityReferenceFieldItemListInterface $items: The item list.

string $langcode: The language code of the referenced entities to display.

Return value

\Drupal\Core\Entity\EntityInterface[] The array of referenced entities to display, keyed by delta.

See also

::prepareView()

11 calls to EntityReferenceFormatterBase::getEntitiesToView()
AuthorFormatter::viewElements in core/modules/user/src/Plugin/Field/FieldFormatter/AuthorFormatter.php
Builds a renderable array for a field value.
EntityReferenceEntityFormatter::viewElements in core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceEntityFormatter.php
Builds a renderable array for a field value.
EntityReferenceIdFormatter::viewElements in core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceIdFormatter.php
Builds a renderable array for a field value.
EntityReferenceLabelFormatter::viewElements in core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceLabelFormatter.php
Builds a renderable array for a field value.
EntityReferenceTaxonomyTermRssFormatter::viewElements in core/modules/taxonomy/src/Plugin/Field/FieldFormatter/EntityReferenceTaxonomyTermRssFormatter.php
Builds a renderable array for a field value.

... See full list

1 method overrides EntityReferenceFormatterBase::getEntitiesToView()
ImageFormatterBase::getEntitiesToView in core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatterBase.php
Returns the referenced entities for display.

File

core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceFormatterBase.php, line 42

Class

EntityReferenceFormatterBase
Parent plugin for entity reference formatters.

Namespace

Drupal\Core\Field\Plugin\Field\FieldFormatter

Code

protected function getEntitiesToView(EntityReferenceFieldItemListInterface $items, $langcode) {
  $entities = [];
  foreach ($items as $delta => $item) {

    // Ignore items where no entity could be loaded in prepareView().
    if (!empty($item->_loaded)) {
      $entity = $item->entity;

      // Set the entity in the correct language for display.
      if ($entity instanceof TranslatableInterface) {
        $entity = \Drupal::service('entity.repository')
          ->getTranslationFromContext($entity, $langcode);
      }
      $access = $this
        ->checkAccess($entity);

      // Add the access result's cacheability, ::view() needs it.
      $item->_accessCacheability = CacheableMetadata::createFromObject($access);
      if ($access
        ->isAllowed()) {

        // Add the referring item, in case the formatter needs it.
        $entity->_referringItem = $items[$delta];
        $entities[$delta] = $entity;
      }
    }
  }
  return $entities;
}