FAPI

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

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']);
  }
}
?>

Syndicate content