I can alter every form submit in Drupal with this hook:


On the user_pass_reset form there is a button for auto-login from a link.

I need to add some custom logic just after the user click on the button "Login".

Here's how I tried to do that :

function tools_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id){

   if($form_id == 'user_pass_reset')
        $form['actions']['submit']['#submit'][] = 'tools_compte_mot_de_passe_reset_submit';
        $form['#submit'][] = 'tools_compte_mot_de_passe_reset_submit';

I have set the 2 form methods because I trying to understand which is called.

My custom submit :

function tools_compte_mot_de_passe_reset_submit($form, \Drupal\Core\Form\FormStateInterface &$form_state)

My custom submit is never called... if I try on a different form, that works, but not on this one.

Where i am wrong ?


From \Drupal\user\Form\UserPasswordResetForm::buildForm:

$form['#action'] = Url::fromRoute('user.reset.login', [
  'uid' => $user->id(),
  'timestamp' => $timestamp,
  'hash' => $hash,

If you look at the form in your browser, you can see that it directly POSTS to that other route. as a result of that, your submit isn't called because for that, it would need to actually submit to itself again.

That then points to \Drupal\user\Controller\UserController::resetPassLogin(), which calls user_login_finalize(), which calls user_login().

Depending on what you want to do on submit, you can either do in hook_user_login() or by altering that route.

  • Thank Berdir, i want call a method in the service of the force reset password module – Kevin Sep 14 '18 at 4:24
  • \Drupal::service('force_password_change.service')->forceUserPasswordChange(\Drupal::currentUser()->id()); – Kevin Sep 14 '18 at 7:35
  • So i need to create a custom controller for alter this route with a paste code from \Drupal\user\Controller\UserController::resetPassLogin() for keep the validation do inside. But if the core change, i need to update the code... not really the best way... – Kevin Sep 14 '18 at 8:03
  • 1
    The same would be true if you want to replace a submit callback completely, that's really not a difference. But you don't. You just want to do one additional, as I mentioned, you can probably do what you want directly in hook_user_login(), combined with a check for the current route? – Berdir Sep 14 '18 at 8:41
  • 1
    And if that somehow wouldn't work, you could extend the user controller, call the parent method and do something before or after that. You only need to replace if something it does must not happen or happen differently. – Berdir Sep 14 '18 at 8:42

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.