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()
.
<span style="color: #000000"><span style="color: #0000BB"><?php<br></span><span style="color: #FF8000">/**<br>* Implementation of hook_form_alter<br>*/<br></span><span style="color: #007700">function </span><span style="color: #0000BB">custom_module_form_alter</span><span style="color: #007700">(&</span><span style="color: #0000BB">$form</span><span style="color: #007700">, </span><span style="color: #0000BB">$form_state</span><span style="color: #007700">, </span><span style="color: #0000BB">$form_id</span><span style="color: #007700">) {<br> if (</span><span style="color: #0000BB">$form_id </span><span style="color: #007700">== </span><span style="color: #DD0000">'node_type_form'</span><span style="color: #007700">) {<br> </span><span style="color: #FF8000">// Set form redirect<br> </span><span style="color: #0000BB">$form</span><span style="color: #007700">[</span><span style="color: #DD0000">'#redirect'</span><span style="color: #007700">] = </span><span style="color: #0000BB">some</span><span style="color: #007700">/</span><span style="color: #0000BB">other</span><span style="color: #007700">/</span><span style="color: #0000BB">path</span><span style="color: #007700">;<br> }<br>}<br></span><span style="color: #0000BB">?></span></span>
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:
<span style="color: #000000"><span style="color: #0000BB"><?php<br></span><span style="color: #FF8000">/**<br>* Implementation of hook_form_alter<br>*/<br></span><span style="color: #007700">function </span><span style="color: #0000BB">custom_module_form_alter</span><span style="color: #007700">(&</span><span style="color: #0000BB">$form</span><span style="color: #007700">, </span><span style="color: #0000BB">$form_state</span><span style="color: #007700">, </span><span style="color: #0000BB">$form_id</span><span style="color: #007700">) {<br> if (</span><span style="color: #0000BB">$form_id </span><span style="color: #007700">== </span><span style="color: #DD0000">'node_type_form'</span><span style="color: #007700">) {<br> </span><span style="color: #FF8000">// Overwrite the default node submission handler with our own.<br> </span><span style="color: #0000BB">$form</span><span style="color: #007700">[</span><span style="color: #DD0000">'buttons'</span><span style="color: #007700">][</span><span style="color: #DD0000">'submit'</span><span style="color: #007700">][</span><span style="color: #DD0000">'#submit'</span><span style="color: #007700">][] = </span><span style="color: #DD0000">'my_module_example_form_submit'<br> </span><span style="color: #007700">}<br>}<br></span><span style="color: #0000BB">?></span></span>
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:
<span style="color: #000000"><span style="color: #0000BB"><?php<br></span><span style="color: #FF8000">/**<br> * Custom submit handler. Overwrites the form redirection variable.<br> */<br></span><span style="color: #007700">function </span><span style="color: #0000BB">my_module_example_form_submit</span><span style="color: #007700">(</span><span style="color: #0000BB">$form</span><span style="color: #007700">, &</span><span style="color: #0000BB">$form_state</span><span style="color: #007700">) {<br> </span><span style="color: #0000BB">$form_state</span><span style="color: #007700">[</span><span style="color: #DD0000">'#redirect'</span><span style="color: #007700">] = </span><span style="color: #DD0000">'some/other/path'</span><span style="color: #007700">;<br>}<br></span><span style="color: #0000BB">?></span></span>
That should do it - the node form should now redirect to whatever path your set in the submit handler.