function EntityViewBuilder::buildComponents

Same name and namespace in other branches
  1. 11.x 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()
  3. 8.9.x core/lib/Drupal/Core/Entity/EntityViewBuilder.php \Drupal\Core\Entity\EntityViewBuilder::buildComponents()
1 method overrides EntityViewBuilder::buildComponents()
WorkspaceViewBuilder::buildComponents in core/modules/workspaces/src/WorkspaceViewBuilder.php
Builds the component fields and properties of a set of entities.

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];
    }
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.