function EntityViewDisplay::buildMultiple

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Entity/Entity/EntityViewDisplay.php \Drupal\Core\Entity\Entity\EntityViewDisplay::buildMultiple()
  2. 10 core/lib/Drupal/Core/Entity/Entity/EntityViewDisplay.php \Drupal\Core\Entity\Entity\EntityViewDisplay::buildMultiple()
  3. 11.x core/lib/Drupal/Core/Entity/Entity/EntityViewDisplay.php \Drupal\Core\Entity\Entity\EntityViewDisplay::buildMultiple()

Overrides EntityViewDisplayInterface::buildMultiple

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 230

Class

EntityViewDisplay
Configuration entity.

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

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