You are here

protected function LayoutParagraphsFormatter::getEntitiesToView in Layout Paragraphs 2.0.x

Same name and namespace in other branches
  1. 1.0.x src/Plugin/Field/FieldFormatter/LayoutParagraphsFormatter.php \Drupal\layout_paragraphs\Plugin\Field\FieldFormatter\LayoutParagraphsFormatter::getEntitiesToView()

Returns the referenced entities for display.

See \Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase::getEntitiesToView().

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()

File

src/Plugin/Field/FieldFormatter/LayoutParagraphsFormatter.php, line 41

Class

LayoutParagraphsFormatter
Layout Paragraphs field formatter.

Namespace

Drupal\layout_paragraphs\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() and
    // items that are not root level components.
    if (!empty($item->_loaded) && LayoutParagraphsComponent::isRootComponent($item->entity)) {
      $entity = $item->entity;
      $access = $this
        ->checkAccess($entity);

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

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

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