Created
August 17, 2011 01:52
-
-
Save SeanJA/1150629 to your computer and use it in GitHub Desktop.
Allow drupal users to login with their email address as well
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| //"stolen" and modified from code in login toboggan | |
| /** | |
| * Capture the form stubmit and check the form_id if it matches a login form, then | |
| * it will check it as an email using an extra validation step | |
| * @param array $form The current form (I guess...) | |
| * @param array $form_state The state of the current form (I guess...) | |
| * @param string $form_id The unique identifier for this form | |
| * @see hook_form_alter | |
| */ | |
| function my_module_form_alter(&$form, $form_state, $form_id) { | |
| switch ($form_id) { | |
| //there are two normal login forms | |
| case 'user_login': | |
| case 'user_login_block': | |
| // Ensure a valid validate array. | |
| $form['#validate'] = is_array($form['#validate']) ? $form['#validate'] : array(); | |
| // your validation function must run first. | |
| array_unshift($form['#validate'],'my_module_user_login_validate'); | |
| break; | |
| //ignore other stuff | |
| default: | |
| return; | |
| break; | |
| } | |
| } | |
| /** | |
| * Capture the username entered, if there is an @ symbol, it will check it against email addresses instead of the username | |
| * @param array $form The current form (I guess...) | |
| * @param array $form_state The state of the form (I guess...) | |
| */ | |
| function my_module_user_login_validate($form, &$form_state) { | |
| if (isset($form_state['values']['name']) && strpos($form_state['values']['name'], '@') !== false) { | |
| if ($name = db_result(db_query("SELECT name FROM {users} WHERE LOWER(mail) = LOWER('%s')", $form_state['values']['name']))) { | |
| form_set_value($form['name'], $name, $form_state); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment