8.5.x BlockViewBuilder.php BlockViewBuilder::buildPreRenderableBlock($entity, ModuleHandlerInterface $module_handler)
8.0.x BlockViewBuilder.php BlockViewBuilder::buildPreRenderableBlock($entity, ModuleHandlerInterface $module_handler)
8.1.x BlockViewBuilder.php BlockViewBuilder::buildPreRenderableBlock($entity, ModuleHandlerInterface $module_handler)
8.2.x BlockViewBuilder.php BlockViewBuilder::buildPreRenderableBlock($entity, ModuleHandlerInterface $module_handler)
8.3.x BlockViewBuilder.php BlockViewBuilder::buildPreRenderableBlock($entity, ModuleHandlerInterface $module_handler)
8.4.x BlockViewBuilder.php BlockViewBuilder::buildPreRenderableBlock($entity, ModuleHandlerInterface $module_handler)
8.6.x BlockViewBuilder.php BlockViewBuilder::buildPreRenderableBlock($entity, ModuleHandlerInterface $module_handler)

Builds a #pre_render-able block render array.

Parameters

\Drupal\block\BlockInterface $entity: A block config entity.

\Drupal\Core\Extension\ModuleHandlerInterface $module_handler: The module handler service.

Return value

array A render array with a #pre_render callback to render the block.

2 calls to BlockViewBuilder::buildPreRenderableBlock()
BlockViewBuilder::lazyBuilder in core/modules/block/src/BlockViewBuilder.php
#lazy_builder callback; builds a #pre_render-able block.
BlockViewBuilder::viewMultiple in core/modules/block/src/BlockViewBuilder.php
Builds the render array for the provided entities.

File

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

Class

BlockViewBuilder
Provides a Block view builder.

Namespace

Drupal\block

Code

protected static function buildPreRenderableBlock($entity, ModuleHandlerInterface $module_handler) {
  $plugin = $entity
    ->getPlugin();
  $plugin_id = $plugin
    ->getPluginId();
  $base_id = $plugin
    ->getBaseId();
  $derivative_id = $plugin
    ->getDerivativeId();
  $configuration = $plugin
    ->getConfiguration();

  // Inject runtime contexts.
  if ($plugin instanceof ContextAwarePluginInterface) {
    $contexts = \Drupal::service('context.repository')
      ->getRuntimeContexts($plugin
      ->getContextMapping());
    \Drupal::service('context.handler')
      ->applyContextMapping($plugin, $contexts);
  }

  // Create the render array for the block as a whole.
  // @see template_preprocess_block().
  $build = [
    '#theme' => 'block',
    '#attributes' => [],
    // All blocks get a "Configure block" contextual link.
    '#contextual_links' => [
      'block' => [
        'route_parameters' => [
          'block' => $entity
            ->id(),
        ],
      ],
    ],
    '#weight' => $entity
      ->getWeight(),
    '#configuration' => $configuration,
    '#plugin_id' => $plugin_id,
    '#base_plugin_id' => $base_id,
    '#derivative_plugin_id' => $derivative_id,
    '#id' => $entity
      ->id(),
    '#pre_render' => [
      static::class . '::preRender',
    ],
    // Add the entity so that it can be used in the #pre_render method.
    '#block' => $entity,
  ];

  // If an alter hook wants to modify the block contents, it can append
  // another #pre_render hook.
  $module_handler
    ->alter([
    'block_view',
    "block_view_{$base_id}",
  ], $build, $plugin);
  return $build;
}