You are here

public function Blocks::preRenderBlock in Context 8

Same name and namespace in other branches
  1. 8.4 src/Plugin/ContextReaction/Blocks.php \Drupal\context\Plugin\ContextReaction\Blocks::preRenderBlock()
  2. 8.0 src/Plugin/ContextReaction/Blocks.php \Drupal\context\Plugin\ContextReaction\Blocks::preRenderBlock()

Renders the content using the provided block plugin.

Parameters

array $build:

Return value

array

File

src/Plugin/ContextReaction/Blocks.php, line 265

Class

Blocks
Provides a content reaction that will let you place blocks in the current themes regions.

Namespace

Drupal\context\Plugin\ContextReaction

Code

public function preRenderBlock($build) {
  $content = $build['#block_plugin']
    ->build();
  unset($build['#block_plugin']);

  // Abort rendering: render as the empty string and ensure this block is
  // render cached, so we can avoid the work of having to repeatedly
  // determine whether the block is empty. E.g. modifying or adding entities
  // could cause the block to no longer be empty.
  if (is_null($content) || Element::isEmpty($content)) {
    $build = [
      '#markup' => '',
      '#cache' => $build['#cache'],
    ];

    // If $content is not empty, then it contains cacheability metadata, and
    // we must merge it with the existing cacheability metadata. This allows
    // blocks to be empty, yet still bubble cacheability metadata, to indicate
    // why they are empty.
    if (!empty($content)) {
      CacheableMetadata::createFromRenderArray($build)
        ->merge(CacheableMetadata::createFromRenderArray($content))
        ->applyTo($build);
    }
  }
  else {
    $build['content'] = $content;
  }
  return $build;
}