Redirect to "destination" after submitting a multi-step Drupal form
Tagged:  •    •    •  

For the HelpInject module I needed a multi-step form. This is pretty easy in Drupal 6. The problem I encountered was that the “destination” parameter was no longer being honored, and the form was submitting to itself after the last step. It was supposed to be redirecting to “destination”. Here’s the code that made it work. The tip is to unset $form_state['storage'] after you’re done with it because for some reason that’s FAPI’s litmus test for whether or not to redirect.

<?php
// The form is built like this
function foobar_form(&$form_state, $type) {
  if (empty(
$form_state['storage']['foo'])) { } else {  }
}        

// The submit handler follows a similar pattern.
function foobar_form_submit($form, &$form_state) {
  if (empty(
$form_state['storage']['foo'])) {
   
$form_state['storage']['foo'] = $form_state['values']['foo'];
   
$form_state['rebuild'] = TRUE;
  } else {
    ... do
CRUD ...
   
// if you want your form to respect destination,
    // unset the storage.
   
unset($form_state['storage']);
  }
}
?>

Login or register to tag items

Cool

Thanks for sharing, it's indeed something who can be useful to know!

Odd behavior

Odd that FAPI works this way. Thanks for the tip.
Added to http://DrupalSightings.com

thanks

thank you very much for sharing this

hours

This would have taken me hours to figure out, thanks

thanks for sharing. Drupal

thanks for sharing.
Drupal is an Open-Source Content Management System capable of forming the backbone of corporate intranets, corporate blogging systems, personal blogging systems and many other varieties of web presence.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote>
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Lines and paragraphs break automatically.

More information about formatting options