public function BlockGroup::build in Block Group 8
Throws
\Drupal\Component\Plugin\Exception\PluginNotFoundException
\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
Overrides BlockPluginInterface::build
File
- src/
Plugin/ Block/ BlockGroup.php, line 164
Class
- BlockGroup
- Provides a 'BlockGroup' block.
Namespace
Drupal\blockgroup\Plugin\BlockCode
public function build() {
$blockGroupStorage = $this->entityTypeManager
->getStorage('block_group_content');
$renderedBlocks = [];
$derivativeId = $this
->getDerivativeId();
/** @var \Drupal\blockgroup\BlockGroupContentInterface $blockGroup */
$blockGroup = $blockGroupStorage
->load($derivativeId);
/** @var \Drupal\block\BlockInterface[] $blocks */
$blocks = $this->entityTypeManager
->getStorage('block')
->loadByProperties([
'theme' => $this->themeManager
->getActiveTheme()
->getName(),
'region' => $blockGroup
->id(),
]);
uasort($blocks, 'Drupal\\block\\Entity\\Block::sort');
foreach ($blocks as $block) {
// Special condition for Main block.
if ($block
->getPluginId() == 'system_main_block') {
$renderedBlocks[$block
->id()] = $this->mainContent;
}
elseif ($block
->getPluginId() == 'page_title_block') {
$title = $this->titleResolver
->getTitle($this->request, $this->routeMatch
->getRouteObject());
$renderedBlocks[$block
->id()] = [
'#type' => 'page_title',
'#title' => $title,
'#weight' => $block
->getWeight(),
];
}
else {
/** @var \Drupal\Core\Access\AccessResultInterface $accessResult */
$accessResult = $block
->access('view', NULL, TRUE);
if ($accessResult
->isAllowed()) {
// Inject runtime contexts.
// @see \Drupal\block\BlockViewBuilder::buildPreRenderableBlock().
$blockPlugin = $block
->getPlugin();
if ($blockPlugin instanceof ContextAwarePluginInterface) {
$contexts = $this->contextRepository
->getRuntimeContexts(array_values($blockPlugin
->getContextMapping()));
$this->contextHandler
->applyContextMapping($blockPlugin, $contexts);
}
$renderedBlocks[$block
->id()] = $this->entityTypeManager
->getViewBuilder('block')
->view($block);
}
$this->renderer
->addCacheableDependency($renderedBlocks, $accessResult);
}
$this->renderer
->addCacheableDependency($renderedBlocks, $block);
}
$this->renderer
->addCacheableDependency($renderedBlocks, $blockGroup);
$blockDefinition = $this->entityTypeManager
->getDefinition('block');
$listCacheability = new CacheableMetadata();
$listCacheability
->addCacheTags($blockDefinition
->getListCacheTags());
$listCacheability
->addCacheContexts($blockDefinition
->getListCacheContexts());
$this->renderer
->addCacheableDependency($renderedBlocks, $listCacheability);
return $renderedBlocks;
}