ContextBlockPageVariant.php in Context 8.4
File
src/Plugin/DisplayVariant/ContextBlockPageVariant.php
View source
<?php
namespace Drupal\context\Plugin\DisplayVariant;
use Drupal\context\ContextManager;
use Drupal\Core\Display\VariantBase;
use Drupal\Core\Display\PageVariantInterface;
use Drupal\Core\Display\VariantManager;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Render\Element;
use Drupal\Core\Theme\ThemeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ContextBlockPageVariant extends VariantBase implements PageVariantInterface, ContainerFactoryPluginInterface {
protected $contextManager;
protected $mainContent = [];
protected $title = '';
protected $displayVariant;
protected $themeManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, ContextManager $contextManager, VariantManager $displayVariant, ThemeManagerInterface $themeManager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->contextManager = $contextManager;
$this->displayVariant = $displayVariant;
$this->themeManager = $themeManager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('context.manager'), $container
->get('plugin.manager.display_variant'), $container
->get('theme.manager'));
}
public function setMainContent(array $main_content) {
$this->mainContent = $main_content;
return $this;
}
public function setTitle($title) {
$this->title = $title;
return $this;
}
public function build() {
$build = [
'#cache' => [
'tags' => [
'context_block_page',
$this
->getPluginId(),
],
],
];
$build['content']['system_main'] = $this->mainContent;
foreach ($this->contextManager
->getActiveReactions('blocks') as $reaction) {
$build = $reaction
->execute($build, $this->title, $this->mainContent);
}
foreach ($this->contextManager
->getActiveReactions('blocks') as $reaction) {
if ($reaction
->includeDefaultBlocks()) {
$build_block_layout = $this
->getBuildFromBlockLayout();
$regions = $this->themeManager
->getActiveTheme()
->getRegions();
foreach ($regions as $region_key) {
if (empty($build[$region_key])) {
$build[$region_key] = [];
}
if (empty($build_block_layout[$region_key])) {
$build_block_layout[$region_key] = [];
}
$build[$region_key] += $build_block_layout[$region_key];
}
BubbleableMetadata::createFromRenderArray($build)
->merge(BubbleableMetadata::createFromRenderArray($build_block_layout))
->applyTo($build);
foreach (Element::children($build) as $region_key) {
foreach ($build[$region_key] as $blockId) {
if (isset($blockId['#plugin_id']) && $blockId['#plugin_id'] == 'system_main_block') {
unset($build['content']['system_main']);
break;
}
}
}
foreach ($build as $block) {
if (array_key_exists('system_messages_block', $block)) {
unset($build['content']['messages']);
break;
}
}
return $build;
}
}
return $build;
}
private function getBuildFromBlockLayout() {
$display_variant = $this->displayVariant
->createInstance('block_page', $this->displayVariant
->getDefinition('block_page'));
$display_variant
->setTitle($this->title);
$display_variant
->setMainContent($this->mainContent);
return $display_variant
->build();
}
}