BlockPageVariant.php in Drupal 10
File
core/modules/block/src/Plugin/DisplayVariant/BlockPageVariant.php
View source
<?php
namespace Drupal\block\Plugin\DisplayVariant;
use Drupal\block\BlockRepositoryInterface;
use Drupal\Core\Block\MainContentBlockPluginInterface;
use Drupal\Core\Block\TitleBlockPluginInterface;
use Drupal\Core\Block\MessagesBlockPluginInterface;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Display\PageVariantInterface;
use Drupal\Core\Entity\EntityViewBuilderInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Display\VariantBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class BlockPageVariant extends VariantBase implements PageVariantInterface, ContainerFactoryPluginInterface {
protected $blockRepository;
protected $blockViewBuilder;
protected $blockListCacheTags;
protected $mainContent = [];
protected $title = '';
public function __construct(array $configuration, $plugin_id, $plugin_definition, BlockRepositoryInterface $block_repository, EntityViewBuilderInterface $block_view_builder, array $block_list_cache_tags) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->blockRepository = $block_repository;
$this->blockViewBuilder = $block_view_builder;
$this->blockListCacheTags = $block_list_cache_tags;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('block.repository'), $container
->get('entity_type.manager')
->getViewBuilder('block'), $container
->get('entity_type.manager')
->getDefinition('block')
->getListCacheTags());
}
public function setMainContent(array $main_content) {
$this->mainContent = $main_content;
return $this;
}
public function setTitle($title) {
$this->title = $title;
return $this;
}
public function build() {
$main_content_block_displayed = FALSE;
$messages_block_displayed = FALSE;
$build = [
'#cache' => [
'tags' => $this->blockListCacheTags,
],
];
$cacheable_metadata_list = [];
foreach ($this->blockRepository
->getVisibleBlocksPerRegion($cacheable_metadata_list) as $region => $blocks) {
foreach ($blocks as $key => $block) {
$block_plugin = $block
->getPlugin();
if ($block_plugin instanceof MainContentBlockPluginInterface) {
$block_plugin
->setMainContent($this->mainContent);
$main_content_block_displayed = TRUE;
}
elseif ($block_plugin instanceof TitleBlockPluginInterface) {
$block_plugin
->setTitle($this->title);
}
elseif ($block_plugin instanceof MessagesBlockPluginInterface) {
$messages_block_displayed = TRUE;
}
$build[$region][$key] = $this->blockViewBuilder
->view($block);
if ($block_plugin instanceof MainContentBlockPluginInterface || $block_plugin instanceof TitleBlockPluginInterface) {
unset($build[$region][$key]['#cache']['keys']);
}
}
if (!empty($build[$region])) {
$build[$region]['#sorted'] = TRUE;
}
}
if (!$main_content_block_displayed) {
$build['content']['system_main'] = $this->mainContent;
}
if (!$messages_block_displayed) {
$build['content']['messages'] = [
'#weight' => -1000,
'#type' => 'status_messages',
'#include_fallback' => TRUE,
];
}
if (!$main_content_block_displayed || !$messages_block_displayed) {
unset($build['content']['#sorted']);
}
$merged_cacheable_metadata = CacheableMetadata::createFromRenderArray($build);
foreach ($cacheable_metadata_list as $cacheable_metadata) {
$merged_cacheable_metadata = $merged_cacheable_metadata
->merge($cacheable_metadata);
}
$merged_cacheable_metadata
->applyTo($build);
return $build;
}
}
Classes
Name |
Description |
BlockPageVariant |
Provides a page display variant that decorates the main content with blocks. |