The documentation for drupal_redirect_form() does a bad job describing that $form_state['redirect'] can be an array of arguments to pass to drupal_goto().

Why put the documentation there? The Form API docs say about $form_state:

'redirect': a URL that will be used to redirect the form on submission. See drupal_redirect_form() for complete information.

Why bother?
To prevent confusion like in #1095588: Form redirect w/ anchor causes problems - $form_state['redirect'] = "somepath/somepage#section2";.

Comments

bfroehle’s picture

Status: Active » Needs review
StatusFileSize
new2.29 KB
rfay’s picture

Actually, I think an example is in order here. Essentially the whole thing makes sense with an example in the docs. The problem is when the docs say "oh like drupal_redirect()", which says "oh, like url()", etc.

bfroehle’s picture

What are some good examples?

$form_state['redirect'] = array(
  'user/login',
  array(
    'query' => drupal_get_destination(),
  ),
);
rfay’s picture

I just meant a demonstration example, so

"To redirect to xxx/yyy?zzz=32#something,

$form_state['redirect'] = ...
"

bfroehle’s picture

I've added two examples to the patch in #1. See attached.

rfay’s picture

Works for me. Thanks.

geerlingguy’s picture

Status: Needs review » Reviewed & tested by the community

This is ridiculously helpful. I've been reading through all the redirect pages in the past hour (doesn't help that the D6 method of $form['#redirect'] was changed and not very well documented until recently...), and this issue has resolved a lot of questions I still had.

Please commit asap, to avoid any more confusion here.

rfay’s picture

Version: 7.x-dev » 8.x-dev
Issue tags: +Needs backport to D7

D8 first...

webchick’s picture

Status: Reviewed & tested by the community » Fixed

Nice! This looks like a great improvement.

Committed to 8.x and 7.x.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

dhruba.jyoti.saha’s picture

Hi bfroehle and All,

I m working with D 7.12 and need to redirect the anonymous users to 'user/login' page with customized o/p. I tried to do it by using 'hook_form_form_id_alter(parameters)' for the 'user_login_block' form. By default this form has three more validations. So, to maintain the default drupal validations, I kept my validation callback at the end of the array :

$form['#validate'] = array('user_login_name_validate', 'user_login_authenticate_validate', 'user_login_final_validate', 'my_custom_validate');

And, in the definition of 'my_custom_validate' I tried to use all the possible ways suggested in drupal.org and IRC (#drupal, #drupal-support)to redirect based on the new approach of D 7 and even tried drupal_goto(). But it always redirected me to 'node' page.

Then I tried by keeping my custom call back at the beginning of the array so that it gets executed at the first. But no results.

Now finally I tried the below approach for 'my_custom_validate' (can keep at the first or last of the array. if at the first need to do custom validations and if at the end can use drupal default validation):

function my_custom_validate($form, &$form_state) {
global $base_path;
header('Location: ' . $base_path . 'user/login');
}

This works pretty good.

Note: $form_state['#redirect'] works very nice with a custom form.

Note : I am not much clear about why $form_state['#redirect'] or drupal_goto() didn't work where as PHP 'header' worked. can n e one pls help me understanding it? Thanks in advance.