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?

up vote 3 down vote accepted

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.

  • 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. – kiamlaluno Sep 4 '12 at 12:37
  • Perfect! Thanks for the help. – Web Assistant Sep 4 '12 at 13:09

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
    );
  • 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. – Web Assistant Sep 4 '12 at 11:57
  • This plain wrong: To add a submission handler to an existing submission button, you don't add a new submission button. – kiamlaluno Sep 4 '12 at 14:26

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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