Commit 5fef1a6b authored by alexpott's avatar alexpott
Browse files

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

parent 537457b5
......@@ -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 @@ public function addValue($property, $value) {
return $this;
}
/**
* {@inheritdoc}
*/
public function setResponse(Response $response) {
$this->set('response', $response);
return $this;
}
/**
* {@inheritdoc}
*/
......
......@@ -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 @@ public function setFormState(array $form_state_additions);
*/
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.
*
......
......@@ -79,13 +79,13 @@ public function doSubmitForm(&$form, FormStateInterface &$form_state) {
$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;
}
}
......
......@@ -169,7 +169,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
$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.'));
......
......@@ -92,7 +92,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
* {@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']));
}
}
......@@ -84,7 +84,7 @@ public function buildForm(array $form, FormStateInterface $form_state, $customiz
* {@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']));
}
}
......@@ -57,7 +57,7 @@ public function buildForm(array $form, FormStateInterface $form_state, $json = T
*/
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;
......
......@@ -42,7 +42,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
* {@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']));
}
}
......@@ -225,7 +225,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
* {@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']));
}
}
......@@ -49,7 +49,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
* {@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']));
}
}
......@@ -67,7 +67,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
* {@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']));
}
}
......@@ -70,7 +70,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
* {@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']));
}
}
......@@ -128,7 +128,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
* {@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']));
}
}
......@@ -49,7 +49,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
* {@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']));
}
}
......@@ -104,7 +104,7 @@ public function testGetFormIdWithBaseForm() {
}
/**
* Tests the handling of $form_state['response'].
* Tests the handling of FormStateInterface::$response.
*
* @dataProvider formStateResponseProvider
*/
......@@ -136,7 +136,7 @@ public function testHandleFormStateResponse($class, $form_state_key) {
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 @@ public function formStateResponseProvider() {
}
/**
* 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 @@ public function testHandleRedirectWithResponse() {
->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 @@ public function testHandleRedirectWithResponse() {
catch (\Exception $e) {
$this->assertSame('exit', $e->getMessage());
}
$this->assertSame($response, $form_state['response']);
$this->assertSame($response, $form_state->get('response'));
}
/**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment