The [http://drupal.org/project/email_registration](email registration module) is
great for making sure users can only log in with their email address and don't
need to create an account name.
This doesn't work for logging in in the checkout though. To do that, we copy the
form_alter from the email_registration.module to a custom module (in this case: `custom_checkout`).
```
/**
* Implements hook_form_alter().
*/
function custom_checkout_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
if (isset($form['login'])) {
$form['login']['returning_customer']['name']['#type'] = 'email';
$form['login']['returning_customer']['name']['#maxlength'] = Email::EMAIL_MAX_LENGTH;
$form['login']['returning_customer']['name']['#title'] = t('Email address');
$form['login']['returning_customer']['pass']['#description'] = t('Enter the password that accompanies your e-mail.');
$form['login']['returning_customer']['name']['#description'] = t('Enter your e-mail address.');
$form['login']['returning_customer']['name']['#element_validate'][] = 'custom_checkout_email_registration_user_login_validate';
}
}
```
There's a custom `#element_validate` defined in there, that's needed because by
default the username gets stored in 'name', and that is not readable by the pane
form.
We need to set the 'name' in the correct part of the form structure.
```
/**
* Form element validation handler for the user login form.
*
* Allows users to authenticate by email, which is our preferred method.
*/
function custom_checkout_email_registration_user_login_validate($form, FormStateInterface $form_state) {
$mail = $form_state->getValues()['login']['returning_customer']['name'];
if (!empty($mail) && ($user = user_load_by_mail($mail))) {
// Keep the email value in form state for further validation.
$form_state->setValue(['login', 'returning_customer', 'name'], $user->getAccountName());
}
}
```
---
As requested on irc. Hope that's enough. The code works on my machine, I only changed the function names, as they were namespaced to our customer project ;)
Comments
Comment #2
bojanz commentedWe'll also need the register part before committing this to docs.
Comment #3
adrian83 commentedOh, I wish email login would be the Drupal standard! Would I create a module for this, or add it to my theme?
Comment #4
sevenfish commentedAdd on header
Comment #5
karlsheaThis is what I've got for both login and registration during the checkout flow:
Comment #6
perfectcu.be commentedThanks for the hint KarlShea @#5; works like a champ!
Comment #7
socialnicheguru commentedIs this a dupe of Commerce 2 Completion Registration checkout pane? If not please reopen
Comment #8
socialnicheguru commented