function BlockViewBuilder::viewMultiple

Same name in other branches
  1. 8.9.x core/modules/block/src/BlockViewBuilder.php \Drupal\block\BlockViewBuilder::viewMultiple()
  2. 10 core/modules/block/src/BlockViewBuilder.php \Drupal\block\BlockViewBuilder::viewMultiple()
  3. 11.x core/modules/block/src/BlockViewBuilder.php \Drupal\block\BlockViewBuilder::viewMultiple()

Overrides EntityViewBuilder::viewMultiple

1 call to BlockViewBuilder::viewMultiple()
BlockViewBuilder::view in core/modules/block/src/BlockViewBuilder.php
Builds the render array for the provided entity.

File

core/modules/block/src/BlockViewBuilder.php, line 39

Class

BlockViewBuilder
Provides a Block view builder.

Namespace

Drupal\block

Code

public function viewMultiple(array $entities = [], $view_mode = 'full', $langcode = NULL) {
    
    /** @var \Drupal\block\BlockInterface[] $entities */
    $build = [];
    foreach ($entities as $entity) {
        $entity_id = $entity->id();
        $plugin = $entity->getPlugin();
        $cache_tags = Cache::mergeTags($this->getCacheTags(), $entity->getCacheTags());
        $cache_tags = Cache::mergeTags($cache_tags, $plugin->getCacheTags());
        // Create the render array for the block as a whole.
        // @see template_preprocess_block().
        $build[$entity_id] = [
            '#cache' => [
                'keys' => [
                    'entity_view',
                    'block',
                    $entity->id(),
                ],
                'contexts' => Cache::mergeContexts($entity->getCacheContexts(), $plugin->getCacheContexts()),
                'tags' => $cache_tags,
                'max-age' => $plugin->getCacheMaxAge(),
            ],
            '#weight' => $entity->getWeight(),
        ];
        // Allow altering of cacheability metadata or setting #create_placeholder.
        $this->moduleHandler
            ->alter([
            'block_build',
            "block_build_" . $plugin->getBaseId(),
        ], $build[$entity_id], $plugin);
        if ($plugin instanceof MainContentBlockPluginInterface || $plugin instanceof TitleBlockPluginInterface) {
            // Immediately build a #pre_render-able block, since this block cannot
            // be built lazily.
            $build[$entity_id] += static::buildPreRenderableBlock($entity, $this->moduleHandler());
        }
        else {
            // Assign a #lazy_builder callback, which will generate a #pre_render-
            // able block lazily (when necessary).
            $build[$entity_id] += [
                '#lazy_builder' => [
                    static::class . '::lazyBuilder',
                    [
                        $entity_id,
                        $view_mode,
                        $langcode,
                    ],
                ],
            ];
        }
    }
    return $build;
}

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