Issue #2313479 by tim.plunkett: Add FormState::setResponse().

8.0.x
Alex Pott 2014-08-04 11:23:14 +01:00
parent 537457b512
commit 5fef1a6b37
15 changed files with 43 additions and 20 deletions

View File

@ -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}
*/

View File

@ -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.
*

View File

@ -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;
}
}

View File

@ -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.'));

View File

@ -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']));
}
}

View File

@ -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']));
}
}

View File

@ -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;

View File

@ -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']));
}
}

View File

@ -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']));
}
}

View File

@ -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']));
}
}

View File

@ -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']));
}
}

View File

@ -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']));
}
}

View File

@ -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']));
}
}

View File

@ -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']));
}
}

View File

@ -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'));
}
/**