Skip to content
Snippets Groups Projects
Commit 53f6c5a4 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2512062 by dsnopek, tim.plunkett, Wim Leers: VariantInterface extends...

Issue #2512062 by dsnopek, tim.plunkett, Wim Leers: VariantInterface extends ConfigurablePluginInterface so PageDisplayVariantSelectionEvent should allow passing configuration to the Variant (to enable Panels Everywhere)
parent bd6afc18
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
......@@ -201,7 +201,9 @@ protected function prepare(array $main_content, Request $request, RouteMatchInte
if (!$page_display instanceof PageVariantInterface) {
throw new \LogicException('Cannot render the main content for this page because the provided display variant does not implement PageVariantInterface.');
}
$page_display->setMainContent($main_content);
$page_display
->setMainContent($main_content)
->setConfiguration($event->getPluginConfiguration());
// Generate a #type => page render array using the page display variant,
// the page display will build the content for the various page regions.
......
......@@ -25,6 +25,13 @@ class PageDisplayVariantSelectionEvent extends Event {
*/
protected $pluginId;
/**
* The configuration for the selected page display variant.
*
* @var array
*/
protected $pluginConfiguration = [];
/**
* The current route match.
*
......@@ -50,9 +57,12 @@ public function __construct($plugin_id, RouteMatchInterface $route_match) {
*
* @param string $plugin_id
* The ID of the page display variant plugin to use.
*
* @return $this
*/
public function setPluginId($plugin_id) {
$this->pluginId = $plugin_id;
return $this;
}
/**
......@@ -64,6 +74,28 @@ public function getPluginId() {
return $this->pluginId;
}
/**
* Set the configuration for the selected page display variant.
*
* @param array $configuration
* The configuration for the selected page display variant.
*
* @return $this
*/
public function setPluginConfiguration(array $configuration) {
$this->pluginConfiguration = $configuration;
return $this;
}
/**
* Get the configuration for the selected page display variant.
*
* @return array
*/
public function getPluginConfiguration() {
return $this->pluginConfiguration;
}
/**
* Gets the current route match.
*
......
......@@ -32,6 +32,7 @@ class SimplePageVariant extends VariantBase implements PageVariantInterface {
*/
public function setMainContent(array $main_content) {
$this->mainContent = $main_content;
return $this;
}
/**
......
<?php
/**
* @file
* Contains \Drupal\system\Tests\Render\DisplayVariantTest.
*/
namespace Drupal\system\Tests\Render;
use Drupal\simpletest\WebTestBase;
/**
* Tests selecting a display variant.
*
* @group Render
*/
class DisplayVariantTest extends WebTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('display_variant_test');
/**
* Tests selecting the variant and passing configuration.
*/
function testPageDisplayVariantSelectionEvent() {
// Tests that our display variant was selected, and that its configuration
// was passed correctly. If the configuration wasn't passed, we'd get an
// error page here.
$this->drupalGet('<front>');
$this->assertRaw('A very important, required value.');
}
}
name: 'Display variant tests'
type: module
description: 'Support module for testing display variants.'
package: Testing
version: VERSION
core: 8.x
services:
display_variant_test.page_display_variant_subscriber:
class: Drupal\display_variant_test\EventSubscriber\TestPageDisplayVariantSubscriber
tags:
- { name: 'event_subscriber', priority: 1000 }
<?php
/**
* @file
* Contains \Drupal\display_variant_test\EventSubscriber\TestPageDisplayVariantSubscriber.
*/
namespace Drupal\display_variant_test\EventSubscriber;
use Drupal\Core\Render\PageDisplayVariantSelectionEvent;
use Drupal\Core\Render\RenderEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Selects the test page display variant.
*/
class TestPageDisplayVariantSubscriber implements EventSubscriberInterface {
/**
* Selects the page display variant.
*
* @param \Drupal\Core\Render\PageDisplayVariantSelectionEvent $event
* The event to process.
*/
public function onSelectPageDisplayVariant(PageDisplayVariantSelectionEvent $event) {
$event->setPluginId('display_variant_test');
$event->setPluginConfiguration(['required_configuration' => 'A very important, required value.']);
}
/**
* {@inheritdoc}
*/
static function getSubscribedEvents() {
$events[RenderEvents::SELECT_PAGE_DISPLAY_VARIANT][] = array('onSelectPageDisplayVariant');
return $events;
}
}
<?php
/**
* @file
* Contains \Drupal\display_variant_test\Plugin\DisplayVariant\TestDisplayVariant.
*/
namespace Drupal\display_variant_test\Plugin\DisplayVariant;
use Drupal\Core\Display\VariantBase;
use Drupal\Core\Display\PageVariantInterface;
/**
* Provides a display variant that requires configuration.
*
* @DisplayVariant(
* id = "display_variant_test",
* admin_label = @Translation("Test display variant")
* )
*/
class TestDisplayVariant extends VariantBase implements PageVariantInterface {
/**
* The render array representing the main page content.
*
* @var array
*/
protected $mainContent = [];
/**
* {@inheritdoc}
*/
public function setMainContent(array $main_content) {
$this->mainContent = $main_content;
return $this;
}
/**
* {@inheritdoc}
*/
public function build() {
$config = $this->getConfiguration();
if (empty($config['required_configuration'])) {
throw new \Exception('Required configuration is missing!');
}
$build = [];
$build['content']['default'] = [
'#markup' => $config['required_configuration'],
];
return $build;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment