4

In Drupal 7, users can cancel their accounts from the account settings page. However, by default this requires e-mail confirmation.

There is an option "Require e-mail verification when a visitor creates an account" but no option "Do not require e-mail verification when a user cancels an account."

Is there some way to allow users to simply cancel their accounts while logged in rather than requiring them to verify the cancellation via e-mail?

12

The behaviour seems to be hard-coded into the user_cancel_confirm_form_submit(), which sends a message to the user if they don't have the administer users permission.

I can't see any pretty way of getting round it really, but there is something you can do.

First you need to set an undocumented variable called user_mail_cancel_confirm_notify to FALSE. I'd recommend using Drush to do it:

drush vset user_mail_cancel_confirm_notify FALSE

But you can do it in PHP if you need to:

variable_set('user_mail_cancel_confirm_notify', FALSE);

That will stop the _user_mail_notify() function from sending the cancellation confirmation email.

Next, you need to hook into the form submission for the cancellation confirm form, and force-cancel the account (otherwise it would be waiting for the user to click the cancellation link in the email you've just stopped):

function MYMODULE_form_user_cancel_confirm_form_alter(&$form, &$form_state, $form_id) {
  $form['#submit'][] = 'MYMODULE_user_cancel_form_submit';
}

function MYMODULE_user_cancel_form_submit(&$form, &$form_state) {
  // Rather than negating the complex access expression from the original form we can
  // just make the change in the else portion

  global $user;
  $account = $form_state['values']['_account'];
  if (user_access('administer users') && empty($form_state['values']['user_cancel_confirm']) && $account->uid != $user->uid) {
    // Account has already been cancelled by the system.
  }
  else {
    // Cancel the account
    user_cancel($form_state['values'], $account->uid, $form_state['values']['user_cancel_method']);
  }
}

I haven't tested any of that but after reading the functions involved in the user module I'm pretty sure it will work.

1

The answer above is good but you gain control using your custom form_submit, just copy the code above and change default submit by yours.

function MYMODULE_form_user_cancel_confirm_form_alter(&$form, &$form_state, $form_id) {
  $form['#submit'][0]= 'MYMODULE_user_cancel_form_submit';
}

function MYMODULE_user_cancel_form_submit(&$form, &$form_state) {
  //Copy the code from user_cancel_confirm_form_submit() at user.pages.inc
}

It is a little more tricky because you have to avoid using core's default validation, but you gain control over what you want to show to your users. Because the other solution doesn't stop the messages of "email was sent" to be shown

  • The only issue with doing it this way is that you need to keep track of changes to the original core submit function when you update - if there's a bug fix, or heaven forbid a security fix, that will also need to be ported over to the custom module – Clive Sep 30 '15 at 17:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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