diff --git a/core/modules/user/src/AccountForm.php b/core/modules/user/src/AccountForm.php index 053a5ef..8c397d5 100644 --- a/core/modules/user/src/AccountForm.php +++ b/core/modules/user/src/AccountForm.php @@ -412,7 +412,6 @@ public function submitForm(array &$form, FormStateInterface $form_state) { * {@inheritdoc} */ public function validateForm(array &$form, FormStateInterface $form_state) { - $mail_taken = FALSE; $account = $this->entity; $name = $form_state->getValue('name'); $mail = $form_state->getValue('mail'); @@ -422,9 +421,11 @@ public function validateForm(array &$form, FormStateInterface $form_state) { if (!empty($name)) { $name_taken = FALSE; + // For existing users whose username matches another user's email address + // are not forced to update their username. if ($account->isAuthenticated()) { $name_taken = (bool) db_select('users_field_data', 'ufd') - ->condition('ufd.name', db_like($form_state->getValue('name')), 'LIKE') + ->condition('ufd.name', db_like($name), 'LIKE') ->condition('ufd.uid', $account->id(), '<>') ->range(0, 1) ->countQuery() @@ -435,8 +436,8 @@ public function validateForm(array &$form, FormStateInterface $form_state) { $name_taken = (bool) db_select('users_field_data', 'ufd') ->condition( db_or() - ->condition('ufd.name', db_like($form_state->getValue('name')), 'LIKE') - ->condition('ufd.mail', db_like($form_state->getValue('name')), 'LIKE') + ->condition('ufd.name', db_like($name), 'LIKE') + ->condition('ufd.mail', db_like($name), 'LIKE') ) ->condition('ufd.status', 1) ->range(0, 1) @@ -446,16 +447,20 @@ public function validateForm(array &$form, FormStateInterface $form_state) { } if ($name_taken) { - $form_state->setErrorByName('name', $this->t('The name @name is already taken.', array('@name' => $form_state->getValue('name')))); + $form_state->setErrorByName('name', $this->t('The name @name is already taken.', array('@name' => $name))); } } // For new registrations, make sure the email address does not conflict // with an existing user's username. if (!empty($mail)) { + $mail_taken = FALSE; + + // For existing users whose email matches another user's username are not + // forced to update their email address. if ($account->isAuthenticated()) { $mail_taken = (bool) db_select('users_field_data', 'ufd') - ->condition('ufd.mail', db_like($form_state->getValue('mail')), 'LIKE') + ->condition('ufd.mail', db_like($mail), 'LIKE') ->condition('ufd.uid', $account->id(), '<>') ->range(0, 1) ->countQuery() @@ -466,8 +471,8 @@ public function validateForm(array &$form, FormStateInterface $form_state) { $mail_taken = (bool) db_select('users_field_data', 'ufd') ->condition( db_or() - ->condition('ufd.mail', db_like($form_state->getValue('mail')), 'LIKE') - ->condition('ufd.name', db_like($form_state->getValue('mail')), 'LIKE') + ->condition('ufd.mail', db_like($mail), 'LIKE') + ->condition('ufd.name', db_like($mail), 'LIKE') ) ->condition('ufd.status', 1) ->range(0, 1) @@ -477,7 +482,7 @@ public function validateForm(array &$form, FormStateInterface $form_state) { } if ($mail_taken) { - $form_state->setErrorByName('mail', $this->t('The email address @email is already registered.', array('@email' => $form_state->getValue('mail')))); + $form_state->setErrorByName('mail', $this->t('The email address @email is already registered.', array('@email' => $mail))); } } } diff --git a/core/modules/user/src/Form/UserPasswordForm.php b/core/modules/user/src/Form/UserPasswordForm.php index 8d1f62f..370b54b 100644 --- a/core/modules/user/src/Form/UserPasswordForm.php +++ b/core/modules/user/src/Form/UserPasswordForm.php @@ -238,7 +238,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) { // Mail one-time login URL and instructions using current language. $mail = _user_mail_notify('password_reset', $account, $language_interface->getId()); if (!empty($mail)) { - \Drupal::logger('user')->notice('Password reset instructions mailed to %name at %email.', array('%name' => $account->name, '%email' => $account->mail)); + $this->logger('user')->notice('Password reset instructions mailed to %name at %email.', array('%name' => $account->name, '%email' => $account->mail)); drupal_set_message($this->t('Further instructions have been sent to your e-mail address.')); }