I'm trying to make the user creation form redirect back to the home page when you submit it. I'm using Content Profiles so it is two forms sharing a submit button. Even if I set #redirect = '/' on both forms the form always redirects back to itself. It works and everything, but won't redirect how I want.

Any ideas?


function mymodule_form_alter(&$form, $form_state, $form_id) {
      switch ($form_id) {
        case 'user_register':
        $form['#redirect'] = '/';
        case 'coordinator_node_form':
        $form['#redirect'] = '/';           
  • Can we see the code you're using? – jordojuice Sep 14 '11 at 21:56
  • There ya go. Hopefully you spot something. – Joren Sep 14 '11 at 22:07

There are two facts to keep in consideration about $form['#redirect']:

  • $form_state['#redirect'], set in a form submission handler, takes the precedence over $form['#redirect'].
  • $form['#redirect'] and $form_state['#redirect'] are ignored for forms where "#action" has been set by a module. That is what happen with the user login form.
  • $form['#redirect'] and $form_state['#redirect'] are ignored when $_REQUEST['destination'] is set; its value is instead used.

If you want to redirect a form, and be sure it gets redirected to the path you set, you should implement hook_form_alter() to add a form submission handler that alters $form_state['#redirect']. The module implementing that hook should be executed after other modules that alter $form_state['#redirect'] for that module.
This doesn't have any effect, though, in the last two cases I listed before.

| improve this answer | |
  • So is there any downside to my solution? Unsetting destination in hook_form_alter seems to work, but are there situations in which it won't work? – Joren Sep 15 '11 at 18:07
  • The "downside" is that you cannot be sure the user is effectively redirected to the page you want; it's enough another module adds a submission handler, and it sets $form_state['#redirect']. – kiamlaluno Sep 15 '11 at 18:54
  • So how do I redirect a form that has the action set? – Joren Sep 15 '11 at 22:19
  • You unset $form['#action']. – kiamlaluno Sep 15 '11 at 22:43
  • @kiamlaluno, thanks for the insight but where is all this information present in the FAPI documentation at Drupal site? – AgA Dec 26 '11 at 18:17

Ok after much digging I found the answer here: http://drupal.org/node/934664

Apparently destination overwrites #redirect so I just unset destination and it works.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.