PanelsEverywhereDisplayVariant.php in Panels Everywhere 8.4
File
src/Plugin/DisplayVariant/PanelsEverywhereDisplayVariant.php
View source
<?php
namespace Drupal\panels_everywhere\Plugin\DisplayVariant;
use Drupal\Core\Form\FormStateInterface;
use Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant;
use Drupal\Core\Display\PageVariantInterface;
use Drupal\Core\Block\MainContentBlockPluginInterface;
use Drupal\Core\Block\TitleBlockPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PanelsEverywhereDisplayVariant extends PanelsDisplayVariant implements PageVariantInterface {
protected $renderer;
protected $mainContent = [];
protected $title;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$instance->renderer = $container
->get('renderer');
return $instance;
}
public function setTitle($title) {
$this->title = (string) (is_array($title) ? $this->renderer
->renderRoot($title) : $title);
return $this;
}
public function setMainContent(array $main_content) {
$this->mainContent = $main_content;
return $this;
}
public function build() {
$main_content_included = NULL;
$this
->setPageTitle($this->title);
foreach ($this
->getRegionAssignments() as $region => $blocks) {
if (!$blocks) {
continue;
}
foreach ($blocks as $block_id => $block) {
if ($block instanceof MainContentBlockPluginInterface) {
$block
->setMainContent($this->mainContent);
$main_content_included = [
$region,
$block_id,
];
}
}
}
$build = parent::build();
if (!empty($main_content_included)) {
list($region, $block_id) = $main_content_included;
unset($build[$region][$block_id]['#cache']['keys']);
}
return $build;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['route_override_enabled'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Enable page-manager route override'),
'#default_value' => $this
->isRouteOverrideEnabled(),
'#description' => $this
->t('The default behaviour of page-manager is create a route for the specified path and override any existing route (which effectively replaces that route). This behaviour is not desired for panels_everywhere as it prevents the original content of the route from being rendered properly. This is why panels_everywhere will remove those overrides by default. You may want do enable route overrides in cases you do not want the original route to be processed.'),
];
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$this->configuration['route_override_enabled'] = $form_state
->getValue('route_override_enabled');
}
public function isRouteOverrideEnabled() {
return !empty($this->configuration['route_override_enabled']) ? $this->configuration['route_override_enabled'] : FALSE;
}
}