You are here

public function EntityViewBuilder::buildComponents in Drupal 9

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

Builds the component fields and properties of a set of entities.

Parameters

&$build: The renderable array representing the entity content.

\Drupal\Core\Entity\EntityInterface[] $entities: The entities whose content is being built.

\Drupal\Core\Entity\Display\EntityViewDisplayInterface[] $displays: The array of entity view displays holding the display options configured for the entity components, keyed by bundle name.

string $view_mode: The view mode in which the entity is being viewed.

Overrides EntityViewBuilderInterface::buildComponents

7 calls to EntityViewBuilder::buildComponents()
CommentViewBuilder::buildComponents in core/modules/comment/src/CommentViewBuilder.php
In addition to modifying the content key on entities, this implementation will also set the comment entity key which all comments carry.
EntityTestViewBuilder::buildComponents in core/modules/system/tests/modules/entity_test/src/EntityTestViewBuilder.php
Builds the component fields and properties of a set of entities.
EntityViewBuilder::buildMultiple in core/lib/Drupal/Core/Entity/EntityViewBuilder.php
Builds multiple entities' views; augments entity defaults.
FeedViewBuilder::buildComponents in core/modules/aggregator/src/FeedViewBuilder.php
Builds the component fields and properties of a set of entities.
ItemViewBuilder::buildComponents in core/modules/aggregator/src/ItemViewBuilder.php
Builds the component fields and properties of a set of entities.

... See full list

7 methods override EntityViewBuilder::buildComponents()
BlockViewBuilder::buildComponents in core/modules/block/src/BlockViewBuilder.php
Builds the component fields and properties of a set of entities.
CommentViewBuilder::buildComponents in core/modules/comment/src/CommentViewBuilder.php
In addition to modifying the content key on entities, this implementation will also set the comment entity key which all comments carry.
EntityTestViewBuilder::buildComponents in core/modules/system/tests/modules/entity_test/src/EntityTestViewBuilder.php
Builds the component fields and properties of a set of entities.
FeedViewBuilder::buildComponents in core/modules/aggregator/src/FeedViewBuilder.php
Builds the component fields and properties of a set of entities.
ItemViewBuilder::buildComponents in core/modules/aggregator/src/ItemViewBuilder.php
Builds the component fields and properties of a set of entities.

... See full list

File

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

Class

EntityViewBuilder
Base class for entity view builders.

Namespace

Drupal\Core\Entity

Code

public function buildComponents(array &$build, array $entities, array $displays, $view_mode) {
  $entities_by_bundle = [];
  foreach ($entities as $id => $entity) {

    // Initialize the field item attributes for the fields being displayed.
    // The entity can include fields that are not displayed, and the display
    // can include components that are not fields, so we want to act on the
    // intersection. However, the entity can have many more fields than are
    // displayed, so we avoid the cost of calling $entity->getProperties()
    // by iterating the intersection as follows.
    foreach ($displays[$entity
      ->bundle()]
      ->getComponents() as $name => $options) {
      if ($entity
        ->hasField($name)) {
        foreach ($entity
          ->get($name) as $item) {
          $item->_attributes = [];
        }
      }
    }

    // Group the entities by bundle.
    $entities_by_bundle[$entity
      ->bundle()][$id] = $entity;
  }

  // Invoke hook_entity_prepare_view().
  $this
    ->moduleHandler()
    ->invokeAll('entity_prepare_view', [
    $this->entityTypeId,
    $entities,
    $displays,
    $view_mode,
  ]);

  // Let the displays build their render arrays.
  foreach ($entities_by_bundle as $bundle => $bundle_entities) {
    $display_build = $displays[$bundle]
      ->buildMultiple($bundle_entities);
    foreach ($bundle_entities as $id => $entity) {
      $build[$id] += $display_build[$id];
    }
  }
}