You are here

public function PageBlockDisplayVariant::buildRegions in Page Manager 8

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

#pre_render callback for building the regions.

File

src/Plugin/DisplayVariant/PageBlockDisplayVariant.php, line 111
Contains \Drupal\page_manager\Plugin\DisplayVariant\PageBlockDisplayVariant.

Class

PageBlockDisplayVariant
Provides a variant plugin that simply contains blocks.

Namespace

Drupal\page_manager\Plugin\DisplayVariant

Code

public function buildRegions(array $build) {
  $cacheability = CacheableMetadata::createFromRenderArray($build)
    ->addCacheableDependency($this);
  $contexts = $this
    ->getContexts();
  foreach ($this
    ->getRegionAssignments() as $region => $blocks) {
    if (!$blocks) {
      continue;
    }
    $region_name = Html::getClass("block-region-{$region}");
    $build[$region]['#prefix'] = '<div class="' . $region_name . '">';
    $build[$region]['#suffix'] = '</div>';

    /** @var \Drupal\Core\Block\BlockPluginInterface[] $blocks */
    $weight = 0;
    foreach ($blocks as $block_id => $block) {
      if ($block instanceof ContextAwarePluginInterface) {
        $this
          ->contextHandler()
          ->applyContextMapping($block, $contexts);
      }
      $access = $block
        ->access($this->account, TRUE);
      $cacheability
        ->addCacheableDependency($access);
      if (!$access
        ->isAllowed()) {
        continue;
      }
      $block_build = [
        '#theme' => 'block',
        '#attributes' => [],
        '#weight' => $weight++,
        '#configuration' => $block
          ->getConfiguration(),
        '#plugin_id' => $block
          ->getPluginId(),
        '#base_plugin_id' => $block
          ->getBaseId(),
        '#derivative_plugin_id' => $block
          ->getDerivativeId(),
        '#block_plugin' => $block,
        '#pre_render' => [
          [
            $this,
            'buildBlock',
          ],
        ],
        '#cache' => [
          'keys' => [
            'page_manager_block_display',
            $this
              ->id(),
            'block',
            $block_id,
          ],
          // Each block needs cache tags of the page and the block plugin, as
          // only the page is a config entity that will trigger cache tag
          // invalidations in case of block configuration changes.
          'tags' => Cache::mergeTags($this
            ->getCacheTags(), $block
            ->getCacheTags()),
          'contexts' => $block
            ->getCacheContexts(),
          'max-age' => $block
            ->getCacheMaxAge(),
        ],
      ];

      // Merge the cacheability metadata of blocks into the page. This helps
      // to avoid cache redirects if the blocks have more cache contexts than
      // the page, which the page must respect as well.
      $cacheability
        ->addCacheableDependency($block);

      // If an alter hook wants to modify the block contents, it can append
      // another #pre_render hook.
      $this->moduleHandler
        ->alter([
        'block_view',
        'block_view_' . $block
          ->getBaseId(),
      ], $block_build, $block);
      $build[$region][$block_id] = $block_build;
    }
  }
  $build['#title'] = $this
    ->renderPageTitle($this->configuration['page_title']);
  $cacheability
    ->applyTo($build);
  return $build;
}