From 6c20edbc8bb646249d2cbb74967dad72a7e87847 Mon Sep 17 00:00:00 2001
From: Merlin <merlin@geeks4change.net>
Date: Tue, 5 May 2020 00:49:16 +0200
Subject: [PATCH] Issue #2363189 by geek-merlin: Do not allow form builder
 functions to return Response objects

---
 core/lib/Drupal/Core/Form/FormBase.php                     | 5 +++++
 core/lib/Drupal/Core/Form/FormSubmitter.php                | 7 +++----
 core/modules/migrate_drupal_ui/src/Form/IdConflictForm.php | 4 +++-
 core/modules/migrate_drupal_ui/src/Form/OverviewForm.php   | 3 ++-
 core/modules/system/src/Form/ModulesListConfirmForm.php    | 3 ++-
 .../system/src/Form/ModulesUninstallConfirmForm.php        | 3 ++-
 6 files changed, 17 insertions(+), 8 deletions(-)

diff --git a/core/lib/Drupal/Core/Form/FormBase.php b/core/lib/Drupal/Core/Form/FormBase.php
index 1974865053..5368a8e980 100644
--- a/core/lib/Drupal/Core/Form/FormBase.php
+++ b/core/lib/Drupal/Core/Form/FormBase.php
@@ -211,8 +211,13 @@ protected function currentUser() {
    *
    * @return \Symfony\Component\HttpFoundation\RedirectResponse
    *   A redirect response object that may be returned by the controller.
+   *
+   * @deprecated in Drupal 9.1.x and will be removed before 10.0.0. Throw a
+   *   \Drupal\Core\Form\EnforcedResponseException instead. See
+   *   https://www.drupal.org/node/2363189.
    */
   protected function redirect($route_name, array $route_parameters = [], array $options = [], $status = 302) {
+    @trigger_error(__METHOD__ . ' is deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. Throw a \Drupal\Core\Form\EnforcedResponseException instead. See https://www.drupal.org/node/2363189. Current form ID: ' . $this->getFormId(), E_USER_DEPRECATED);
     $options['absolute'] = TRUE;
     return new RedirectResponse(Url::fromRoute($route_name, $route_parameters, $options)->toString(), $status);
   }
diff --git a/core/lib/Drupal/Core/Form/FormSubmitter.php b/core/lib/Drupal/Core/Form/FormSubmitter.php
index 4a6a34c807..15cdb19230 100644
--- a/core/lib/Drupal/Core/Form/FormSubmitter.php
+++ b/core/lib/Drupal/Core/Form/FormSubmitter.php
@@ -122,14 +122,13 @@ public function executeSubmitHandlers(&$form, FormStateInterface &$form_state) {
   public function redirectForm(FormStateInterface $form_state) {
     $redirect = $form_state->getRedirect();
 
+    $url = NULL;
     // Allow using redirect responses directly if needed.
     if ($redirect instanceof RedirectResponse) {
-      return $redirect;
+      $url = $redirect->getTargetUrl();
     }
-
-    $url = NULL;
     // Check for a route-based redirection.
-    if ($redirect instanceof Url) {
+    elseif ($redirect instanceof Url) {
       $url = $redirect->setAbsolute()->toString();
     }
     // If no redirect was specified, redirect to the current path.
diff --git a/core/modules/migrate_drupal_ui/src/Form/IdConflictForm.php b/core/modules/migrate_drupal_ui/src/Form/IdConflictForm.php
index 4146bb41d8..f1b9d6c61c 100644
--- a/core/modules/migrate_drupal_ui/src/Form/IdConflictForm.php
+++ b/core/modules/migrate_drupal_ui/src/Form/IdConflictForm.php
@@ -2,7 +2,9 @@
 
 namespace Drupal\migrate_drupal_ui\Form;
 
+use Drupal\Core\Form\EnforcedResponseException;
 use Drupal\Core\Form\FormStateInterface;
+use Drupal\Core\Url;
 use Drupal\migrate\Audit\IdAuditor;
 use Drupal\migrate\Plugin\migrate\destination\EntityContentBase;
 
@@ -70,7 +72,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
     }
     else {
       $this->store->set('step', 'review');
-      return $this->redirect('migrate_drupal_ui.upgrade_review');
+      throw new EnforcedResponseException(Url::fromRoute('migrate_drupal_ui.upgrade_review'));
     }
   }
 
diff --git a/core/modules/migrate_drupal_ui/src/Form/OverviewForm.php b/core/modules/migrate_drupal_ui/src/Form/OverviewForm.php
index 91c9fb2ab4..cef27f74d1 100644
--- a/core/modules/migrate_drupal_ui/src/Form/OverviewForm.php
+++ b/core/modules/migrate_drupal_ui/src/Form/OverviewForm.php
@@ -2,6 +2,7 @@
 
 namespace Drupal\migrate_drupal_ui\Form;
 
+use Drupal\Core\Form\EnforcedResponseException;
 use Drupal\Core\Form\FormStateInterface;
 use Drupal\Core\Url;
 
@@ -27,7 +28,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
     // form.
     if ($this->state->get('migrate_drupal_ui.performed')) {
       $this->store->set('step', 'incremental');
-      return $this->redirect('migrate_drupal_ui.upgrade_incremental');
+      throw new EnforcedResponseException(Url::fromRoute('migrate_drupal_ui.upgrade_incremental'));
     }
 
     $form = parent::buildForm($form, $form_state);
diff --git a/core/modules/system/src/Form/ModulesListConfirmForm.php b/core/modules/system/src/Form/ModulesListConfirmForm.php
index 834b436345..0a98a23000 100644
--- a/core/modules/system/src/Form/ModulesListConfirmForm.php
+++ b/core/modules/system/src/Form/ModulesListConfirmForm.php
@@ -7,6 +7,7 @@
 use Drupal\Core\Extension\ModuleHandlerInterface;
 use Drupal\Core\Extension\ModuleInstallerInterface;
 use Drupal\Core\Form\ConfirmFormBase;
+use Drupal\Core\Form\EnforcedResponseException;
 use Drupal\Core\Form\FormStateInterface;
 use Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface;
 use Drupal\Core\Url;
@@ -118,7 +119,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
 
     // Redirect to the modules list page if the key value store is empty.
     if (!$this->modules) {
-      return $this->redirect('system.modules_list');
+      throw new EnforcedResponseException(Url::fromRoute('system.modules_list'));
     }
 
     $items = $this->buildMessageList();
diff --git a/core/modules/system/src/Form/ModulesUninstallConfirmForm.php b/core/modules/system/src/Form/ModulesUninstallConfirmForm.php
index 37717e2d27..ea8f32476a 100644
--- a/core/modules/system/src/Form/ModulesUninstallConfirmForm.php
+++ b/core/modules/system/src/Form/ModulesUninstallConfirmForm.php
@@ -9,6 +9,7 @@
 use Drupal\Core\Extension\ModuleExtensionList;
 use Drupal\Core\Extension\ModuleInstallerInterface;
 use Drupal\Core\Form\ConfirmFormBase;
+use Drupal\Core\Form\EnforcedResponseException;
 use Drupal\Core\Form\FormStateInterface;
 use Drupal\Core\Url;
 use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -153,7 +154,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
     // Prevent this page from showing when the module list is empty.
     if (empty($this->modules)) {
       $this->messenger()->addError($this->t('The selected modules could not be uninstalled, either due to a website problem or due to the uninstall confirmation form timing out. Please try again.'));
-      return $this->redirect('system.modules_uninstall');
+      throw new EnforcedResponseException(Url::fromRoute('system.modules_uninstall'));
     }
 
     $data = $this->moduleExtensionList->getList();
-- 
2.17.1

