View source
<?php
namespace Drupal\panels\Plugin\DisplayVariant;
use Drupal\Component\Render\HtmlEscapedText;
use Drupal\Component\Uuid\UuidInterface;
use Drupal\Core\Block\BlockManager;
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\PluginFormInterface;
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\layout_plugin\Plugin\Layout\LayoutInterface;
use Drupal\layout_plugin\Plugin\Layout\LayoutPluginManagerInterface;
use Drupal\panels\Form\LayoutChangeRegions;
use Drupal\panels\Form\LayoutChangeSettings;
use Drupal\panels\Form\LayoutPluginSelector;
use Drupal\panels\Plugin\DisplayBuilder\DisplayBuilderInterface;
use Drupal\panels\Plugin\DisplayBuilder\DisplayBuilderManagerInterface;
use Drupal\panels\Plugin\PanelsPattern\PanelsPatternInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PanelsDisplayVariant extends BlockDisplayVariant implements PluginWizardInterface {
protected $moduleHandler;
protected $builderManager;
protected $builder;
protected $layoutManager;
protected $layout;
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, DisplayBuilderManagerInterface $builder_manager, LayoutPluginManagerInterface $layout_manager) {
$this->moduleHandler = $module_handler;
$this->builderManager = $builder_manager;
$this->layoutManager = $layout_manager;
parent::__construct($configuration, $plugin_id, $plugin_definition, $context_handler, $account, $uuid_generator, $token, $block_manager, $condition_manager);
}
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'), $container
->get('plugin.manager.panels.display_builder'), $container
->get('plugin.manager.layout_plugin'));
}
public function getBuilder() {
if (!isset($this->builder)) {
if (empty($this->configuration['builder'])) {
$this->builder = $this->builderManager
->createInstance('standard', []);
}
else {
$this->builder = $this->builderManager
->createInstance($this->configuration['builder'], []);
}
}
return $this->builder;
}
public function setBuilder($builder) {
if ($builder instanceof DisplayBuilderInterface) {
$this->builder = $builder;
$this->configuration['builder'] = $builder
->getPluginId();
}
elseif (is_string($builder)) {
$this->builder = NULL;
$this->configuration['builder'] = $builder;
}
else {
throw new \Exception("Builder must be a string or DisplayBuilderInterface object");
}
return $this;
}
public function getLayout() {
if (!isset($this->layout)) {
$this->layout = $this->layoutManager
->createInstance($this->configuration['layout'], $this->configuration['layout_settings']);
}
return $this->layout;
}
public function setLayout($layout, array $layout_settings = []) {
if ($layout instanceof LayoutInterface) {
$this->layout = $layout;
$this->configuration['layout'] = $layout
->getPluginId();
$this->configuration['layout_settings'] = $layout_settings;
}
elseif (is_string($layout)) {
$this->layout = NULL;
$this->configuration['layout'] = $layout;
$this->configuration['layout_settings'] = $layout_settings;
}
else {
throw new \Exception("Layout must be a string or LayoutInterface object");
}
return $this;
}
public function getPattern() {
if (!isset($this->pattern)) {
if (empty($this->configuration['pattern'])) {
$this->pattern = \Drupal::service('plugin.manager.panels.pattern')
->createInstance('default');
}
else {
$this->pattern = \Drupal::service('plugin.manager.panels.pattern')
->createInstance($this->configuration['pattern']);
}
}
return $this->pattern;
}
public function setPattern($pattern) {
if ($pattern instanceof PanelsPatternInterface) {
$this->pattern = $pattern;
$this->configuration['pattern'] = $pattern
->getPluginId();
}
elseif (is_string($pattern)) {
$this->pattern = NULL;
$this->configuration['pattern'] = $pattern;
}
else {
throw new \Exception("Pattern must be a string or PanelsPatternInterface object");
}
return $this;
}
public function setStorage($type, $id) {
$this->configuration['storage_type'] = $type;
$this->configuration['storage_id'] = $id;
return $this;
}
public function getStorageType() {
return $this->configuration['storage_type'] ?: NULL;
}
public function getStorageId() {
return $this->configuration['storage_id'] ?: NULL;
}
public function getRegionNames() {
return $this
->getLayout()
->getPluginDefinition()['region_names'];
}
public function getPageTitle() {
return $this->configuration['page_title'];
}
public function setPageTitle($title) {
$this->configuration['page_title'] = $title;
return $this;
}
public function build() {
$build = $this
->getBuilder()
->build($this);
$build['#title'] = $this
->renderPageTitle($this->configuration['page_title']);
$this->moduleHandler
->alter('panels_build', $build, $this);
return $build;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$plugins = $this->builderManager
->getDefinitions();
$options = array();
foreach ($plugins as $id => $plugin) {
$options[$id] = $plugin['label'];
}
if (!$this
->getStorageType()) {
unset($options['ipe']);
}
$form['builder'] = [
'#title' => $this
->t('Builder'),
'#type' => 'select',
'#options' => $options,
'#default_value' => !empty($this->configuration['builder']) ? $this->configuration['builder'] : 'standard',
];
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
if ($form_state
->hasValue('builder')) {
$this->configuration['builder'] = $form_state
->getValue('builder');
}
$this->configuration['page_title'] = $form_state
->getValue('page_title');
}
public function access(AccountInterface $account = NULL) {
if (empty($this->configuration['blocks'])) {
return FALSE;
}
return parent::access($account);
}
public function defaultConfiguration() {
return parent::defaultConfiguration() + [
'uuid' => $this
->uuidGenerator()
->generate(),
'layout' => '',
'layout_settings' => [],
'page_title' => '',
'storage_type' => '',
'storage_id' => '',
];
}
public function getWizardOperations($cached_values) {
$operations = [];
$operations['layout'] = [
'title' => $this
->t('Layout'),
'form' => LayoutPluginSelector::class,
];
if (!empty($this
->getConfiguration()['layout']) && $cached_values['plugin']
->getLayout() instanceof PluginFormInterface) {
if (empty($cached_values['layout_change']['new_layout'])) {
$layout = $cached_values['plugin']
->getLayout();
$r = new \ReflectionClass(get_class($layout));
}
else {
$layout_definition = \Drupal::service('plugin.manager.layout_plugin')
->getDefinition($cached_values['layout_change']['new_layout']);
$r = new \ReflectionClass($layout_definition['class']);
}
$method = $r
->getMethod('buildConfigurationForm');
if ($method->class != 'Drupal\\layout_plugin\\Plugin\\Layout\\LayoutBase') {
$operations['settings'] = [
'title' => $this
->t('Layout Settings'),
'form' => LayoutChangeSettings::class,
];
}
}
if (!empty($cached_values['layout_change']['old_layout'])) {
$operations['regions'] = [
'title' => $this
->t('Layout Regions'),
'form' => LayoutChangeRegions::class,
];
}
$plugin = $cached_values['plugin'];
$builder = $plugin
->getBuilder();
if ($builder instanceof PluginWizardInterface) {
$operations = array_merge($operations, $builder
->getWizardOperations($cached_values));
}
return $operations;
}
public function setConfiguration(array $configuration) {
if (empty($configuration['uuid'])) {
$configuration['uuid'] = $this
->uuidGenerator()
->generate();
}
if (!empty($configuration['layout']) && !empty($configuration['blocks'])) {
$layout_definition = $this->layoutManager
->getDefinition($configuration['layout']);
$valid_regions = $layout_definition['regions'];
$first_region = array_keys($valid_regions)[0];
foreach ($configuration['blocks'] as &$block) {
if (!isset($valid_regions[$block['region']])) {
$block['region'] = $first_region;
}
}
}
return parent::setConfiguration($configuration);
}
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 = array();
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;
}
}