View source
<?php
namespace Drupal\twig_tweak\View;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Block\TitleBlockPluginInterface;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Controller\TitleResolverInterface;
use Drupal\Core\Plugin\ContextAwarePluginInterface;
use Drupal\Core\Plugin\Context\ContextHandlerInterface;
use Drupal\Core\Plugin\Context\ContextRepositoryInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class BlockViewBuilder {
protected $pluginManagerBlock;
protected $contextRepository;
protected $contextHandler;
protected $account;
protected $requestStack;
protected $routeMatch;
protected $titleResolver;
public function __construct(CacheableDependencyInterface $plugin_manager_block, ContextRepositoryInterface $context_repository, ContextHandlerInterface $context_handler, AccountInterface $account, RequestStack $request_stack, RouteMatchInterface $route_match, TitleResolverInterface $title_resolver) {
$this->pluginManagerBlock = $plugin_manager_block;
$this->contextRepository = $context_repository;
$this->contextHandler = $context_handler;
$this->account = $account;
$this->requestStack = $request_stack;
$this->routeMatch = $route_match;
$this->titleResolver = $title_resolver;
}
public function build(string $id, array $configuration = [], bool $wrapper = TRUE) : array {
$configuration += [
'label_display' => BlockPluginInterface::BLOCK_LABEL_VISIBLE,
];
$block_plugin = $this->pluginManagerBlock
->createInstance($id, $configuration);
if ($block_plugin instanceof ContextAwarePluginInterface) {
$contexts = $this->contextRepository
->getRuntimeContexts($block_plugin
->getContextMapping());
$this->contextHandler
->applyContextMapping($block_plugin, $contexts);
}
$build = [];
$access = $block_plugin
->access($this->account, TRUE);
if ($access
->isAllowed()) {
if ($block_plugin instanceof TitleBlockPluginInterface) {
$request = $this->requestStack
->getCurrentRequest();
$title = $this->titleResolver
->getTitle($request, $this->routeMatch
->getRouteObject());
$block_plugin
->setTitle($title);
}
$build['content'] = $block_plugin
->build();
if ($block_plugin instanceof TitleBlockPluginInterface) {
$build['content']['#cache']['contexts'][] = 'url';
}
if ($wrapper && !Element::isEmpty($build['content'])) {
$build += [
'#theme' => 'block',
'#id' => $configuration['id'] ?? NULL,
'#attributes' => [],
'#contextual_links' => [],
'#configuration' => $block_plugin
->getConfiguration(),
'#plugin_id' => $block_plugin
->getPluginId(),
'#base_plugin_id' => $block_plugin
->getBaseId(),
'#derivative_plugin_id' => $block_plugin
->getDerivativeId(),
];
foreach ([
'#attributes',
'#contextual_links',
] as $property) {
if (isset($build['content'][$property])) {
$build[$property] = $build['content'][$property];
unset($build['content'][$property]);
}
}
}
}
CacheableMetadata::createFromRenderArray($build)
->merge(CacheableMetadata::createFromObject($access))
->merge(CacheableMetadata::createFromObject($block_plugin))
->applyTo($build);
if (!isset($build['#cache']['keys'])) {
$build['#cache']['keys'] = [
'twig_tweak_block',
$id,
'[configuration]=' . hash('sha256', serialize($configuration)),
'[wrapper]=' . (int) $wrapper,
];
}
return $build;
}
}