Score:1

How to redirect to a specific page after submitting password reset?

cn flag

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.

in flag
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.
Jaypan avatar
de flag
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.
thiokol avatar
cn flag
I've updated the post with the code tried.
Jaypan avatar
de flag
The route should be `entity.user.canonical`. Other than that, the code looks good. Are you sure the submit handler is being called?
No Sssweat avatar
ua flag
@Jaypan [`user.page`](https://github.com/drupal/drupal/blob/9.4.x/core/modules/user/user.routing.yml#L141) is a fine alternative.
Jaypan avatar
de flag
Oh, interesting. Yeah that would work, thanks I learned something.
Score:2
ua flag

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');
}
thiokol avatar
cn flag
This worked, many thanks!
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.