0

I am trying to catch the username and password which are entered on the user login form. This is my code:

function impak_integration_user_login(&$edit, $account) {
  drupal_set_message("Welcome User!" . print_r($edit). print_r($account));
  global $user;
  drupal_set_message(print_r($user));
  }

The hook does work, as It does print out the Welcome message after login. However, I can't figure out how to get any of the user information, specifically the entered username and password.

2

The information send in the login form is in the $edit array. You can get this information as follows:

function YOURMODULE_user_login(&$edit, $account) {
  $username = $edit['input']['name'];
  $password = $edit['input']['pass'];
  drupal_set_message("Welcome $username your password is $password");
}
  • Thanks, that worked! But I dont understand why - my printout from the above message was Welcome User!11 1 - every print_r returned a '1'. Why? – user1015214 Oct 1 '13 at 20:56
  • I'm not sure why print_r would return '1', but I strongly recommend you to use dpm() function which ships with Devel module. You can use it like: dpm($edit) instead of drupal_set_message(print_r($edit)) – Елин Й. Oct 1 '13 at 21:04
  • 1
    @ЕлинЙ. - The print_r function has a second parameter which must be used if you wish to return the output. The proper syntax for the OP would be print_r($account,TRUE) to see the content of the $account object. – Triskelion Oct 1 '13 at 21:46
  • 1
    Very interesting, I didn't know that, thanks! I see the reason why I normally don't need that parameter, I just do a a print. But now, I am doing the print_r inside of the drupal_set_message, so I needed to have print_r actually return the output to drupal_set_message, not actually print it. – user1015214 Oct 2 '13 at 13:12
1

Think twice why you need to do something before login! As one of the possible answer
- In order to catch the user login, you can implement hook_form_alter() as below

/**
* Implements hook_form_alter().
*/
function MYMODULE_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) {
    case 'user_login_block':
    case 'user_login' :
      array_unshift($form['#submit'], 'custom_login_submit');
      break;
  }
}

/**
* Custom submit handler
*/
function custom_login_submit($form, &$form_state) {
  //code here
  dpm($form_state);
}

clearly you will get both things in $formstate

The reason you might not be getting pass in hook_user_login() is because it is called after login.

  • Can you explain your code a little better? I originally was going to do hook_form_alter, following this post drupal.stackexchange.com/questions/41782/…. However, I couldn't get it to work. What exactly is your array_unshift doing and why does it work? – user1015214 Oct 1 '13 at 21:01
  • it makes your submit handler to act before the drupal default, keeping all other handlers intact – arpitr Oct 1 '13 at 21:09
  • Can you give the submit handler any name you want? Does it have to end with 'submit'? – user1015214 Oct 2 '13 at 13:09
  • you can give it any name but better to give which make sense – arpitr Oct 2 '13 at 13:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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