using hook_form_alter() to modify $form_state['redirect']

Blackguard - January 27, 2009 - 22:11

I'd like to overwrite the final destination after a form is submitted in the ecard module

I was hopeful in trying to do this very simple thing:

<?php
function formalter_ecard_form_form_alter(&$form, &$form_state)
{
 
$form_state['redirect'] = 'process-your-donation';
}
?>

But I'm obviously sadly mistaken :(

What's the real way to change $form_state['redirect']?

hook_submit

incrn8 - January 28, 2009 - 00:30

I modified the redirect in a new hook_submit function. It looks just like your form_alter function, except that "form_alter" gets changed to "submit".
______________________________________________________________________________________________________
mybesinformatik.com - Drupal website development

hook_submit expects a node object...

Blackguard - January 28, 2009 - 14:51

I tried it, and it did not do the trick. I look at the API and as far as I can tell hook_submit acts on nodes when they are submitted, not forms.

hook_form_alter doesn't take

jefflane - February 24, 2009 - 00:13

hook_form_alter doesn't take form_state by reference, so you can't change it like that

according to http://api.drupal.org/api/function/hook_form_alter

<?php
hook_form_alter
(&$form, $form_state, $form_id)
?>
is the correct prototype

That being said, I also want to change $form_state from hook_form_alter, I'll let you know if I find out how to do it.

This should work. Although,

jefflane - February 24, 2009 - 00:43

This should work. Although, you may want to avoid using hook_form_alter if you have the ability to affect the submit callback. Hope it works for you.

<?php
function jeff_form_alter(&$form, $form_state, $form_id)
{
    
$form['#submit'][] = 'jeff_form_submit'
}

function
jeff_form_submit($form, &$form_state)
{
    
$form_state['redirect'] = 'process-your-donation';
}
?>

Thanks, I'll try this out

Blackguard - February 28, 2009 - 17:19

Thanks, I'll try this out next time. I'm not sure I understand the comment about not using hook_form_alter() but I'll look into it. I suppose you mean if I should simply want to edit the callback function directly instead of assigning a new one with the hook, depending if this is my own module or if I'm hooking into someone else's.

 
 

Drupal is a registered trademark of Dries Buytaert.