Redirect to "destination" after submitting a multi-step Drupal form
Submitted by Robert Douglass on Wed, 05/06/2009 - 12:49.
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']);
}
}
?>


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