Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Open sidebar
project
drupal
Commits
a7f28c12
Commit
a7f28c12
authored
Oct 30, 2013
by
webchick
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue
#2105661
by dawehner, tim.plunkett, chx: Add support for $form_state()['redirect_route()'].
parent
9e722833
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
88 additions
and
4 deletions
+88
-4
core/lib/Drupal/Core/Form/FormBuilder.php
core/lib/Drupal/Core/Form/FormBuilder.php
+17
-0
core/modules/action/lib/Drupal/action/ActionFormControllerBase.php
...les/action/lib/Drupal/action/ActionFormControllerBase.php
+3
-1
core/modules/action/lib/Drupal/action/Form/ActionAdminManageForm.php
...s/action/lib/Drupal/action/Form/ActionAdminManageForm.php
+4
-1
core/modules/action/lib/Drupal/action/Form/ActionDeleteForm.php
...odules/action/lib/Drupal/action/Form/ActionDeleteForm.php
+3
-1
core/modules/views_ui/lib/Drupal/views_ui/Form/Ajax/ReorderDisplays.php
...iews_ui/lib/Drupal/views_ui/Form/Ajax/ReorderDisplays.php
+5
-1
core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php
core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php
+56
-0
No files found.
core/lib/Drupal/Core/Form/FormBuilder.php
View file @
a7f28c12
...
...
@@ -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'
]))
{
...
...
core/modules/action/lib/Drupal/action/ActionFormControllerBase.php
View file @
a7f28c12
...
...
@@ -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'
,
);
}
}
core/modules/action/lib/Drupal/action/Form/ActionAdminManageForm.php
View file @
a7f28c12
...
...
@@ -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'
]),
);
}
}
...
...
core/modules/action/lib/Drupal/action/Form/ActionDeleteForm.php
View file @
a7f28c12
...
...
@@ -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'
,
);
}
}
core/modules/views_ui/lib/Drupal/views_ui/Form/Ajax/ReorderDisplays.php
View file @
a7f28c12
...
...
@@ -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'
),
);
}
}
core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php
View file @
a7f28c12
...
...
@@ -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.
*
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment