You are here

public function EntityViewBuilder::buildMultiple in Drupal 10

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

Builds multiple entities' views; augments entity defaults.

This function is assigned as a #pre_render callback in ::viewMultiple().

By delaying the building of an entity until the #pre_render processing in \Drupal::service('renderer')->render(), the processing cost of assembling an entity's renderable array is saved on cache-hit requests.

Parameters

array $build_list: A renderable array containing build information and context for an entity view.

Return value

array The updated renderable array.

See also

\Drupal\Core\Render\RendererInterface::render()

2 calls to EntityViewBuilder::buildMultiple()
BlockContentViewBuilder::viewMultiple in core/modules/block_content/src/BlockContentViewBuilder.php
Builds the render array for the provided entities.
EntityViewBuilder::build in core/lib/Drupal/Core/Entity/EntityViewBuilder.php
Builds an entity's view; augments entity defaults.

File

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

Class

EntityViewBuilder
Base class for entity view builders.

Namespace

Drupal\Core\Entity

Code

public function buildMultiple(array $build_list) {

  // Build the view modes and display objects.
  $view_modes = [];
  $entity_type_key = "#{$this->entityTypeId}";
  $view_hook = "{$this->entityTypeId}_view";

  // Find the keys for the ContentEntities in the build; Store entities for
  // rendering by view_mode.
  $children = Element::children($build_list);
  foreach ($children as $key) {
    if (isset($build_list[$key][$entity_type_key])) {
      $entity = $build_list[$key][$entity_type_key];
      if ($entity instanceof FieldableEntityInterface) {
        $view_modes[$build_list[$key]['#view_mode']][$key] = $entity;
      }
    }
  }

  // Build content for the displays represented by the entities.
  foreach ($view_modes as $view_mode => $view_mode_entities) {
    $displays = EntityViewDisplay::collectRenderDisplays($view_mode_entities, $view_mode);
    $this
      ->buildComponents($build_list, $view_mode_entities, $displays, $view_mode);
    foreach (array_keys($view_mode_entities) as $key) {

      // Allow for alterations while building, before rendering.
      $entity = $build_list[$key][$entity_type_key];
      $display = $displays[$entity
        ->bundle()];
      $this
        ->moduleHandler()
        ->invokeAll($view_hook, [
        &$build_list[$key],
        $entity,
        $display,
        $view_mode,
      ]);
      $this
        ->moduleHandler()
        ->invokeAll('entity_view', [
        &$build_list[$key],
        $entity,
        $display,
        $view_mode,
      ]);
      $this
        ->addContextualLinks($build_list[$key], $entity);
      $this
        ->alterBuild($build_list[$key], $entity, $display, $view_mode);

      // Assign the weights configured in the display.
      // @todo Once https://www.drupal.org/node/1875974 provides the missing
      //   API, only do it for 'extra fields', since other components have
      //   been taken care of in EntityViewDisplay::buildMultiple().
      foreach ($display
        ->getComponents() as $name => $options) {
        if (isset($build_list[$key][$name])) {
          $build_list[$key][$name]['#weight'] = $options['weight'];
        }
      }

      // Allow modules to modify the render array.
      $this
        ->moduleHandler()
        ->alter([
        $view_hook,
        'entity_view',
      ], $build_list[$key], $entity, $display);
    }
  }
  return $build_list;
}