I'm working on a multi-step form, and one of the last steps is a confirmation page with a 'Continue' and 'Cancel' button. If they hit cancel, I want it to go to a whole other page.

I have a custom submit handler for the cancel button, but the functionality doesn't appear to be working. I have:

function hook_form_cancel_submit($form, &form_state) {
  $form_state['redirect'] = array('another/page');

I don't want the form to be rebuilt after cancellation, but then I wonder how my redirect would get triggered, since it exists only in $form_state?

  • Could you maybe use drupal_goto instead of the redirect, or do you need to keep the values of the form intact and usable? – Jamie Hollern Jul 20 '11 at 15:44
  • drupal_goto() is not used in form submission handlers. – kiamlaluno Jul 20 '11 at 16:19
  • @kiamlaluno "Should not be used", or "will not work"? – user1359 Jul 20 '11 at 16:24
  • It is useless to use drupal_goto(), since Drupal will call the function for you; then, as Chris Pliakas rightly reported, calling it from a form submission handler would avoid other submission handlers are called too. – kiamlaluno Jul 20 '11 at 16:58

Form generation reports the following text:

$form_state['redirect'] is used to redirect the form on submission. It may either be a string containing the destination URL, or an array of arguments compatible with drupal_goto(). See drupal_redirect_form() for complete information.

The function drupal_redirect_form() is called by drupal_process_form(), which calls it after executing the form submission handlers with form_execute_handlers().

if ($form_state['submitted'] && !form_get_errors() && !$form_state['rebuild']) {
  // Execute form submit handlers.
  form_execute_handlers('submit', $form, $form_state);

  // ...

  // If batches were set in the submit handlers, we process them now,
  // possibly ending execution. We make sure we do not react to the batch
  // that is already being processed (if a batch operation performs a
  // drupal_form_submit).
  if ($batch = & batch_get() && !isset($batch['current_set'])) {
    // ...

  // Set a flag to indicate the the form has been processed and executed.
  $form_state['executed'] = TRUE;

  // Redirect the form based on values in $form_state.

  // ...

In short, the form is not rebuild, before redirecting the user to a new page. The $form_state array that is passed to drupal_redirect_form() is the same array passed to form_execute_handler() (the difference is that the form handlers can add or alter values contained in $form_state).

  • That's a lot of copied and pasted code before getting to an answer! :D – user1359 Jul 20 '11 at 17:06
  • I always try giving the proof of what I am saying, rather than leaving the task of finding out if I am correct to who asked the question. :-) – kiamlaluno Jul 20 '11 at 17:18
  • I think part of the idea of a site like this is that it's not up to the asker or the answerer to find out if an answer is correct -- the community can comment, upvote, or downvote an answer, which helps the asker decide on the reliability of the information. – user1359 Jul 20 '11 at 17:26
  • 3
    An answer such as "No, it is not rebuild." is surely less useful than an answer that is more extended, and that points to the involved code. – kiamlaluno Jul 20 '11 at 17:30
  • Yeah, but you can put the answer at the beginning. Halfway through I was about to bail because I wasn't sure if you were getting to an answer or chastising me for not RTFMing :) – user1359 Jul 20 '11 at 17:36

Use drupal_goto()

function mymodule_form_cancel_submit($form, $form_state){
  • 2
    As a point of best practice, it is not advisable to use drupal_goto() in submit handlers as it stops script execution and redirects immediately. Therefore it kills any overrides and prevents other submit handlers from firing. – Chris Pliakas Jul 20 '11 at 16:55
  • 2
    This is good to know. However, for cancellation isn't this good. – iStryker Jul 20 '11 at 17:07

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.