From c0bee1ddda81de14dcc0577a14adb205c7c1138c Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Mon, 3 Oct 2022 14:22:33 +0100
Subject: [PATCH] Issue #3294833 by claudiu.cristea, alexpott: Page variant
 plugin is not correctlly instantiated

---
 core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php    | 6 +++---
 .../src/Plugin/DisplayVariant/TestDisplayVariant.php        | 2 ++
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php b/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php
index d4905fb592a9..1786034ce8b3 100644
--- a/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php
+++ b/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php
@@ -219,6 +219,7 @@ protected function prepare(array $main_content, Request $request, RouteMatchInte
       $event = new PageDisplayVariantSelectionEvent('simple_page', $route_match);
       $this->eventDispatcher->dispatch($event, RenderEvents::SELECT_PAGE_DISPLAY_VARIANT);
       $variant_id = $event->getPluginId();
+      $variant_configuration = $event->getPluginConfiguration();
 
       // We must render the main content now already, because it might provide a
       // title. We set its $is_root_call parameter to FALSE, to ensure
@@ -244,15 +245,14 @@ protected function prepare(array $main_content, Request $request, RouteMatchInte
       $title = $get_title($main_content);
 
       // Instantiate the page display, and give it the main content.
-      $page_display = $this->displayVariantManager->createInstance($variant_id);
+      $page_display = $this->displayVariantManager->createInstance($variant_id, $variant_configuration);
       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)
         ->setTitle($title)
-        ->addCacheableDependency($event)
-        ->setConfiguration($event->getPluginConfiguration());
+        ->addCacheableDependency($event);
       // Some display variants need to be passed an array of contexts with
       // values because they can't get all their contexts globally. For example,
       // in Page Manager, you can create a Page which has a specific static
diff --git a/core/modules/system/tests/modules/display_variant_test/src/Plugin/DisplayVariant/TestDisplayVariant.php b/core/modules/system/tests/modules/display_variant_test/src/Plugin/DisplayVariant/TestDisplayVariant.php
index 4280cd416e7d..cd2b0f4585d3 100644
--- a/core/modules/system/tests/modules/display_variant_test/src/Plugin/DisplayVariant/TestDisplayVariant.php
+++ b/core/modules/system/tests/modules/display_variant_test/src/Plugin/DisplayVariant/TestDisplayVariant.php
@@ -67,6 +67,7 @@ public function setContexts(array $contexts) {
    * {@inheritdoc}
    */
   public function setMainContent(array $main_content) {
+    assert(!empty($this->getConfiguration()['required_configuration']), 'Ensure that ::setMainContent() is called with the variant configuration');
     $this->mainContent = $main_content;
     return $this;
   }
@@ -75,6 +76,7 @@ public function setMainContent(array $main_content) {
    * {@inheritdoc}
    */
   public function setTitle($title) {
+    assert(!empty($this->getConfiguration()['required_configuration']), 'Ensure that ::setTitle() is called with the variant configuration');
     $this->title = $title;
     return $this;
   }
-- 
GitLab