View source
<?php
namespace Drupal\panels\Plugin\DisplayBuilder;
use Drupal\Component\Utility\Html;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\Context\ContextHandlerInterface;
use Drupal\Core\Plugin\ContextAwarePluginInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\ctools\Plugin\PluginWizardInterface;
use Drupal\panels\Form\LayoutChangeRegions;
use Drupal\panels\Form\LayoutChangeSettings;
use Drupal\panels\Form\LayoutPluginSelector;
use Drupal\panels\Form\PanelsContentForm;
use Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant;
use Symfony\Component\DependencyInjection\ContainerInterface;
class StandardDisplayBuilder extends DisplayBuilderBase implements PluginWizardInterface, ContainerFactoryPluginInterface {
protected $contextHandler;
protected $account;
public function __construct(array $configuration, $plugin_id, $plugin_definition, ContextHandlerInterface $context_handler, AccountInterface $account) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->contextHandler = $context_handler;
$this->account = $account;
}
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'));
}
protected function buildRegions(array $regions, array $contexts) {
$build = [];
foreach ($regions 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);
}
if ($block
->access($this->account)) {
$block_render_array = [
'#theme' => 'block',
'#attributes' => [],
'#contextual_links' => [],
'#weight' => $weight++,
'#configuration' => $block
->getConfiguration(),
'#plugin_id' => $block
->getPluginId(),
'#base_plugin_id' => $block
->getBaseId(),
'#derivative_plugin_id' => $block
->getDerivativeId(),
];
$content = $block
->build();
if ($content !== NULL && !Element::isEmpty($content)) {
foreach ([
'#attributes',
'#contextual_links',
] as $property) {
if (isset($content[$property])) {
$block_render_array[$property] += $content[$property];
unset($content[$property]);
}
}
}
if (Element::isEmpty($content)) {
$block_render_array = [];
$cacheable_metadata = CacheableMetadata::createFromObject($block_render_array);
$cacheable_metadata
->applyTo($block_render_array);
if (isset($content['#cache'])) {
$block_render_array['#cache'] += $content['#cache'];
}
}
$block_render_array['content'] = $content;
$build[$region][$block_id] = $block_render_array;
}
}
}
return $build;
}
public function build(PanelsDisplayVariant $panels_display) {
$regions = $panels_display
->getRegionAssignments();
$contexts = $panels_display
->getContexts();
$layout = $panels_display
->getLayout();
$regions = $this
->buildRegions($regions, $contexts);
if ($layout) {
$regions = $layout
->build($regions);
}
return $regions;
}
public function getWizardOperations($cached_values) {
$operations = [];
$operations['content'] = [
'title' => $this
->t('Content'),
'form' => PanelsContentForm::class,
];
return $operations;
}
}