Redirecting a Form
Using your hook_form_alter or hook_form_FORM_ID_alter function you can redirect where a node form will go after a user submits the form.
The default behavior for a node form is to go to the new node that the form just created.
If the new node received nid 30, then go to node/30. If, however, you would like to go to a thank you page that has nid of 10, you need to redirect the form to go to node/10 instead.
It's easiest to use hook_form_FORM_ID_alter in a module.
Let's say the module is called my_redirect and you've created a content type that has form_id of my_content_type_node_form. You'll be creating two functions:
my_redirect_form_my_content_type_node_form_alter() - this will add an additional submit handler
my_redirect_node_form_submit() - this will contain the address of the node to redirect to
Within your my_redirect module, you create the following:
function my_redirect_form_my_content_type_node_form_alter(&$form, &$form_state){
$form['#submit'][] = 'my_redirect_node_form_submit';
}
You're appending to the ['submit']['#submit'] array so that 1) you make sure that the default node_form_submit() function runs and saves the node, and 2) you can override the redirect setting AFTER node_form_submit() sets it.
function my_redirect_node_form_submit($form, &$form_state) {
// only redirect if node_form_submit() has successfully saved the node
if ( !empty($form_state['nid']) ) {
$form_state['redirect'] = 'node/10'; // the address of your thank you page
// alternatively, you could redirect back to the form using: $form_state['redirect'] = sprintf('node/%d/edit', $form_state['nid']);
// if you are really, really determined to force the redirect, even if a destination has been set in the url, add this: unset($_GET['destination']);
// you could also add a message (this is in addition to whatever message is produced by the nodeapi function): drupal_set_message(t('Congratulations!'));
}
}
If you were to use the debug function (and woe unto those who dare develop in Drupal without it -- and don't forget to go to /admin/config/development/logging and turn on error messages) and look at your form, you'd see
[actions] => Array
(
[#type] => actions
[submit] => Array
(
[#type] => submit
[#access] => 1
[#value] => Save
[#weight] => 5
[#submit] => Array
(
[0] => node_form_submit
[1] => my_redirect_node_form_submit
)
)
As you can see there are two submit functions being called. And now it's a little more obvious why your custom submit function needed to be assigned to $form['actions']['submit']['#submit'][1].
The next time you use your form, you should be directed to the thank you page at node/10.
Examining the code at node_form_submit is a useful exercise, and helps you to understand why the order in which submit functions occur matters. It is also helpful to look at https://api.drupal.org/api/drupal/includes!form.inc/function/drupal_redi..., which explains how the normal form redirect rules work, and the exceptions.
Note that when you examine that code, you see why you really do NOT want to "unset($form_state['rebuild'])" as you see in many answers on this topic. If 'rebuild' is true after node_form_submit() runs, it is because the node did not save. In this case, you do NOT want to redirect, and you do want to permit Drupal to take the user back to the form, warts and all.
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion