function 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 41

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' => [
        'contexts' => Cache::mergeContexts($entity->getCacheContexts(), $plugin->getCacheContexts()),
        'tags' => $cache_tags,
        'max-age' => $plugin->getCacheMaxAge(),
      ],
      '#weight' => $entity->getWeight(),
    ];
    // For block plugins implementing CacheOptionalInterface, the expectation
    // is that the cost of rendering them is less than retrieving them from
    // cache. Only add cache keys to the block render array if the block
    // plugin does not implement CacheOptionalInterface.
    // If any CacheOptionalInterface block is set to render as a placeholder
    // (createPlaceholder() returns TRUE), the cached response in the internal
    // page cache or external caches and proxies will include the block
    // markup, but the block is not cached anywhere else. If a
    // CacheOptionalInterface block is not set to render as a placeholder,
    // then its rendered markup is cached within the rendered page in the
    // dynamic page cache.
    if (!$plugin instanceof CacheOptionalInterface) {
      $build[$entity_id]['#cache']['keys'] = [
        'entity_view',
        'block',
        $entity->id(),
      ];
    }
    else {
      // When a block implements CacheOptionalInterface, it will be excluded
      // from the dynamic render cache. Since it is also cheap to render,
      // prevent it being placeholdered by BigPipe. This avoids loading
      // BigPipe's JavaScript if this block is the only placeholder on the
      // page, which is likely to be the case on dynamic page cache hits.
      $build[$entity_id]['#placeholder_strategy_denylist'] = [
        BigPipeStrategy::class => TRUE,
      ];
    }
    // 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.
      $cacheableMetadata = CacheableMetadata::createFromRenderArray($build[$entity_id]);
      $preRenderableBlock = static::buildPreRenderableBlock($entity, $this->moduleHandler());
      $cacheableMetadata->addCacheableDependency(CacheableMetadata::createFromRenderArray($preRenderableBlock));
      $build[$entity_id] += $preRenderableBlock;
      $cacheableMetadata->applyTo($build[$entity_id]);
    }
    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,
          ],
        ],
      ];
      // Only add create_placeholder if it's explicitly set to TRUE, so it can
      // be set to TRUE by automatic placeholdering conditions if it's absent.
      if ($plugin->createPlaceholder()) {
        $build[$entity_id] += [
          '#create_placeholder' => TRUE,
        ];
      }
    }
  }
  return $build;
}

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