You are here

protected function StandardDisplayBuilder::buildRegions in Panels 8.3

Same name and namespace in other branches
  1. 8.4 src/Plugin/DisplayBuilder/StandardDisplayBuilder.php \Drupal\panels\Plugin\DisplayBuilder\StandardDisplayBuilder::buildRegions()

Build render arrays for each of the regions.

Parameters

array $regions: The render array representing regions.

array $contexts: The array of context objects.

Return value

array An associative array, keyed by region ID, containing the render arrays representing the content of each region.

1 call to StandardDisplayBuilder::buildRegions()
StandardDisplayBuilder::build in src/Plugin/DisplayBuilder/StandardDisplayBuilder.php
Renders a Panels display.

File

src/Plugin/DisplayBuilder/StandardDisplayBuilder.php, line 95

Class

StandardDisplayBuilder
The standard display builder for viewing a PanelsDisplayVariant.

Namespace

Drupal\panels\Plugin\DisplayBuilder

Code

protected function buildRegions(array $regions, array $contexts) {
  $build = [];
  foreach ($regions 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);
      }
      if ($block
        ->access($this->account)) {
        $block_render_array = [
          '#theme' => 'block',
          '#attributes' => [],
          '#contextual_links' => [],
          '#weight' => $weight++,
          '#configuration' => $block
            ->getConfiguration(),
          '#plugin_id' => $block
            ->getPluginId(),
          '#base_plugin_id' => $block
            ->getBaseId(),
          '#derivative_plugin_id' => $block
            ->getDerivativeId(),
        ];

        // Build the block and bubble its attributes up if possible. This
        // allows modules like Quickedit to function.
        // See \Drupal\block\BlockViewBuilder::preRender() for reference.
        $content = $block
          ->build();
        if ($content !== NULL && !Element::isEmpty($content)) {
          foreach ([
            '#attributes',
            '#contextual_links',
          ] as $property) {
            if (isset($content[$property])) {
              $block_render_array[$property] += $content[$property];
              unset($content[$property]);
            }
          }
        }

        // If the block is empty, instead of trying to render the block
        // correctly return just #cache, so that the render system knows the
        // reasons (cache contexts & tags) why this block is empty.
        if (Element::isEmpty($content)) {
          $block_render_array = [];
          $cacheable_metadata = CacheableMetadata::createFromObject($block_render_array);
          $cacheable_metadata
            ->applyTo($block_render_array);
          if (isset($content['#cache'])) {
            $block_render_array['#cache'] += $content['#cache'];
          }
        }
        $block_render_array['content'] = $content;
        $build[$region][$block_id] = $block_render_array;
      }
    }
  }
  return $build;
}