Web Design, SEO and Web Applications Development

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.

Comments

Thank you!

This was very helpful. That extra wrinkle to the node form submit handling was really causing me problems.

Can you please explain where to put the functions?

This is what I've been looking for. Thanks! I am trying to make a content create form in a block that can slide open/close on the home page, but I got stuck in redirecting users to the same block when they click the 'save' button. By default, they will be directed to a new page /node/xx.

So I understand the content of your functions, but not sure how to modify the function name, hooks, and which file to put them in. FYI, I am in D6. I am a newbie and your explanation will be really helpful.
Thanks a lot!

Leaveleaf, You need to place

Leaveleaf,

You need to place these functions in a custom module. Your best bet is to start with the Module developer's guide. Also, I would refer to the API documentation for hook_form_alter().

lost my actions

Hi there,

Thanks so much for this - it works a treat. I have used it to redirect users when they submit a conference registration form.

The problem I have is that previously I had some actions set up - one to display a message "thanks for your registration" to the user, and one to send an email to the event manager when a new registration is submitted.

These don't seem to work any more. Why would this be, and how can I get those actions triggered again?

TIA,

Phil

Post new comment

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

More information about formatting options