This blog describes about how to login using both email and username in Drupal 7. All of you know we could login using only username in Drupal 7.
I've tried to login using email without any contrib modules. Finally i got the code. First alter form to add custom form validation. In custom form validation, get the name from user table by email and set that value into name field in form. Let see the code:
Now you can login using both username and email. I've hope you know how to login using both username and email in Drupal 7.
I've tried to login using email without any contrib modules. Finally i got the code. First alter form to add custom form validation. In custom form validation, get the name from user table by email and set that value into name field in form. Let see the code:
<?php
/**
* Implement hook_form_alter().
*/
function phponwebsites_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == "user_login" || $form_id == "user_login_block") {
$form['name']['#title'] = t('Username or E-mail Address');
// Ensure a valid validate array.
$form['#validate'] = is_array($form['#validate']) ? $form['#validate'] : array();
// login using username or email address
array_unshift($form['#validate'],'phponwebsites_user_login_validate');
}
}
/**
* Implement phponwebsites_user_login_validate()
*
* Return name by its email address
*/
function phponwebsites_user_login_validate($form, &$form_state) {
if (isset($form_state['values']['name']) && strpos($form_state['values']['name'], '@') !== false) {
$name = db_query("SELECT name FROM {users} WHERE LOWER(mail) = LOWER(:name)", array(':name' => $form_state['values']['name']))->fetchField();
}
if (isset($name)) {
form_set_value($form['name'], $name, $form_state);
}
}
/**
* Implement hook_form_alter().
*/
function phponwebsites_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == "user_login" || $form_id == "user_login_block") {
$form['name']['#title'] = t('Username or E-mail Address');
// Ensure a valid validate array.
$form['#validate'] = is_array($form['#validate']) ? $form['#validate'] : array();
// login using username or email address
array_unshift($form['#validate'],'phponwebsites_user_login_validate');
}
}
/**
* Implement phponwebsites_user_login_validate()
*
* Return name by its email address
*/
function phponwebsites_user_login_validate($form, &$form_state) {
if (isset($form_state['values']['name']) && strpos($form_state['values']['name'], '@') !== false) {
$name = db_query("SELECT name FROM {users} WHERE LOWER(mail) = LOWER(:name)", array(':name' => $form_state['values']['name']))->fetchField();
}
if (isset($name)) {
form_set_value($form['name'], $name, $form_state);
}
}
Now you can login using both username and email. I've hope you know how to login using both username and email in Drupal 7.
Related articles:
Add new menu item into already created menu in Drupal 7
Add class into menu item in Drupal 7
Create menu tab programmatically in Drupal 7
Add custom fields to search api index in Drupal 7
Clear views cache when insert, update and delete a node in Drupal 7
Create a page without header and footer in Drupal 7
Redirect users after login in Drupal 7
Add new menu item into already created menu in Drupal 7
Add class into menu item in Drupal 7
Create menu tab programmatically in Drupal 7
Add custom fields to search api index in Drupal 7
Clear views cache when insert, update and delete a node in Drupal 7
Create a page without header and footer in Drupal 7
Redirect users after login in Drupal 7
Or you could use a more standard approach like https://www.drupal.org/project/email_registration or https://www.drupal.org/project/logintoboggan which are both widely used and adopted.
ReplyDeleteAlso, the Super Login module accomplishes this in a lightweight way without the bloat of the logintoboggan module: https://www.drupal.org/project/super_login
ReplyDeleteThe least expensive approach to set up and consistently manufacture an enrollment site is to make a direct mail advertisement.
ReplyDeleteTalkTalk login/
The developer has moved a module that meets your need from the sandbox to the project page:
ReplyDeletehttps://www.drupal.org/project/login_email
The module exists as 7.x-1.x-dev It works as is...looks like a bit of assistance can turn this into an official release.
Email Registration changes the registration process and Login Toboggan is pretty heavy.
Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained! email extractor 6.6.5.5 registration key
ReplyDeleteIt is truly a well-researched content and excellent wording. I got so engaged in this material that I couldn’t wait reading. I am impressed with your work and skill. Thanks. g mass
ReplyDeleteI exactly got what you mean, thanks for posting. And, I am too much happy to find this website on the world of Google. how to send personalized emails in gmail
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteKeeping record of client IP and login time in the information base under the client account. hotmail login
ReplyDeleteVery helpful! The concept of creating sticky brand names was thoroughly explained and indeed a course to take! CBT Email Extractor
ReplyDeletei am always looking for some free stuffs over the internet. there are also some companies which gives free samples. https://officialbopkingdlow.com/
ReplyDeleteSo lot to occur over your amazing blog. Your blog procures me a fantastic transaction of enjoyable.. Salubrious lot beside the scene. Free Email Extractor Software Download
ReplyDeleteKeep up the good work; I read few posts on this website, including I consider that your blog is fascinating and has sets of the fantastic piece of information. Thanks for your valuable efforts. 토토커뮤니티
ReplyDeletenice post, keep up with this interesting work. It really is good to know that this topic is being covered also on this web site so cheers for taking time to discuss this! 안전놀이터
ReplyDeleteThis is my first time i visit here. I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here keep up the good work Zendable
ReplyDeletePositive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. 대전1인샵
ReplyDeleteAn interesting dialogue is price comment. I feel that it is best to write more on this matter, it may not be a taboo topic however usually individuals are not enough to talk on such topics. To the next. Cheers. 먹튀검증
ReplyDeleteAll things considered, anything too obscure will quickly get shipped off spam or be erased by the email client. Same goes for anything excessively immediate (like an undeniable attempt to sell something) or something that suggests that the mailing is essential for a major email impact. gmx net
ReplyDeleteUnbelievable!! The problem I was thinking about was solved. 슬롯커뮤니티
ReplyDeleteYou are really awesome.
Outlook 2010 has several latest features offering real benefits for you. buy mass email accounts
ReplyDelete