Commit a7f28c12 authored by webchick's avatar webchick

Issue #2105661 by dawehner, tim.plunkett, chx: Add support for $form_state()['redirect_route()'].

parent 9e722833
......@@ -923,6 +923,23 @@ public function redirectForm($form_state) {
if (!empty($form_state['no_redirect'])) {
return;
}
// Allow using 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']['options']['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'])) {
......
......@@ -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',
);
}
}
......@@ -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']),
);
}
}
......
......@@ -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',
);
}
}
......@@ -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'),
);
}
}
......@@ -13,6 +13,7 @@
use Drupal\Core\Session\AccountInterface;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
......@@ -226,6 +227,46 @@ public function testRedirectWithResult($form_state, $result, $status = 302) {
$this->assertSame($status, $redirect->getStatusCode());
}
/**
* Tests the redirectForm() with redirect_route 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('absolute' => TRUE), 'test-route'),
array('test_route_b', array('key' => 'value'), array('absolute' => TRUE), '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.
*
......@@ -237,6 +278,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);
......@@ -259,6 +302,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.
*
......
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