Newer
Older
Dries Buytaert
committed
<?php
/**
* @file
* Contains \Drupal\user\Form\UserPasswordForm.
*/
namespace Drupal\user\Form;
Alex Pott
committed
use Drupal\Core\Field\Plugin\Field\FieldType\EmailItem;
use Drupal\Core\Form\FormBase;
Dries Buytaert
committed
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageManagerInterface;
Angie Byron
committed
use Drupal\Core\Render\Element\Email;
catch
committed
use Drupal\user\UserStorageInterface;
Dries Buytaert
committed
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a user password reset form.
*/
class UserPasswordForm extends FormBase {
Dries Buytaert
committed
/**
catch
committed
* The user storage.
Dries Buytaert
committed
*
catch
committed
* @var \Drupal\user\UserStorageInterface
Dries Buytaert
committed
*/
catch
committed
protected $userStorage;
Dries Buytaert
committed
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
Dries Buytaert
committed
*/
protected $languageManager;
/**
* Constructs a UserPasswordForm object.
*
catch
committed
* @param \Drupal\user\UserStorageInterface $user_storage
* The user storage.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
Dries Buytaert
committed
* The language manager.
*/
public function __construct(UserStorageInterface $user_storage, LanguageManagerInterface $language_manager) {
catch
committed
$this->userStorage = $user_storage;
Dries Buytaert
committed
$this->languageManager = $language_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
catch
committed
$container->get('entity.manager')->getStorage('user'),
Dries Buytaert
committed
$container->get('language_manager')
);
}
/**
* {@inheritdoc}
*/
Alex Pott
committed
public function getFormId() {
Dries Buytaert
committed
return 'user_pass';
}
/**
* {@inheritdoc}
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The request object.
*/
Dries Buytaert
committed
public function buildForm(array $form, FormStateInterface $form_state) {
Dries Buytaert
committed
$form['name'] = array(
'#type' => 'textfield',
'#title' => $this->t('Username or email address'),
Dries Buytaert
committed
'#size' => 60,
Angie Byron
committed
'#maxlength' => max(USERNAME_MAX_LENGTH, Email::EMAIL_MAX_LENGTH),
Dries Buytaert
committed
'#required' => TRUE,
'#attributes' => array(
'autocorrect' => 'off',
'autocapitalize' => 'off',
'spellcheck' => 'false',
'autofocus' => 'autofocus',
),
);
// Allow logged in users to request this also.
$user = $this->currentUser();
Dries Buytaert
committed
if ($user->isAuthenticated()) {
Dries Buytaert
committed
$form['name']['#type'] = 'value';
$form['name']['#value'] = $user->getEmail();
Dries Buytaert
committed
$form['mail'] = array(
'#prefix' => '<p>',
'#markup' => $this->t('Password reset instructions will be mailed to %email. You must log out to use the password reset link in the email.', array('%email' => $user->getEmail())),
Dries Buytaert
committed
'#suffix' => '</p>',
);
}
else {
$form['name']['#default_value'] = $this->getRequest()->query->get('name');
Dries Buytaert
committed
}
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array('#type' => 'submit', '#value' => $this->t('Email new password'));
Dries Buytaert
committed
return $form;
}
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
public function validateForm(array &$form, FormStateInterface $form_state) {
Alex Pott
committed
$name = trim($form_state->getValue('name'));
Dries Buytaert
committed
// Try to load by email.
catch
committed
$users = $this->userStorage->loadByProperties(array('mail' => $name, 'status' => '1'));
Dries Buytaert
committed
if (empty($users)) {
// No success, try to load by name.
catch
committed
$users = $this->userStorage->loadByProperties(array('name' => $name, 'status' => '1'));
Dries Buytaert
committed
}
$account = reset($users);
Dries Buytaert
committed
if ($account && $account->id()) {
Alex Pott
committed
$form_state->setValueForElement(array('#parents' => array('account')), $account);
Dries Buytaert
committed
}
else {
$form_state->setErrorByName('name', $this->t('Sorry, %name is not recognized as a username or an email address.', array('%name' => $name)));
Dries Buytaert
committed
}
}
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
public function submitForm(array &$form, FormStateInterface $form_state) {
$langcode = $this->languageManager->getCurrentLanguage()->getId();
Dries Buytaert
committed
Alex Pott
committed
$account = $form_state->getValue('account');
Dries Buytaert
committed
// Mail one time login URL and instructions using current language.
Alex Pott
committed
$mail = _user_mail_notify('password_reset', $account, $langcode);
Dries Buytaert
committed
if (!empty($mail)) {
$this->logger('user')->notice('Password reset instructions mailed to %name at %email.', array('%name' => $account->getUsername(), '%email' => $account->getEmail()));
drupal_set_message($this->t('Further instructions have been sent to your email address.'));
Dries Buytaert
committed
}
Alex Pott
committed
$form_state->setRedirect('user.page');