Same name and namespace in other branches
  1. 8.9.x core/lib/Drupal/Core/Entity/EntityViewBuilder.php \Drupal\Core\Entity\EntityViewBuilder::viewMultiple()
  2. 9 core/lib/Drupal/Core/Entity/EntityViewBuilder.php \Drupal\Core\Entity\EntityViewBuilder::viewMultiple()

Builds the render array for the provided entities.

Parameters

array $entities: An array of entities implementing EntityInterface to view.

string $view_mode: (optional) The view mode that should be used to render the entity.

string $langcode: (optional) For which language the entity should be rendered, defaults to the current content language.

Return value

array A render array for the entities, indexed by the same keys as the entities array passed in $entities.

Throws

\InvalidArgumentException Can be thrown when the set of parameters is inconsistent, like when trying to view Comments and passing a Node which is not the one the comments belongs to, or not passing one, and having the comments node not be available for loading.

Overrides EntityViewBuilderInterface::viewMultiple

2 calls to EntityViewBuilder::viewMultiple()
BlockContentViewBuilder::viewMultiple in core/modules/block_content/src/BlockContentViewBuilder.php
Builds the render array for the provided entities.
EntityViewBuilder::view in core/lib/Drupal/Core/Entity/EntityViewBuilder.php
Builds the render array for the provided entity.
4 methods override EntityViewBuilder::viewMultiple()
BlockContentViewBuilder::viewMultiple in core/modules/block_content/src/BlockContentViewBuilder.php
Builds the render array for the provided entities.
BlockViewBuilder::viewMultiple in core/modules/block/src/BlockViewBuilder.php
Builds the render array for the provided entities.
EntityTestViewBuilderOverriddenView::viewMultiple in core/modules/system/tests/modules/entity_test/src/EntityTestViewBuilderOverriddenView.php
Builds the render array for the provided entities.
TourViewBuilder::viewMultiple in core/modules/tour/src/TourViewBuilder.php
Builds the render array for the provided entities.

File

core/lib/Drupal/Core/Entity/EntityViewBuilder.php, line 145

Class

EntityViewBuilder
Base class for entity view builders.

Namespace

Drupal\Core\Entity

Code

public function viewMultiple(array $entities = [], $view_mode = 'full', $langcode = NULL) {
  $build_list = [
    '#sorted' => TRUE,
    '#pre_render' => [
      [
        $this,
        'buildMultiple',
      ],
    ],
  ];
  $weight = 0;
  foreach ($entities as $key => $entity) {

    // Ensure that from now on we are dealing with the proper translation
    // object.
    $entity = $this->entityRepository
      ->getTranslationFromContext($entity, $langcode);

    // Set build defaults.
    $build_list[$key] = $this
      ->getBuildDefaults($entity, $view_mode);
    $entityType = $this->entityTypeId;
    $this
      ->moduleHandler()
      ->alter([
      $entityType . '_build_defaults',
      'entity_build_defaults',
    ], $build_list[$key], $entity, $view_mode);
    $build_list[$key]['#weight'] = $weight++;
  }
  return $build_list;
}