View source
<?php
namespace Drupal\blockgroup\Plugin\Block;
use Drupal\Component\Plugin\ContextAwarePluginInterface;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Block\MainContentBlockPluginInterface;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Controller\TitleResolverInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\Context\ContextHandlerInterface;
use Drupal\Core\Plugin\Context\ContextRepositoryInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Theme\ThemeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
class BlockGroup extends BlockBase implements ContainerFactoryPluginInterface, MainContentBlockPluginInterface {
protected $entityTypeManager;
protected $themeManager;
protected $renderer;
protected $contextRepository;
protected $contextHandler;
protected $titleResolver;
protected $routeMatch;
protected $request;
protected $mainContent;
public function setMainContent(array $main_content) {
$this->mainContent = $main_content;
}
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, ThemeManagerInterface $theme_manager, RendererInterface $renderer, ContextRepositoryInterface $context_repository, ContextHandlerInterface $context_handler, TitleResolverInterface $title_resolver, RouteMatchInterface $route_match, Request $request) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
$this->themeManager = $theme_manager;
$this->renderer = $renderer;
$this->contextRepository = $context_repository;
$this->contextHandler = $context_handler;
$this->titleResolver = $title_resolver;
$this->routeMatch = $route_match;
$this->request = $request;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('theme.manager'), $container
->get('renderer'), $container
->get('context.repository'), $container
->get('context.handler'), $container
->get('title_resolver'), $container
->get('current_route_match'), $container
->get('request_stack')
->getCurrentRequest());
}
public function build() {
$blockGroupStorage = $this->entityTypeManager
->getStorage('block_group_content');
$renderedBlocks = [];
$derivativeId = $this
->getDerivativeId();
$blockGroup = $blockGroupStorage
->load($derivativeId);
$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) {
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 {
$accessResult = $block
->access('view', NULL, TRUE);
if ($accessResult
->isAllowed()) {
$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;
}
}