From f856c0c63fb10a76940216f3fc791266abc7f44a Mon Sep 17 00:00:00 2001 From: Sascha Grossenbacher Date: Thu, 16 Mar 2017 14:55:50 +0100 Subject: [PATCH] Add setting to hide username on the login checkout pane --- .../schema/commerce_checkout.schema.yml | 3 +++ .../Plugin/Commerce/CheckoutPane/Login.php | 20 ++++++++++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/modules/checkout/config/schema/commerce_checkout.schema.yml b/modules/checkout/config/schema/commerce_checkout.schema.yml index 65bbe4b645..bdadcb1c09 100644 --- a/modules/checkout/config/schema/commerce_checkout.schema.yml +++ b/modules/checkout/config/schema/commerce_checkout.schema.yml @@ -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 diff --git a/modules/checkout/src/Plugin/Commerce/CheckoutPane/Login.php b/modules/checkout/src/Plugin/Commerce/CheckoutPane/Login.php index 63675fac10..ff9c840f1c 100644 --- a/modules/checkout/src/Plugin/Commerce/CheckoutPane/Login.php +++ b/modules/checkout/src/Plugin/Commerce/CheckoutPane/Login.php @@ -116,6 +116,7 @@ public function defaultConfiguration() { return [ 'allow_guest_checkout' => TRUE, 'allow_registration' => FALSE, + 'email_only' => FALSE, ] + parent::defaultConfiguration(); } @@ -136,6 +137,10 @@ public function buildConfigurationSummary() { } } + if (!empty($this->configuration['email_only'])) { + $summary .= '
' . $this->t('Use E-mail only for registration and login'); + } + return $summary; } @@ -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; } @@ -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']); } } @@ -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' => [ @@ -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', @@ -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; }