You are here

public function EntityViewDisplay::buildMultiple in Drupal 10

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

Builds a renderable array for the components of a set of entities.

This only includes the components handled by the Display object, but excludes 'extra fields', that are typically rendered through specific, ad-hoc code in EntityViewBuilderInterface::buildComponents() or in hook_entity_view() implementations.

hook_entity_display_build_alter() is invoked on each entity, allowing 3rd party code to alter the render array.

Parameters

\Drupal\Core\Entity\FieldableEntityInterface[] $entities: The entities being displayed.

Return value

array A renderable array for the entities, indexed by the same keys as the $entities array parameter.

Overrides EntityViewDisplayInterface::buildMultiple

See also

hook_entity_display_build_alter()

2 calls to EntityViewDisplay::buildMultiple()
EntityViewDisplay::build in core/lib/Drupal/Core/Entity/Entity/EntityViewDisplay.php
Builds a renderable array for the components of an entity.
LayoutBuilderEntityViewDisplay::buildMultiple in core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php
Builds a renderable array for the components of a set of entities.
1 method overrides EntityViewDisplay::buildMultiple()
LayoutBuilderEntityViewDisplay::buildMultiple in core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php
Builds a renderable array for the components of a set of entities.

File

core/lib/Drupal/Core/Entity/Entity/EntityViewDisplay.php, line 228

Class

EntityViewDisplay
Configuration entity that contains display options for all components of a rendered entity in a given view mode.

Namespace

Drupal\Core\Entity\Entity

Code

public function buildMultiple(array $entities) {
  $build_list = [];
  foreach ($entities as $key => $entity) {
    $build_list[$key] = [];
  }

  // Run field formatters.
  foreach ($this
    ->getComponents() as $name => $options) {
    if ($formatter = $this
      ->getRenderer($name)) {

      // Group items across all entities and pass them to the formatter's
      // prepareView() method.
      $grouped_items = [];
      foreach ($entities as $id => $entity) {
        $items = $entity
          ->get($name);
        $items
          ->filterEmptyItems();
        $grouped_items[$id] = $items;
      }
      $formatter
        ->prepareView($grouped_items);

      // Then let the formatter build the output for each entity.
      foreach ($entities as $id => $entity) {
        $items = $grouped_items[$id];

        /** @var \Drupal\Core\Access\AccessResultInterface $field_access */
        $field_access = $items
          ->access('view', NULL, TRUE);

        // The language of the field values to display is already determined
        // in the incoming $entity. The formatter should build its output of
        // those values using:
        // - the entity language if the entity is translatable,
        // - the current "content language" otherwise.
        if ($entity instanceof TranslatableDataInterface && $entity
          ->isTranslatable()) {
          $view_langcode = $entity
            ->language()
            ->getId();
        }
        else {
          $view_langcode = NULL;
        }
        $build_list[$id][$name] = $field_access
          ->isAllowed() ? $formatter
          ->view($items, $view_langcode) : [];

        // Apply the field access cacheability metadata to the render array.
        $this->renderer
          ->addCacheableDependency($build_list[$id][$name], $field_access);
      }
    }
  }
  foreach ($entities as $id => $entity) {

    // Assign the configured weights.
    foreach ($this
      ->getComponents() as $name => $options) {
      if (isset($build_list[$id][$name]) && !Element::isEmpty($build_list[$id][$name])) {
        $build_list[$id][$name]['#weight'] = $options['weight'];
      }
    }

    // Let other modules alter the renderable array.
    $context = [
      'entity' => $entity,
      'view_mode' => $this->originalMode,
      'display' => $this,
    ];
    \Drupal::moduleHandler()
      ->alter('entity_display_build', $build_list[$id], $context);
  }
  return $build_list;
}