For the life of me, I can't figure out how to get form redirects to work. It's just ignoring what I put in for the $form_state['redirect'] value.
I have a custom "registration" content type, where, depending if the customer wants to pay by credit card or cheque, I redirect them to 2 different pages.
I thought this would be simple and straight forward. Not so!
I've been following the documentation at:
http://api.drupal.org/api/file/developer/topics/forms_api.html
..near the bottom where it says:
To determine where the user should be sent after the form is processed, the _submit function can place a path or URL in $form_state['redirect'] which will be the target of a drupal_goto; every form is redirected after a submit.
The Code:
function re_form_alter(&$form, &$form_state) {
if ($form['type']['#value'] == 'registration') {
$form['#submit'] = array('registration_submit');
}
//$form['#redirect'] = "http://www.google.com"; // NOTE: THIS WILL WORK IF UNCOMMENTED
}
function registration_submit($form, &$form_state) {
// I know this function is being reached, but the redirect lines effectively do nothing (regardless of what URL I try)
if ($form_state['values']['field_payment_type'][0]['value'] == 'cheque')
$form_state['redirect'] = $GLOBALS['drupal_url'] . '/sites/all/modules/re/registration_process.php?cheque=1';
else
$form_state['redirect'] = $GLOBALS['drupal_url'] . '/sites/all/modules/re/registration_process.php?credit=1'';
}
The behaviour:
Instead of redirecting to whatever I put in $form_state['redirect'], it just goes to the newly created node page.
Anyone have any ideas why it's ignoring this? Is it because I didn't create the form programmatically, and instead, I'm hooking in through _form_alter?
One other question: I'd really like to include the newly created node ID (nid) in the URL that's being redirected to. I'd probably have to do this with a different function than _submit though correct?
Thanks - any help would really be appreciated as I've been stuck on this for many hours now!
Comments
Anyone?
Anyone know if this is a bug or am I missing something here...
I can't get it to work either
Here's the two part form: http://d6.prometheuslabor.com/my_module/form
Here's the code: http://pastebin.com/m4b5fc9f9
Linest 165 to 169 are the important part
line 166 has $form_state['redirect'] = 'node/19'; which should redirect the form to node/19
--
Prometheus
subscribing
I'm in the same situation igorific is in, I have my form alter hook that adds a submit function to the ones already there (it is a node form as well). In my case i'm working with translations, and i want that after creating a translation it doesn't redirect me to the newly created node, but to the node that is the translation source, for instance if i have a node in english and want to translate it, i want to create the translation and after submiting it go to the original english version of the node. anyway, i add a submit function to $form['#submit'] array, and set $form_state['redirect'] to send me to the original node. but it doesn't work!! please some help
I found something here
here: http://drupal.org/node/264625#comment-864572
I managed to make it work as it was suggested there. hope it helps.