1

I'm trying to do a redirect after hitting the submit button in a Drupal 8 reset password form.

I use the theme_form_alter hook and a function as a new submit actions. The problem is, that the redirect works but no email is send. If I uncomment the line for the new submit hook the mail is send but of course there is no redirect.

function theme_form_alter(&$form, &$form_state, $form_id) {
  if($form['#form_id'] == 'user_pass') {
    $form['actions']['submit']['#submit'][] = 'user_pass_redirect';
  }
}

function user_pass_redirect(&$form, $form_state) {
  $url = \Drupal\Core\Url::fromRoute('entity.node.canonical', [
    'node' => '87',
  ]);

 $form_state->setRedirectUrl($url);
}

I'm doing exactly the same thing for the contact_message_personal_form form and it works.

So, I have no idea why that happens for the contact_message_personal_form and the user_pass has isn't working.

Thanks for your help

1 Answer 1

1

I found a solution. I compared the output from a dump of user_pass $form and contact_message_personal_form $form variable.
My problem was, that my $form['actions']['submit']['#submit'] array was empty. So I added the missing '::submitForm' parameter and everything works.

function module_form_alter(&$form, $form_state, $form_id) {
  if($form['#form_id'] == 'user_pass') {
    $form['actions']['submit']['#submit'][] = '::submitForm';
    $form['actions']['submit']['#submit'][] = 'user_pass_redirect'; 
  }
}

function user_pass_redirect(&$form, $form_state) {
  $url = \Drupal\Core\Url::fromRoute('entity.node.canonical', [
    'node' => '87',
  ]);
  $form_state->setRedirectUrl($url);
}

I have no idea why my variable was empty but this works for me.

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.