ContextBlockPageVariant.php in Context 8
File
src/Plugin/DisplayVariant/ContextBlockPageVariant.php
View source
<?php
namespace Drupal\context\Plugin\DisplayVariant;
use Drupal\Component\Utility\NestedArray;
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 Symfony\Component\DependencyInjection\ContainerInterface;
class ContextBlockPageVariant extends VariantBase implements PageVariantInterface, ContainerFactoryPluginInterface {
protected $contextManager;
protected $mainContent = [];
protected $title = '';
public function __construct(array $configuration, $plugin_id, $plugin_definition, ContextManager $contextManager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->contextManager = $contextManager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('context.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 = NestedArray::mergeDeep($this
->getBuildFromBlockLayout(), $build);
return $build;
}
}
return $build;
}
private function getBuildFromBlockLayout() {
$plugin_manager = \Drupal::service('plugin.manager.display_variant');
$display_variant = $plugin_manager
->createInstance('block_page', $plugin_manager
->getDefinition('block_page'));
$display_variant
->setTitle($this->title);
return $display_variant
->build();
}
}