You are here

public function Loader::loadRegion in Bamboo Twig 8

Builds the render array of a given region.

Parameters

string $region: The region to build.

string $theme: (optional) The name of the theme to load the region. If it is not provided then default theme will be used.

Return value

array A render array to display the region content.

File

bamboo_twig_loaders/src/TwigExtension/Loader.php, line 146

Class

Loader
Provides a 'Loader' Twig Extensions.

Namespace

Drupal\bamboo_twig_loaders\TwigExtension

Code

public function loadRegion($region, $theme = NULL) {
  $blocks = $this->entityTypeManager
    ->getStorage('block')
    ->loadByProperties([
    'region' => $region,
    'theme' => $theme ?: $this->configFactory
      ->get('system.theme')
      ->get('default'),
  ]);
  $view_builder = $this->entityTypeManager
    ->getViewBuilder('block');
  $build = [];

  /* @var $blocks \Drupal\block\BlockInterface[] */
  foreach ($blocks as $id => $block) {
    $block_plugin = $block
      ->getPlugin();
    if ($block_plugin instanceof TitleBlockPluginInterface) {
      $request = $this->requestStack
        ->getCurrentRequest();
      if ($route = $request->attributes
        ->get(RouteObjectInterface::ROUTE_OBJECT)) {
        $block_plugin
          ->setTitle($this->titleResolver
          ->getTitle($request, $route));
      }
    }
    $build[$id] = $view_builder
      ->view($block);
  }
  return $build;
}