2

I am using hook_form_alter to add validation and submit handlers to the registration form:

<?php
function MYMODULE_form_alter(&$form, &$form_state, $form_id) { 
  if ($form_id == 'user_register_form') {
    $form['#validate'][] = 'blah_register_validate';
    $form['#submit'][] = 'blah_register_submit';
  }
}
?>

They were both working fine until I added the following hook:

<?php
function MYMODULE_user_login(&$edit, $account) { 
  drupal_goto('user');  
}
?>

The validation function still works but the submit function gets bypassed completely - it doesn't get called at all by form_execute_handlers() in form.inc. As soon as I remove hook_user_login, it works again. Anyone have an idea why?

Update:

hook_user_login() gets called by user_login_finalize() in the User Module. Unfortunately, this happens before my submit function gets called - so obviously when I use drupal_goto() the submit function will never get called. Can anyone confirm if I am adding the submit handler correctly, is there another way?

2 Answers 2

3

If you check out drupal_goto() you can see it calls drupal_exit() which is not something you want to do in a hook. The comments for hook_user_login() discuss this a little, finishing with this snippet:

function mymodule_user_login(&$edit, $account)
{
  // Your logic will set $redirection to the desired location
  $redirection = 'node/394';

  // Unless there is already a redirection going, or the user is trying to reset his password, we redirect to $redirection.
  if (empty($_GET['destination'])
    && !is_null($redirection)
    && (!isset($_POST['form_id']) || $_POST['form_id'] != 'user_pass_reset'))
  {
    $_GET['destination'] = $redirection; // Should we use $edit['redirect'] instead..?
  }
}

Also mentioned is the login destination module.

1
  • This is the right answer: As many other hooks, hook_user_login() is not supposed to call drupal_goto(), which would stop other hooks (and other form handlers) from being executed.
    – apaderno
    Sep 4, 2012 at 12:37
1

read about drupal_get_destination function. Used to direct the user back to the referring page after completing a form.

You should try something like this(If I understand the question) : drupal_goto('user', array('query'=>drupal_get_destination()) );

If you trying to add a new Submit handler to an existing user_login_form you should try this:

 $form['new_submit'] = array(
        '#type' => 'submit',
        '#value' => 'Submit',
        '#submit' => array('blah_register_submit'),//name of your submit function
    );
2
  • Ah sorry, maybe I didn't explain my problem very clearly. The problem is that my submit function is being called 'after' hook_user_login. So if I add drupal_goto, the page redirects and the submit function doesn't get called at all. So nothing to do with the destination GET variable. Sep 4, 2012 at 11:57
  • This plain wrong: To add a submission handler to an existing submission button, you don't add a new submission button.
    – apaderno
    Sep 4, 2012 at 14:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.