3 namespace Drupal\Core\Render;
5 use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
6 use Drupal\Core\Cache\RefinableCacheableDependencyTrait;
7 use Drupal\Core\Routing\RouteMatchInterface;
8 use Symfony\Component\EventDispatcher\Event;
11 * Event fired when rendering main content, to select a page display variant.
13 * Subscribers of this event can call the following setters to pass additional
14 * information along to the selected variant:
15 * - self::setPluginConfiguration()
16 * - self::setContexts()
17 * - self::addCacheableDependency()
19 * @see \Drupal\Core\Render\RenderEvents::SELECT_PAGE_DISPLAY_VARIANT
20 * @see \Drupal\Core\Render\MainContent\HtmlRenderer
22 class PageDisplayVariantSelectionEvent extends Event implements RefinableCacheableDependencyInterface {
24 use RefinableCacheableDependencyTrait;
27 * The selected page display variant plugin ID.
34 * The configuration for the selected page display variant.
38 protected $pluginConfiguration = [];
41 * The current route match.
43 * @var \Drupal\Core\Routing\RouteMatchInterface
45 protected $routeMatch;
48 * An array of collected contexts to pass to the page display variant.
50 * @var \Drupal\Component\Plugin\Context\ContextInterface[]
52 protected $contexts = [];
55 * Constructs the page display variant plugin selection event.
57 * @param string $plugin_id
58 * The ID of the page display variant plugin to use by default.
59 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
60 * The current route match, for context.
62 public function __construct($plugin_id, RouteMatchInterface $route_match) {
63 $this->pluginId = $plugin_id;
64 $this->routeMatch = $route_match;
68 * The selected page display variant plugin ID.
70 * @param string $plugin_id
71 * The ID of the page display variant plugin to use.
75 public function setPluginId($plugin_id) {
76 $this->pluginId = $plugin_id;
81 * The selected page display variant plugin ID.
85 public function getPluginId() {
86 return $this->pluginId;
90 * Set the configuration for the selected page display variant.
92 * @param array $configuration
93 * The configuration for the selected page display variant.
97 public function setPluginConfiguration(array $configuration) {
98 $this->pluginConfiguration = $configuration;
103 * Get the configuration for the selected page display variant.
107 public function getPluginConfiguration() {
108 return $this->pluginConfiguration;
112 * Gets the current route match.
114 * @return \Drupal\Core\Routing\RouteMatchInterface
115 * The current route match, for context.
117 public function getRouteMatch() {
118 return $this->routeMatch;
122 * Gets the contexts that were set during event dispatch.
124 * @return \Drupal\Component\Plugin\Context\ContextInterface[]
125 * An array of set contexts, keyed by context name.
127 public function getContexts() {
128 return $this->contexts;
132 * Sets the contexts to be passed to the page display variant.
134 * @param \Drupal\Component\Plugin\Context\ContextInterface[] $contexts
135 * An array of contexts, keyed by context name.
139 public function setContexts(array $contexts) {
140 $this->contexts = $contexts;