Score:0

specific submit handler to user form

us flag

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?

4uk4 avatar
cn flag
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 https://drupal.stackexchange.com/questions/5861/how-to-redirect-to-a-page-after-submitting-a-form
Frédéric Hébert avatar
us flag
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 ?
Score:0
cn flag

$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

Frédéric Hébert avatar
us flag
thanks really. It's obvious now!
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.