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.
15 class UserPasswordForm extends FormBase {
20 * @var \Drupal\user\UserStorageInterface
22 protected $userStorage;
25 * The language manager.
27 * @var \Drupal\Core\Language\LanguageManagerInterface
29 protected $languageManager;
32 * Constructs a UserPasswordForm object.
34 * @param \Drupal\user\UserStorageInterface $user_storage
36 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
37 * The language manager.
39 public function __construct(UserStorageInterface $user_storage, LanguageManagerInterface $language_manager) {
40 $this->userStorage = $user_storage;
41 $this->languageManager = $language_manager;
47 public static function create(ContainerInterface $container) {
49 $container->get('entity.manager')->getStorage('user'),
50 $container->get('language_manager')
57 public function getFormId() {
64 * @param \Symfony\Component\HttpFoundation\Request $request
67 public function buildForm(array $form, FormStateInterface $form_state) {
69 '#type' => 'textfield',
70 '#title' => $this->t('Username or email address'),
72 '#maxlength' => max(USERNAME_MAX_LENGTH, Email::EMAIL_MAX_LENGTH),
75 'autocorrect' => 'off',
76 'autocapitalize' => 'off',
77 'spellcheck' => 'false',
78 'autofocus' => 'autofocus',
81 // Allow logged in users to request this also.
82 $user = $this->currentUser();
83 if ($user->isAuthenticated()) {
84 $form['name']['#type'] = 'value';
85 $form['name']['#value'] = $user->getEmail();
88 '#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()]),
95 '#markup' => $this->t('Password reset instructions will be sent to your registered email address.'),
98 $form['name']['#default_value'] = $this->getRequest()->query->get('name');
100 $form['actions'] = ['#type' => 'actions'];
101 $form['actions']['submit'] = ['#type' => 'submit', '#value' => $this->t('Submit')];
102 $form['#cache']['contexts'][] = 'url.query_args';
110 public function validateForm(array &$form, FormStateInterface $form_state) {
111 $name = trim($form_state->getValue('name'));
112 // Try to load by email.
113 $users = $this->userStorage->loadByProperties(['mail' => $name]);
115 // No success, try to load by name.
116 $users = $this->userStorage->loadByProperties(['name' => $name]);
118 $account = reset($users);
119 if ($account && $account->id()) {
120 // Blocked accounts cannot request a new password.
121 if (!$account->isActive()) {
122 $form_state->setErrorByName('name', $this->t('%name is blocked or has not been activated yet.', ['%name' => $name]));
125 $form_state->setValueForElement(['#parents' => ['account']], $account);
129 $form_state->setErrorByName('name', $this->t('%name is not recognized as a username or an email address.', ['%name' => $name]));
136 public function submitForm(array &$form, FormStateInterface $form_state) {
137 $langcode = $this->languageManager->getCurrentLanguage()->getId();
139 $account = $form_state->getValue('account');
140 // Mail one time login URL and instructions using current language.
141 $mail = _user_mail_notify('password_reset', $account, $langcode);
143 $this->logger('user')->notice('Password reset instructions mailed to %name at %email.', ['%name' => $account->getUsername(), '%email' => $account->getEmail()]);
144 drupal_set_message($this->t('Further instructions have been sent to your email address.'));
147 $form_state->setRedirect('user.page');