You are here

public function BlockViewBuilder::viewMultiple in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/block/src/BlockViewBuilder.php \Drupal\block\BlockViewBuilder::viewMultiple()

Builds the render array for the provided entities.

Parameters

array $entities: An array of entities implementing EntityInterface to view.

string $view_mode: (optional) The view mode that should be used to render the entity.

string $langcode: (optional) For which language the entity should be rendered, defaults to the current content language.

Return value

A render array for the entities, indexed by the same keys as the entities array passed in $entities.

Throws

\InvalidArgumentException Can be thrown when the set of parameters is inconsistent, like when trying to view Comments and passing a Node which is not the one the comments belongs to, or not passing one, and having the comments node not be available for loading.

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 82
Contains \Drupal\block\BlockViewBuilder.

Class

BlockViewBuilder
Provides a Block view builder.

Namespace

Drupal\block

Code

public function viewMultiple(array $entities = array(), $view_mode = 'full', $langcode = NULL) {

  /** @var \Drupal\block\BlockInterface[] $entities */
  $build = array();
  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] = array(
      '#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;
}