From 8c50a65a7d27c3e38b2979074656e3cf70738ce4 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Sat, 6 Apr 2024 12:48:31 +0100
Subject: [PATCH] Issue #3427398 by aaron.ferris, alexpott, sdhruvi5142,
 rkoller, smustgrave, DanielVeza: Cancel button on the discard changes in the
 layout builder confirmation step should take you back to the layout builder

---
 .../layout_builder/src/Form/DiscardLayoutChangesForm.php | 4 ++--
 .../src/FunctionalJavascript/LayoutBuilderUiTest.php     | 9 ++++++++-
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/core/modules/layout_builder/src/Form/DiscardLayoutChangesForm.php b/core/modules/layout_builder/src/Form/DiscardLayoutChangesForm.php
index 5f49f298a830..5e4f7c250310 100644
--- a/core/modules/layout_builder/src/Form/DiscardLayoutChangesForm.php
+++ b/core/modules/layout_builder/src/Form/DiscardLayoutChangesForm.php
@@ -79,7 +79,7 @@ public function getQuestion() {
    * {@inheritdoc}
    */
   public function getCancelUrl() {
-    return $this->sectionStorage->getRedirectUrl();
+    return $this->sectionStorage->getLayoutBuilderUrl();
   }
 
   /**
@@ -100,7 +100,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
 
     $this->messenger->addMessage($this->t('The changes to the layout have been discarded.'));
 
-    $form_state->setRedirectUrl($this->getCancelUrl());
+    $form_state->setRedirectUrl($this->sectionStorage->getRedirectUrl());
   }
 
 }
diff --git a/core/modules/layout_builder/tests/src/FunctionalJavascript/LayoutBuilderUiTest.php b/core/modules/layout_builder/tests/src/FunctionalJavascript/LayoutBuilderUiTest.php
index 5929fcb365eb..7b6e8cb1e4fb 100644
--- a/core/modules/layout_builder/tests/src/FunctionalJavascript/LayoutBuilderUiTest.php
+++ b/core/modules/layout_builder/tests/src/FunctionalJavascript/LayoutBuilderUiTest.php
@@ -94,10 +94,17 @@ public function testUnsavedChangesMessage() {
     $assert_session = $this->assertSession();
     $page = $this->getSession()->getPage();
 
-    // Make and then discard changes.
     $this->assertModifiedLayout(static::FIELD_UI_PREFIX . '/display/default/layout');
+    // Discard then cancel.
+    $page->pressButton('Discard changes');
+    $page->clickLink('Cancel');
+    $assert_session->addressEquals(static::FIELD_UI_PREFIX . '/display/default/layout');
+    $assert_session->pageTextContainsOnce('You have unsaved changes.');
+
+    // Discard then confirm.
     $page->pressButton('Discard changes');
     $page->pressButton('Confirm');
+    $assert_session->addressEquals(static::FIELD_UI_PREFIX . '/display/default');
     $assert_session->pageTextNotContains('You have unsaved changes.');
 
     // Make and then save changes.
-- 
GitLab