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 public function buildForm(array $form, FormStateInterface $form_state) {
66 '#type' => 'textfield',
67 '#title' => $this->t('Username or email address'),
69 '#maxlength' => max(USERNAME_MAX_LENGTH, Email::EMAIL_MAX_LENGTH),
72 'autocorrect' => 'off',
73 'autocapitalize' => 'off',
74 'spellcheck' => 'false',
75 'autofocus' => 'autofocus',
78 // Allow logged in users to request this also.
79 $user = $this->currentUser();
80 if ($user->isAuthenticated()) {
81 $form['name']['#type'] = 'value';
82 $form['name']['#value'] = $user->getEmail();
85 '#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()]),
92 '#markup' => $this->t('Password reset instructions will be sent to your registered email address.'),
95 $form['name']['#default_value'] = $this->getRequest()->query->get('name');
97 $form['actions'] = ['#type' => 'actions'];
98 $form['actions']['submit'] = ['#type' => 'submit', '#value' => $this->t('Submit')];
99 $form['#cache']['contexts'][] = 'url.query_args';
107 public function validateForm(array &$form, FormStateInterface $form_state) {
108 $name = trim($form_state->getValue('name'));
109 // Try to load by email.
110 $users = $this->userStorage->loadByProperties(['mail' => $name]);
112 // No success, try to load by name.
113 $users = $this->userStorage->loadByProperties(['name' => $name]);
115 $account = reset($users);
116 if ($account && $account->id()) {
117 // Blocked accounts cannot request a new password.
118 if (!$account->isActive()) {
119 $form_state->setErrorByName('name', $this->t('%name is blocked or has not been activated yet.', ['%name' => $name]));
122 $form_state->setValueForElement(['#parents' => ['account']], $account);
126 $form_state->setErrorByName('name', $this->t('%name is not recognized as a username or an email address.', ['%name' => $name]));
133 public function submitForm(array &$form, FormStateInterface $form_state) {
134 $langcode = $this->languageManager->getCurrentLanguage()->getId();
136 $account = $form_state->getValue('account');
137 // Mail one time login URL and instructions using current language.
138 $mail = _user_mail_notify('password_reset', $account, $langcode);
140 $this->logger('user')->notice('Password reset instructions mailed to %name at %email.', ['%name' => $account->getUsername(), '%email' => $account->getEmail()]);
141 drupal_set_message($this->t('Further instructions have been sent to your email address.'));
144 $form_state->setRedirect('user.page');