diff --git a/core/lib/Drupal/Core/Form/FormBuilder.php b/core/lib/Drupal/Core/Form/FormBuilder.php index de36029..031eb69 100644 --- a/core/lib/Drupal/Core/Form/FormBuilder.php +++ b/core/lib/Drupal/Core/Form/FormBuilder.php @@ -921,6 +921,23 @@ public function redirectForm($form_state) { if (!empty($form_state['no_redirect'])) { return; } + + // Allow to use redirect responses directly if needed. + if (isset($form_state['redirect']) && $form_state['redirect'] instanceof RedirectResponse) { + return $form_state['redirect']; + } + + // Check for a route-based redirection. + if (isset($form_state['redirect_route'])) { + $form_state['redirect_route'] += array( + 'route_parameters' => array(), + 'options' => array(), + ); + $form_state['redirect_route']['absolute'] = TRUE; + $url = $this->urlGenerator->generateFromRoute($form_state['redirect_route']['route_name'], $form_state['redirect_route']['route_parameters'], $form_state['redirect_route']['options']); + return new RedirectResponse($url); + } + // Only invoke a redirection if redirect value was not set to FALSE. if (!isset($form_state['redirect']) || $form_state['redirect'] !== FALSE) { if (isset($form_state['redirect'])) { diff --git a/core/modules/action/lib/Drupal/action/ActionFormControllerBase.php b/core/modules/action/lib/Drupal/action/ActionFormControllerBase.php index 2bd8573..3cf0cde 100644 --- a/core/modules/action/lib/Drupal/action/ActionFormControllerBase.php +++ b/core/modules/action/lib/Drupal/action/ActionFormControllerBase.php @@ -149,7 +149,9 @@ public function save(array $form, array &$form_state) { $this->entity->save(); drupal_set_message($this->t('The action has been successfully saved.')); - $form_state['redirect'] = 'admin/config/system/actions'; + $form_state['redirect_route'] = array( + 'route_name' => 'action.admin', + ); } } diff --git a/core/modules/action/lib/Drupal/action/Form/ActionAdminManageForm.php b/core/modules/action/lib/Drupal/action/Form/ActionAdminManageForm.php index 1accfcf..e44a7f8 100644 --- a/core/modules/action/lib/Drupal/action/Form/ActionAdminManageForm.php +++ b/core/modules/action/lib/Drupal/action/Form/ActionAdminManageForm.php @@ -88,7 +88,10 @@ public function buildForm(array $form, array &$form_state) { */ public function submitForm(array &$form, array &$form_state) { if ($form_state['values']['action']) { - $form_state['redirect'] = 'admin/config/system/actions/add/' . $form_state['values']['action']; + $form_state['redirect_route'] = array( + 'route_name' => 'action.admin_add', + 'route_parameters' => array('action_id' => $form_state['values']['action']), + ); } } diff --git a/core/modules/action/lib/Drupal/action/Form/ActionDeleteForm.php b/core/modules/action/lib/Drupal/action/Form/ActionDeleteForm.php index a49a901..968b7f0 100644 --- a/core/modules/action/lib/Drupal/action/Form/ActionDeleteForm.php +++ b/core/modules/action/lib/Drupal/action/Form/ActionDeleteForm.php @@ -46,7 +46,9 @@ public function submit(array $form, array &$form_state) { watchdog('user', 'Deleted action %aid (%action)', array('%aid' => $this->entity->id(), '%action' => $this->entity->label())); drupal_set_message($this->t('Action %action was deleted', array('%action' => $this->entity->label()))); - $form_state['redirect'] = 'admin/config/system/actions'; + $form_state['redirect_route'] = array( + 'route_name' => 'action.admin', + ); } } diff --git a/core/modules/views_ui/lib/Drupal/views_ui/Form/Ajax/ReorderDisplays.php b/core/modules/views_ui/lib/Drupal/views_ui/Form/Ajax/ReorderDisplays.php index 9b4bb38..1bdf851 100644 --- a/core/modules/views_ui/lib/Drupal/views_ui/Form/Ajax/ReorderDisplays.php +++ b/core/modules/views_ui/lib/Drupal/views_ui/Form/Ajax/ReorderDisplays.php @@ -186,7 +186,11 @@ public function submitForm(array &$form, array &$form_state) { // Store in cache. $view->cacheSet(); - $form_state['redirect'] = array('admin/structure/views/view/' . $view->id() . '/edit', array('fragment' => 'views-tab-default')); + $form_state['redirect_route'] = array( + 'route_name' => 'views_ui.operation', + 'route_parameters' => array('view' => $view->id(), 'operation' => 'edit'), + 'options' => array('fragment' => 'views-tab-default'), + ); } } diff --git a/core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php b/core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php index 4e3c0db..16d49a9 100644 --- a/core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php +++ b/core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php @@ -13,7 +13,8 @@ use Drupal\Core\Session\AccountInterface; use Drupal\Tests\UnitTestCase; use Symfony\Component\DependencyInjection\ContainerInterface; -use Symfony\Component\HttpFoundation\Request; + use Symfony\Component\HttpFoundation\RedirectResponse; + use Symfony\Component\HttpFoundation\Request; /** * Tests the form builder. @@ -223,6 +224,46 @@ public function testRedirectWithResult($form_state, $result, $status = 302) { } /** + * Tests the redirectForm() with redirect_form when a redirect is expected. + * + * @param array $form_state + * An array of form state data to use for the redirect. + * @param string $result + * The URL the redirect is targeting. + * @param int $status + * (optional) The HTTP status code for the redirect. + * + * @dataProvider providerTestRedirectWithRouteWithResult + */ + public function testRedirectWithRouteWithResult($form_state, $result, $status = 302) { + $this->urlGenerator->expects($this->once()) + ->method('generateFromRoute') + ->will($this->returnValueMap(array( + array('test_route_a', array(), array(), 'test-route'), + array('test_route_b', array('key' => 'value'), array(), 'test-route/value'), + )) + ); + + $form_state += $this->formBuilder->getFormStateDefaults(); + $redirect = $this->formBuilder->redirectForm($form_state); + $this->assertSame($result, $redirect->getTargetUrl()); + $this->assertSame($status, $redirect->getStatusCode()); + } + + /** + * Tests the redirectForm() method with a response object. + */ + public function testRedirectWithResponseObject() { + $redirect = new RedirectResponse('/example'); + $form_state['redirect'] = $redirect; + + $form_state += $this->formBuilder->getFormStateDefaults(); + $result_redirect = $this->formBuilder->redirectForm($form_state); + + $this->assertSame($redirect, $result_redirect); + } + + /** * Tests the redirectForm() method when no redirect is expected. * * @param array $form_state @@ -233,6 +274,8 @@ public function testRedirectWithResult($form_state, $result, $status = 302) { public function testRedirectWithoutResult($form_state) { $this->urlGenerator->expects($this->never()) ->method('generateFromPath'); + $this->urlGenerator->expects($this->never()) + ->method('generateFromRoute'); $form_state += $this->formBuilder->getFormStateDefaults(); $redirect = $this->formBuilder->redirectForm($form_state); $this->assertNull($redirect); @@ -256,6 +299,19 @@ public function providerTestRedirectWithResult() { } /** + * Provides test data for testing the redirectForm() method with a route name. + * + * @return array + * Returns some test data. + */ + public function providerTestRedirectWithRouteWithResult() { + return array( + array(array('redirect_route' => array('route_name' => 'test_route_a')), 'test-route'), + array(array('redirect_route' => array('route_name' => 'test_route_b', 'route_parameters' => array('key' => 'value'))), 'test-route/value'), + ); + } + + /** * Provides test data for testing the redirectForm() method with no redirect. * * @return array