Changing the 'redirect' value of a Drupal node form in D6

Generally, when you want to change the location that a for redirects to after submission, you usually should set $form_state['#redirect'] within a call to hook_form_alter().

<?php
/**
* Implementation of hook_form_alter
*/
function custom_module_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'node_type_form') {
    // Set form redirect
    $form['#redirect'] = some/other/path;
  }
}
?>

Unfortunately, that doesn't work for the node form. The node form has three submission buttons ('Submit', 'Preview', and 'Delete'), and each of these has it's own submission handler which sets the redirect value. Furthermore, the redirect values set in the submission handlers associated with the buttons overwrite $form['#redirect'] as set in the code example above.

In order to redirect the node form, you need to add your own submission handler to be executed after the default handler:

<?php
/**
* Implementation of hook_form_alter
*/
function custom_module_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'node_type_form') {
    // Overwrite the default node submission handler with our own.
    $form['buttons']['submit']['#submit'][] = 'my_module_example_form_submit'
  }
}
?>

Since our submit handler is added to the submit array after the original node-form-submit handler and therefore executed later, we can overwrite $form_state['redirect'] variable:

<?php
/**
 * Custom submit handler. Overwrites the form redirection variable.
 */
function my_module_example_form_submit($form, &$form_state) {
  $form_state['#redirect'] = 'some/other/path';
}
?>

That should do it - the node form should now redirect to whatever path your set in the submit handler.

Tags: