public function RegionViewBuilder::build in Twig Tweak 3.x
Same name and namespace in other branches
- 3.1.x src/View/RegionViewBuilder.php \Drupal\twig_tweak\View\RegionViewBuilder::build()
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
- src/
View/ RegionViewBuilder.php, line 73
Class
- RegionViewBuilder
- Region view builder.
Namespace
Drupal\twig_tweak\ViewCode
public function build(string $region, string $theme = NULL) : array {
$blocks = $this->entityTypeManager
->getStorage('block')
->loadByProperties([
'region' => $region,
'theme' => $theme ?: $this->configFactory
->get('system.theme')
->get('default'),
]);
$view_builder = $this->entityTypeManager
->getViewBuilder('block');
$build = [];
$entity_type = $this->entityTypeManager
->getDefinition('block');
$cache_metadata = (new CacheableMetadata())
->addCacheTags($entity_type
->getListCacheTags())
->addCacheContexts($entity_type
->getListCacheContexts());
/** @var \Drupal\block\BlockInterface[] $blocks */
foreach ($blocks as $id => $block) {
$access = $block
->access('view', NULL, TRUE);
$cache_metadata = $cache_metadata
->merge(CacheableMetadata::createFromObject($access));
if ($access
->isAllowed()) {
$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);
}
}
if ($build) {
$build['#region'] = $region;
$build['#theme_wrappers'] = [
'region',
];
}
$cache_metadata
->applyTo($build);
return $build;
}