1

When a logged in user resets their password at /user/password and clicks the submit button, they are being redirected to the frontpage.

How can I redirect to /user instead?

I've tried the second answer here: How to redirect users after password reset from first time user, but the redirect doesnt happen.

function myModule_form_user_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  if ($form_state->get('user_pass_reset')) {
    $form['actions']['submit']['#submit'][] = 'mymodule_form_submit';
  }
}

function mymodule_form_submit(array $form, FormStateInterface $form_state){
  $form_state->setRedirect('user.page');
}

For the route name I've tried both entity.user.canonical and user.page. I'm not sure if the code just isn't correct for Drupal 9 or I'm getting the route wrong, but I would expect an error in that case instead of the redirect just not happening.

6
  • You might want to check if you have modules that are doing that redirect to the front page. Some modules register event subscribers to request/response events and may override some form redirection.
    – Joseph
    Feb 1, 2023 at 18:26
  • You'll need to show us the code you've tried, to be able to provide suggestions as to why it may not have worked. At the moment all we can really say is that you must have done something that didn't work right, which of course isn't helpful at all.
    – Jaypan
    Feb 1, 2023 at 19:40
  • I've updated the post with the code tried. Feb 1, 2023 at 20:09
  • The route should be entity.user.canonical. Other than that, the code looks good. Are you sure the submit handler is being called?
    – Jaypan
    Feb 1, 2023 at 20:25
  • @Jaypan user.page is a fine alternative.
    – No Sssweat
    Feb 2, 2023 at 2:16

1 Answer 1

2

Looks like the form ID changed, its user_pass for D9

use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_form_FORM_ID_alter().
 */
function mymodule_form_user_pass_alter(&$form, FormStateInterface $form_state, $form_id) {
  $form['actions']['submit']['#submit'][] = 'mymodule_form_submit';
}

/**
 * Custom submit.
 *
 * Redirects user to the user page.
 */
function mymodule_form_submit(array $form, FormStateInterface $form_state){
  $form_state->setRedirect('user.page');
}
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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