3 namespace Drupal\user\Form;
5 use Drupal\Core\Form\FormBase;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Language\LanguageManagerInterface;
8 use Drupal\Core\Render\Element\Email;
9 use Drupal\user\UserStorageInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
13 * Provides a user password reset form.
17 class UserPasswordForm extends FormBase {
22 * @var \Drupal\user\UserStorageInterface
24 protected $userStorage;
27 * The language manager.
29 * @var \Drupal\Core\Language\LanguageManagerInterface
31 protected $languageManager;
34 * Constructs a UserPasswordForm object.
36 * @param \Drupal\user\UserStorageInterface $user_storage
38 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
39 * The language manager.
41 public function __construct(UserStorageInterface $user_storage, LanguageManagerInterface $language_manager) {
42 $this->userStorage = $user_storage;
43 $this->languageManager = $language_manager;
49 public static function create(ContainerInterface $container) {
51 $container->get('entity.manager')->getStorage('user'),
52 $container->get('language_manager')
59 public function getFormId() {
66 public function buildForm(array $form, FormStateInterface $form_state) {
68 '#type' => 'textfield',
69 '#title' => $this->t('Username or email address'),
71 '#maxlength' => max(USERNAME_MAX_LENGTH, Email::EMAIL_MAX_LENGTH),
74 'autocorrect' => 'off',
75 'autocapitalize' => 'off',
76 'spellcheck' => 'false',
77 'autofocus' => 'autofocus',
80 // Allow logged in users to request this also.
81 $user = $this->currentUser();
82 if ($user->isAuthenticated()) {
83 $form['name']['#type'] = 'value';
84 $form['name']['#value'] = $user->getEmail();
87 '#markup' => $this->t('Password reset instructions will be mailed to %email. You must log out to use the password reset link in the email.', ['%email' => $user->getEmail()]),
94 '#markup' => $this->t('Password reset instructions will be sent to your registered email address.'),
97 $form['name']['#default_value'] = $this->getRequest()->query->get('name');
99 $form['actions'] = ['#type' => 'actions'];
100 $form['actions']['submit'] = ['#type' => 'submit', '#value' => $this->t('Submit')];
101 $form['#cache']['contexts'][] = 'url.query_args';
109 public function validateForm(array &$form, FormStateInterface $form_state) {
110 $name = trim($form_state->getValue('name'));
111 // Try to load by email.
112 $users = $this->userStorage->loadByProperties(['mail' => $name]);
114 // No success, try to load by name.
115 $users = $this->userStorage->loadByProperties(['name' => $name]);
117 $account = reset($users);
118 if ($account && $account->id()) {
119 // Blocked accounts cannot request a new password.
120 if (!$account->isActive()) {
121 $form_state->setErrorByName('name', $this->t('%name is blocked or has not been activated yet.', ['%name' => $name]));
124 $form_state->setValueForElement(['#parents' => ['account']], $account);
128 $form_state->setErrorByName('name', $this->t('%name is not recognized as a username or an email address.', ['%name' => $name]));
135 public function submitForm(array &$form, FormStateInterface $form_state) {
136 $langcode = $this->languageManager->getCurrentLanguage()->getId();
138 $account = $form_state->getValue('account');
139 // Mail one time login URL and instructions using current language.
140 $mail = _user_mail_notify('password_reset', $account, $langcode);
142 $this->logger('user')->notice('Password reset instructions mailed to %name at %email.', ['%name' => $account->getUsername(), '%email' => $account->getEmail()]);
143 $this->messenger()->addStatus($this->t('Further instructions have been sent to your email address.'));
146 $form_state->setRedirect('user.page');