By luyendao on
Apologies in advance, still learning the Drupal ropes and best practices.
I'm doing something very simple, and yet have spent hours on figuring this out. I'm using _form_alter() API function to modify the user registration page, i want it to redirect to another page after it's done. I need to use a submit handler because i need to process a checkbox element i've added in the form.
There's not much code here, and yet the submit handler function is not being called, how do I check to see what's going on?
Thanks for any insights -
function mods_form_alter(&$form, &$form_state, $form_id) {
if($form_id=="user_register") {
$form['buttons']['submit']['#submit'][] = 'mods_validate_register';
}
}
function mods_validate_register($form, &$form_state) {
$form_state['#redirect'] = 'node/add/group';
}
Comments
A few things
A few things look wrong here.
First, according to the API page for the user_register form, the submit button is not nested within a 'buttons' element. So it should just be
$form['submit']['#submit'][] = 'mods_validate_register';. Also, it's a little weird that you're attaching a submit handler, but the function name is mods_validate_register()... why not mods_submit_register()..?Second, you want to set
$form_state['redirect'](no #-sign). The #-sign syntax can be used directly on the $form in the form_alter() though, like$form['#redirect'] = 'node/add/group';.Third, adding elements to the user_register form (ie the checkbox you mentioned) is usually better-suited in hook_user() on the "register" $op. That way, the value of your checkbox (or anything else added to the form) will be automatically saved to the account.
Lastly, an invaluable resource for debugging is the devel module. With it enabled, you can just do
dpm($form);ordpm($form_state);and you'll be able to see a pretty-printed version of the data structure.Hope that helps. :)
Hi Roper, thanks for the very
Hi Roper, thanks for the very quick reply.
Very very helpful notes, you've helped a lot. Regarding the submit handler, the only reason I used that is because I came across various articles saying that you need to use that and call another function in order to override, because it's called at the end, but to be honest i was trying to get something working.
Once again thanks for the tips, how do you know when it's best to use a certain function, just experience or is there some best practices guides?
Hi Roper, I got it 99%
Hi Roper,
I got it 99% working, the callback is working on the submit handler but the redirection doesn't happen, when i add print_r($form_state) to the callback function, i notice that $form_state['redirect'] is empty. So when I submit the form it takes me back to the default homepage.
Not sure what i'm doing wrong here? ;-) I'm guessing it might not be in the correct position in the array?
p.s. for some reason dpm($form_state) doens't output anything, only print_r() does.
You need to
You need to
print_r($form_state)after you add to it, of course it's empty before you've added it. :-PAs for why it's not working, I'm not sure. Are you using the LoginToboggan module? I think that provides a way to set the registration redirect, maybe it's overriding yours. Otherwise I don't know why it's not working... :-\
Thanks for your help,
Thanks for your help, appreciate your time.
I came across someone's post with the similar "challenge", and in the callback they ended up using:
that worked, but i don't know if there are any caveats. Anyhow i'm stuck somewhere else because it looks like the callback happens too early, so the user hasn't been saved yet and therefore is not authorized to go to a node creation page.
I'll take another look at the Rules engine, probably a better path than what i'm trying to do maybe.