View source
<?php
namespace Drupal\page_manager\Plugin\DisplayVariant;
use Drupal\Component\Render\HtmlEscapedText;
use Drupal\Component\Uuid\UuidInterface;
use Drupal\Component\Utility\Html;
use Drupal\Core\Block\BlockManager;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Condition\ConditionManager;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\Context\ContextHandlerInterface;
use Drupal\Core\Plugin\ContextAwarePluginInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Render\Markup;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Utility\Token;
use Drupal\ctools\Plugin\DisplayVariant\BlockDisplayVariant;
use Drupal\ctools\Plugin\PluginWizardInterface;
use Drupal\page_manager_ui\Form\VariantPluginContentForm;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PageBlockDisplayVariant extends BlockDisplayVariant implements PluginWizardInterface {
protected $moduleHandler;
public function __construct(array $configuration, $plugin_id, $plugin_definition, ContextHandlerInterface $context_handler, AccountInterface $account, UuidInterface $uuid_generator, Token $token, BlockManager $block_manager, ConditionManager $condition_manager, ModuleHandlerInterface $module_handler) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $context_handler, $account, $uuid_generator, $token, $block_manager, $condition_manager);
$this->moduleHandler = $module_handler;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('context.handler'), $container
->get('current_user'), $container
->get('uuid'), $container
->get('token'), $container
->get('plugin.manager.block'), $container
->get('plugin.manager.condition'), $container
->get('module_handler'));
}
public function build() {
$build['#cache']['keys'] = [
'page_manager_block_display',
$this
->id(),
];
$build['#pre_render'][] = [
$this,
'buildRegions',
];
return $build;
}
public function buildRegions(array $build) {
$cacheability = CacheableMetadata::createFromRenderArray($build)
->addCacheableDependency($this);
$contexts = $this
->getContexts();
foreach ($this
->getRegionAssignments() as $region => $blocks) {
if (!$blocks) {
continue;
}
$region_name = Html::getClass("block-region-{$region}");
$build[$region]['#prefix'] = '<div class="' . $region_name . '">';
$build[$region]['#suffix'] = '</div>';
$weight = 0;
foreach ($blocks as $block_id => $block) {
if ($block instanceof ContextAwarePluginInterface) {
$this
->contextHandler()
->applyContextMapping($block, $contexts);
}
$access = $block
->access($this->account, TRUE);
$cacheability
->addCacheableDependency($access);
if (!$access
->isAllowed()) {
continue;
}
$block_build = [
'#theme' => 'block',
'#attributes' => [],
'#weight' => $weight++,
'#configuration' => $block
->getConfiguration(),
'#plugin_id' => $block
->getPluginId(),
'#base_plugin_id' => $block
->getBaseId(),
'#derivative_plugin_id' => $block
->getDerivativeId(),
'#block_plugin' => $block,
'#pre_render' => [
[
$this,
'buildBlock',
],
],
'#cache' => [
'keys' => [
'page_manager_block_display',
$this
->id(),
'block',
$block_id,
],
'tags' => Cache::mergeTags($this
->getCacheTags(), $block
->getCacheTags()),
'contexts' => $block
->getCacheContexts(),
'max-age' => $block
->getCacheMaxAge(),
],
];
$cacheability
->addCacheableDependency($block);
$this->moduleHandler
->alter([
'block_view',
'block_view_' . $block
->getBaseId(),
], $block_build, $block);
$build[$region][$block_id] = $block_build;
}
}
$build['#title'] = $this
->renderPageTitle($this->configuration['page_title']);
$cacheability
->applyTo($build);
return $build;
}
public function buildBlock($build) {
$content = $build['#block_plugin']
->build();
unset($build['#block_plugin']);
if ($content !== NULL && !Element::isEmpty($content)) {
$build['content'] = $content;
}
else {
$build = [
'#markup' => '',
'#cache' => $build['#cache'],
];
}
if (!empty($content)) {
CacheableMetadata::createFromRenderArray($build)
->merge(CacheableMetadata::createFromRenderArray($content))
->applyTo($build);
}
return $build;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['page_title'] = [
'#type' => 'textfield',
'#title' => $this
->t('Page title'),
'#description' => $this
->t('Configure the page title that will be used for this display.'),
'#default_value' => $this->configuration['page_title'] ?: '',
];
$form['uuid'] = [
'#type' => 'value',
'#value' => $this->configuration['uuid'] ?: $this->uuidGenerator
->generate(),
];
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
if ($form_state
->hasValue('page_title')) {
$this->configuration['page_title'] = $form_state
->getValue('page_title');
}
if ($form_state
->hasValue('uuid')) {
$this->configuration['uuid'] = $form_state
->getValue('uuid');
}
}
public function getWizardOperations($cached_values) {
return [
'content' => [
'title' => $this
->t('Content'),
'form' => VariantPluginContentForm::class,
],
];
}
public function defaultConfiguration() {
return parent::defaultConfiguration() + [
'page_title' => '',
];
}
protected function renderPageTitle($page_title) {
$data = $this
->getContextAsTokenData();
$title = (string) $this->token
->replace(new HtmlEscapedText($page_title), $data);
return Markup::create($title);
}
protected function getContextAsTokenData() {
$data = [];
foreach ($this
->getContexts() as $context) {
if (strpos($context
->getContextDefinition()
->getDataType(), 'entity:') === 0) {
$token_type = substr($context
->getContextDefinition()
->getDataType(), 7);
if ($token_type == 'taxonomy_term') {
$token_type = 'term';
}
$data[$token_type] = $context
->getContextValue();
}
}
return $data;
}
public function getRegionNames() {
return [
'top' => 'Top',
'bottom' => 'Bottom',
];
}
public function __sleep() {
$vars = parent::__sleep();
if (($key = array_search('contexts', $vars)) !== FALSE) {
unset($vars[$key]);
}
return $vars;
}
}