0

I have a submit handler to the user form. It redirect the user to a specific page.

use Symfony\Component\HttpFoundation\RedirectResponse as RedirectResponse;

/**
 * @file
 * Primary module hooks for EHESS Propositions 3 module.
 *
 * @DCG
 * This file is no longer required in Drupal 8.
 * @see https://www.drupal.org/node/2217931
 */
function ehess_propositions_3_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'user_form') {
    foreach (array_keys($form['actions']) as $action) {
      if (isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
        $form['actions'][$action]['#submit'][] = '_ehess_propositions_3_user_profile_submit';
      }
    }
  }
}

function _ehess_propositions_3_user_profile_submit($form, &$form_state) {
  $response = new RedirectResponse('/admin/content/reglement/');
  $response->send();
}

But the problem is that the submit handler is fired both for user update and user deletion. That block the redirection to "confirm deletion" page of user.

Is there a specific action to updating user form only and not user delete?

2
  • You are attaching the submit handler to all submit buttons, including the cancel button. BTW don't send the response, add it to $form_state. See drupal.stackexchange.com/questions/5861/…
    – 4uk4
    Nov 29, 2021 at 10:43
  • But nothing in the example distinguish beetwen update user and delete user. That was my question to which action attached the handler? Is there a "submit-update" action like ? Nov 29, 2021 at 12:50

1 Answer 1

0

$action can be submit and delete. You don't need the foreach loop if you don't want to attach the submit handler to all submit buttons.

Add the handler to the save button directly:

function ehess_propositions_3_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'user_form') {
    $form['actions']['submit']['#submit'][] = '_ehess_propositions_3_user_profile_submit';
  }
}

For the correct way to redirect in a form see https://drupal.stackexchange.com/a/191594/47547

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.