You are here

public function UserPasswordForm::buildForm in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/user/src/Form/UserPasswordForm.php \Drupal\user\Form\UserPasswordForm::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

core/modules/user/src/Form/UserPasswordForm.php, line 119

Class

UserPasswordForm
Provides a user password reset form.

Namespace

Drupal\user\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form['name'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Username or email address'),
    '#size' => 60,
    '#maxlength' => max(UserInterface::USERNAME_MAX_LENGTH, Email::EMAIL_MAX_LENGTH),
    '#required' => TRUE,
    '#attributes' => [
      'autocorrect' => 'off',
      'autocapitalize' => 'off',
      'spellcheck' => 'false',
      'autofocus' => 'autofocus',
    ],
  ];

  // Allow logged in users to request this also.
  $user = $this
    ->currentUser();
  if ($user
    ->isAuthenticated()) {
    $form['name']['#type'] = 'value';
    $form['name']['#value'] = $user
      ->getEmail();
    $form['mail'] = [
      '#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.', [
        '%email' => $user
          ->getEmail(),
      ]),
      '#suffix' => '</p>',
    ];
  }
  else {
    $form['mail'] = [
      '#prefix' => '<p>',
      '#markup' => $this
        ->t('Password reset instructions will be sent to your registered email address.'),
      '#suffix' => '</p>',
    ];
    $form['name']['#default_value'] = $this
      ->getRequest()->query
      ->get('name');
  }
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Submit'),
  ];
  $form['#cache']['contexts'][] = 'url.query_args';
  return $form;
}