Skip to content

Add setting to hide username on the login checkout pane #677

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 8.x-2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions modules/checkout/config/schema/commerce_checkout.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ commerce_checkout.commerce_checkout_pane.login:
allow_registration:
type: boolean
label: 'Allow registration'
email_only:
type: boolean
label: 'E-Mail only registration'

commerce_checkout_pane_configuration:
type: mapping
Expand Down
20 changes: 17 additions & 3 deletions modules/checkout/src/Plugin/Commerce/CheckoutPane/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public function defaultConfiguration() {
return [
'allow_guest_checkout' => TRUE,
'allow_registration' => FALSE,
'email_only' => FALSE,
] + parent::defaultConfiguration();
}

Expand All @@ -136,6 +137,10 @@ public function buildConfigurationSummary() {
}
}

if (!empty($this->configuration['email_only'])) {
$summary .= '<br>' . $this->t('Use E-mail only for registration and login');
}

return $summary;
}

Expand All @@ -160,6 +165,13 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
],
];

$form['email_only'] = [
'#type' => 'checkbox',
'#title' => $this->t('Use E-Mail only for registration and login'),
'#description' => $this->t('If checked, the name field will not be shown and the e-mail will be used as username.'),
'#default_value' => $this->configuration['email_only'],
];

return $form;
}

Expand All @@ -173,6 +185,7 @@ public function submitConfigurationForm(array &$form, FormStateInterface $form_s
$values = $form_state->getValue($form['#parents']);
$this->configuration['allow_guest_checkout'] = !empty($values['allow_guest_checkout']);
$this->configuration['allow_registration'] = !empty($values['allow_registration']);
$this->configuration['email_only'] = !empty($values['email_only']);
}
}

Expand Down Expand Up @@ -201,7 +214,7 @@ public function buildPaneForm(array $pane_form, FormStateInterface $form_state,
];
$pane_form['returning_customer']['name'] = [
'#type' => 'textfield',
'#title' => $this->t('Username'),
'#title' => $this->configuration['email_only'] ? $this->t('E-Mail') : $this->t('Username'),
'#size' => 60,
'#maxlength' => USERNAME_MAX_LENGTH,
'#attributes' => [
Expand Down Expand Up @@ -270,6 +283,7 @@ public function buildPaneForm(array $pane_form, FormStateInterface $form_state,
'#maxlength' => USERNAME_MAX_LENGTH,
'#description' => $this->t("Several special characters are allowed, including space, period (.), hyphen (-), apostrophe ('), underscore (_), and the @ sign."),
'#required' => FALSE,
'#access' => empty($this->configuration['email_only']),
'#attributes' => [
'class' => ['username'],
'autocorrect' => 'off',
Expand Down Expand Up @@ -340,13 +354,13 @@ public function validatePaneForm(array &$pane_form, FormStateInterface $form_sta

case 'register':
$email = $values['register']['mail'];
$username = $values['register']['name'];
$username = $this->configuration['email_only'] ? $values['register']['mail'] : $values['register']['name'];
$password = trim($values['register']['password']);
if (empty($email)) {
$form_state->setError($pane_form['register']['mail'], $this->t('Email field is required.'));
return;
}
if (empty($username)) {
if (empty($this->configuration['email_only']) && empty($username)) {
$form_state->setError($pane_form['register']['name'], $this->t('Username field is required.'));
return;
}
Expand Down