From 5fef1a6b370bac83057f92061b55a72e44879957 Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Mon, 4 Aug 2014 11:23:14 +0100 Subject: [PATCH] Issue #2313479 by tim.plunkett: Add FormState::setResponse(). --- core/lib/Drupal/Core/Form/FormState.php | 9 +++++++++ core/lib/Drupal/Core/Form/FormStateInterface.php | 14 ++++++++++++++ core/lib/Drupal/Core/Form/FormSubmitter.php | 8 ++++---- core/modules/locale/src/Form/ExportForm.php | 2 +- .../form_test/src/Form/FormTestCheckboxForm.php | 2 +- .../src/Form/FormTestCheckboxesRadiosForm.php | 2 +- .../src/Form/FormTestCheckboxesZeroForm.php | 2 +- .../form_test/src/Form/FormTestColorForm.php | 2 +- .../src/Form/FormTestDisabledElementsForm.php | 2 +- .../form_test/src/Form/FormTestEmailForm.php | 2 +- .../src/Form/FormTestLanguageSelectForm.php | 2 +- .../form_test/src/Form/FormTestRangeForm.php | 2 +- .../form_test/src/Form/FormTestSelectForm.php | 2 +- .../modules/form_test/src/Form/FormTestUrlForm.php | 2 +- .../Drupal/Tests/Core/Form/FormBuilderTest.php | 10 +++++----- 15 files changed, 43 insertions(+), 20 deletions(-) diff --git a/core/lib/Drupal/Core/Form/FormState.php b/core/lib/Drupal/Core/Form/FormState.php index b0a21db0f0a..f015f14cf05 100644 --- a/core/lib/Drupal/Core/Form/FormState.php +++ b/core/lib/Drupal/Core/Form/FormState.php @@ -8,6 +8,7 @@ namespace Drupal\Core\Form; use Drupal\Core\Url; +use Symfony\Component\HttpFoundation\Response; /** * Stores information about the state of a form. @@ -579,6 +580,14 @@ class FormState implements FormStateInterface, \ArrayAccess { return $this; } + /** + * {@inheritdoc} + */ + public function setResponse(Response $response) { + $this->set('response', $response); + return $this; + } + /** * {@inheritdoc} */ diff --git a/core/lib/Drupal/Core/Form/FormStateInterface.php b/core/lib/Drupal/Core/Form/FormStateInterface.php index 9cabff74ab0..3f3fe96137b 100644 --- a/core/lib/Drupal/Core/Form/FormStateInterface.php +++ b/core/lib/Drupal/Core/Form/FormStateInterface.php @@ -8,6 +8,7 @@ namespace Drupal\Core\Form; use Drupal\Core\Url; +use Symfony\Component\HttpFoundation\Response; /** * Provides an interface for an object containing the current state of a form. @@ -75,6 +76,19 @@ interface FormStateInterface { */ public function setIfNotExists($property, $value); + /** + * Sets a response for this form. + * + * If a response is set, it will be used during processing and returned + * directly. The form will not be rebuilt or redirected. + * + * @param \Symfony\Component\HttpFoundation\Response $response + * The response to return. + * + * @return $this + */ + public function setResponse(Response $response); + /** * Sets the redirect URL for the form. * diff --git a/core/lib/Drupal/Core/Form/FormSubmitter.php b/core/lib/Drupal/Core/Form/FormSubmitter.php index 4c5d1c75993..fb447a2974f 100644 --- a/core/lib/Drupal/Core/Form/FormSubmitter.php +++ b/core/lib/Drupal/Core/Form/FormSubmitter.php @@ -79,13 +79,13 @@ class FormSubmitter implements FormSubmitterInterface { $form_state['executed'] = TRUE; // If no response has been set, process the form redirect. - if (!isset($form_state['response']) && $redirect = $this->redirectForm($form_state)) { - $form_state['response'] = $redirect; + if (!$form_state->has('response') && $redirect = $this->redirectForm($form_state)) { + $form_state->setResponse($redirect); } // If there is a response was set, return it instead of continuing. - if (isset($form_state['response']) && $form_state['response'] instanceof Response) { - return $form_state['response']; + if (($response = $form_state->get('response')) && $response instanceof Response) { + return $response; } } diff --git a/core/modules/locale/src/Form/ExportForm.php b/core/modules/locale/src/Form/ExportForm.php index 5177c694d86..1a7772e3464 100644 --- a/core/modules/locale/src/Form/ExportForm.php +++ b/core/modules/locale/src/Form/ExportForm.php @@ -169,7 +169,7 @@ class ExportForm extends FormBase { $response = new BinaryFileResponse($uri); $response->setContentDisposition('attachment', $filename); - $form_state['response'] = $response; + $form_state->setResponse($response); } else { drupal_set_message($this->t('Nothing to export.')); diff --git a/core/modules/system/tests/modules/form_test/src/Form/FormTestCheckboxForm.php b/core/modules/system/tests/modules/form_test/src/Form/FormTestCheckboxForm.php index 6a23006ae16..63f8ebbe42e 100644 --- a/core/modules/system/tests/modules/form_test/src/Form/FormTestCheckboxForm.php +++ b/core/modules/system/tests/modules/form_test/src/Form/FormTestCheckboxForm.php @@ -92,7 +92,7 @@ class FormTestCheckboxForm extends FormBase { * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { - $form_state['response'] = new JsonResponse($form_state['values']); + $form_state->setResponse(new JsonResponse($form_state['values'])); } } diff --git a/core/modules/system/tests/modules/form_test/src/Form/FormTestCheckboxesRadiosForm.php b/core/modules/system/tests/modules/form_test/src/Form/FormTestCheckboxesRadiosForm.php index 721399e1585..1077c425e7c 100644 --- a/core/modules/system/tests/modules/form_test/src/Form/FormTestCheckboxesRadiosForm.php +++ b/core/modules/system/tests/modules/form_test/src/Form/FormTestCheckboxesRadiosForm.php @@ -84,7 +84,7 @@ class FormTestCheckboxesRadiosForm extends FormBase { * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { - $form_state['response'] = new JsonResponse($form_state['values']); + $form_state->setResponse(new JsonResponse($form_state['values'])); } } diff --git a/core/modules/system/tests/modules/form_test/src/Form/FormTestCheckboxesZeroForm.php b/core/modules/system/tests/modules/form_test/src/Form/FormTestCheckboxesZeroForm.php index 709a76d371d..70efd25fba7 100644 --- a/core/modules/system/tests/modules/form_test/src/Form/FormTestCheckboxesZeroForm.php +++ b/core/modules/system/tests/modules/form_test/src/Form/FormTestCheckboxesZeroForm.php @@ -57,7 +57,7 @@ class FormTestCheckboxesZeroForm extends FormBase { */ public function submitForm(array &$form, FormStateInterface $form_state) { if (!empty($form_state['json'])) { - $form_state['response'] = new JsonResponse($form_state['values']); + $form_state->setResponse(new JsonResponse($form_state['values'])); } else { $form_state['redirect'] = FALSE; diff --git a/core/modules/system/tests/modules/form_test/src/Form/FormTestColorForm.php b/core/modules/system/tests/modules/form_test/src/Form/FormTestColorForm.php index 3603631fbfd..9d37c5af47b 100644 --- a/core/modules/system/tests/modules/form_test/src/Form/FormTestColorForm.php +++ b/core/modules/system/tests/modules/form_test/src/Form/FormTestColorForm.php @@ -42,7 +42,7 @@ class FormTestColorForm extends FormBase { * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { - $form_state['response'] = new JsonResponse($form_state['values']); + $form_state->setResponse(new JsonResponse($form_state['values'])); } } diff --git a/core/modules/system/tests/modules/form_test/src/Form/FormTestDisabledElementsForm.php b/core/modules/system/tests/modules/form_test/src/Form/FormTestDisabledElementsForm.php index 3555ab67416..da3b8a2de31 100644 --- a/core/modules/system/tests/modules/form_test/src/Form/FormTestDisabledElementsForm.php +++ b/core/modules/system/tests/modules/form_test/src/Form/FormTestDisabledElementsForm.php @@ -225,7 +225,7 @@ class FormTestDisabledElementsForm extends FormBase { * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { - $form_state['response'] = new JsonResponse($form_state['values']); + $form_state->setResponse(new JsonResponse($form_state['values'])); } } diff --git a/core/modules/system/tests/modules/form_test/src/Form/FormTestEmailForm.php b/core/modules/system/tests/modules/form_test/src/Form/FormTestEmailForm.php index 11bad32ba95..f5ceece94d3 100644 --- a/core/modules/system/tests/modules/form_test/src/Form/FormTestEmailForm.php +++ b/core/modules/system/tests/modules/form_test/src/Form/FormTestEmailForm.php @@ -49,7 +49,7 @@ class FormTestEmailForm extends FormBase { * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { - $form_state['response'] = new JsonResponse($form_state['values']); + $form_state->setResponse(new JsonResponse($form_state['values'])); } } diff --git a/core/modules/system/tests/modules/form_test/src/Form/FormTestLanguageSelectForm.php b/core/modules/system/tests/modules/form_test/src/Form/FormTestLanguageSelectForm.php index 71d8311c85f..e0f9900c658 100644 --- a/core/modules/system/tests/modules/form_test/src/Form/FormTestLanguageSelectForm.php +++ b/core/modules/system/tests/modules/form_test/src/Form/FormTestLanguageSelectForm.php @@ -67,7 +67,7 @@ class FormTestLanguageSelectForm extends FormBase { * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { - $form_state['response'] = new JsonResponse($form_state['values']); + $form_state->setResponse(new JsonResponse($form_state['values'])); } } diff --git a/core/modules/system/tests/modules/form_test/src/Form/FormTestRangeForm.php b/core/modules/system/tests/modules/form_test/src/Form/FormTestRangeForm.php index 34f1e52b10c..b7119df2c57 100644 --- a/core/modules/system/tests/modules/form_test/src/Form/FormTestRangeForm.php +++ b/core/modules/system/tests/modules/form_test/src/Form/FormTestRangeForm.php @@ -70,7 +70,7 @@ class FormTestRangeForm extends FormBase { * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { - $form_state['response'] = new JsonResponse($form_state['values']); + $form_state->setResponse(new JsonResponse($form_state['values'])); } } diff --git a/core/modules/system/tests/modules/form_test/src/Form/FormTestSelectForm.php b/core/modules/system/tests/modules/form_test/src/Form/FormTestSelectForm.php index eba498b3905..c7a38f99503 100644 --- a/core/modules/system/tests/modules/form_test/src/Form/FormTestSelectForm.php +++ b/core/modules/system/tests/modules/form_test/src/Form/FormTestSelectForm.php @@ -128,7 +128,7 @@ class FormTestSelectForm extends FormBase { * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { - $form_state['response'] = new JsonResponse($form_state['values']); + $form_state->setResponse(new JsonResponse($form_state['values'])); } } diff --git a/core/modules/system/tests/modules/form_test/src/Form/FormTestUrlForm.php b/core/modules/system/tests/modules/form_test/src/Form/FormTestUrlForm.php index 297b6046212..fe95e91bdc5 100644 --- a/core/modules/system/tests/modules/form_test/src/Form/FormTestUrlForm.php +++ b/core/modules/system/tests/modules/form_test/src/Form/FormTestUrlForm.php @@ -49,7 +49,7 @@ class FormTestUrlForm extends FormBase { * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { - $form_state['response'] = new JsonResponse($form_state['values']); + $form_state->setResponse(new JsonResponse($form_state['values'])); } } diff --git a/core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php b/core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php index fede0c1a53b..144ed65e380 100644 --- a/core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php +++ b/core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php @@ -104,7 +104,7 @@ class FormBuilderTest extends FormTestBase { } /** - * Tests the handling of $form_state['response']. + * Tests the handling of FormStateInterface::$response. * * @dataProvider formStateResponseProvider */ @@ -136,7 +136,7 @@ class FormBuilderTest extends FormTestBase { catch (\Exception $e) { $this->assertSame('exit', $e->getMessage()); } - $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $form_state['response']); + $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $form_state->get('response')); } /** @@ -150,7 +150,7 @@ class FormBuilderTest extends FormTestBase { } /** - * Tests the handling of a redirect when $form_state['response'] exists. + * Tests the handling of a redirect when FormStateInterface::$response exists. */ public function testHandleRedirectWithResponse() { $form_id = 'test_form_id'; @@ -176,7 +176,7 @@ class FormBuilderTest extends FormTestBase { ->method('submitForm') ->will($this->returnCallback(function ($form, FormStateInterface $form_state) use ($response, $redirect) { // Set both the response and the redirect. - $form_state['response'] = $response; + $form_state->setResponse($response); $form_state['redirect'] = $redirect; })); @@ -190,7 +190,7 @@ class FormBuilderTest extends FormTestBase { catch (\Exception $e) { $this->assertSame('exit', $e->getMessage()); } - $this->assertSame($response, $form_state['response']); + $this->assertSame($response, $form_state->get('response')); } /**