function UserPasswordForm::buildForm

Same name in other branches
  1. 9 core/modules/user/src/Form/UserPasswordForm.php \Drupal\user\Form\UserPasswordForm::buildForm()
  2. 8.9.x core/modules/user/src/Form/UserPasswordForm.php \Drupal\user\Form\UserPasswordForm::buildForm()
  3. 11.x core/modules/user/src/Form/UserPasswordForm.php \Drupal\user\Form\UserPasswordForm::buildForm()

Overrides FormInterface::buildForm

File

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

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',
            'autocomplete' => 'username',
        ],
    ];
    // 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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.