You are here

public function PageBlockDisplayVariant::buildBlock in Page Manager 8.4

Same name and namespace in other branches
  1. 8 src/Plugin/DisplayVariant/PageBlockDisplayVariant.php \Drupal\page_manager\Plugin\DisplayVariant\PageBlockDisplayVariant::buildBlock()

#pre_render callback for building a block.

Renders the content using the provided block plugin, if there is no content, aborts rendering, and makes sure the block won't be rendered.

File

src/Plugin/DisplayVariant/PageBlockDisplayVariant.php, line 186

Class

PageBlockDisplayVariant
Provides a variant plugin that simply contains blocks.

Namespace

Drupal\page_manager\Plugin\DisplayVariant

Code

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

  // Remove the block plugin from the render array.
  unset($build['#block_plugin']);
  if ($content !== NULL && !Element::isEmpty($content)) {
    $build['content'] = $content;

    // Add contextual links but prevent duplicating the Views block displays
    // contextual links.
    $add_contextual_links = !empty($content['#contextual_links']) && empty($content['#views_contextual_links']);
    $build['#contextual_links'] = $add_contextual_links ? $content['#contextual_links'] : [];
  }
  else {

    // 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.
    $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);
  }
  return $build;
}