We can create reset password block by \Drupal::formBuilder()->getForm('Drupal\user\Form\UserPasswordForm') in d8, but change password block \Drupal::formBuilder()->getForm('Drupal\user\Form\UserPasswordResetForm') didn't work for me. This is my code:

public function build(Request $request) {
  $session = $request -> getSession();
  $session->set('pass_reset_hash', $hash);
  $session->set('pass_reset_timeout', $timestamp);
  $user = \Drupal::currentUser();
  $expiration_date = $user->getLastLoginTime();

  $myblock = \Drupal::formBuilder()->getForm('Drupal\user\Form\UserPasswordResetForm::class', $user, $expiration_date, $timestamp, $hash);
  return array(
    '#type' => 'markup',
    '#markup' => drupal_render($myblock),
  • As far as I can see the two lines of code are identical, so it should render the same form – user72672 Jan 21 '19 at 7:24
  • @Jdrupal updated the question – kpda806G Jan 21 '19 at 7:28

First of all try to take a look at the buildForm method in the UserPasswordResetForm class, it takes some parameters.

So you must call getForm like this:

\Drupal::formBuilder()->getForm('Drupal\user\Form\UserPasswordResetForm::class', $user, $expiration_date, $timestamp, $hash);

This is the function docs,

$user = User requesting reset.

$expiration_date = Formatted expiration date for the login link, or NULL if the link does not expire.

$timestamp = The current timestamp.

$hash = Login link hash.

So when those parameters are added the form should render perfectly.

| improve this answer | |
  • it didn't work.. check my updated code. – kpda806G Jan 21 '19 at 11:26
  • Try to do like this in your render array. – user72672 Jan 21 '19 at 11:37
  • return array( 'form' => \Drupal::formBuilder()->getForm('Drupal\user\Form\UserPasswordResetForm::class', $user, $expiration_date, $timestamp, $hash) ); – user72672 Jan 21 '19 at 11:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.