2

I want to allow users to cancel their accounts immediately instead of having to confirm the cancellation by email (because users forget passwords, lose access to email, and other problems).

There is a great answer to the Drupal 7 version of this question and it inspired a module.

So I attempted to adopt the code in the module for Drupal 8.

First, though, you need to prevent the "account cancellation confirmation email from being sent; otherwise, users will receive an email to click a link when they don't need to.

You can do this as follows:

  1. drush cex
  2. Edit user.settings.yml
  3. Change cancel_confirm to false
  4. drush cim

Here is my code with a problem:

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

function MYMODULE_user_cancel_form_submit(&$form, &$form_state) {
  $uid = $form['uid']['#value'];
  $user = \Drupal::currentUser();
  if (!$user->hasPermission('administer users') && $uid == $user->id()) {    
    // Hide "A confirmation request to cancel your account has been sent to your email address."
    $messages = drupal_get_messages('status', FALSE);
    if (!empty($messages['status'])) {
      unset($_SESSION['messages']['status']);
    }

    // Cancel the account, from user_cancel_confirm()
    user_cancel($form_state->getValues(), $uid, $form_state->getValue('user_cancel_method'));    

    // Since user_cancel() is not invoked via Form API, batch processing needs
    // to be invoked manually and will redirect to the front page after
    // completion.
    batch_process('');

  }
}

The main problem is that after calling the submit callback, I get the message "The update has been performed.", but the user account is not cancelled.

I used kint() to debug and the submit callback is evaluated and user_cancel is called, but the account is not actually blocked. Users do have permission to cancel their own accounts.

  • Is it a typo or your kint text is not escaped? You wrote kint('why can't I see this?'); – Kojo Oct 14 '17 at 12:46
  • @Kojo typo, I was actually printing the form variable – Patrick Kenny Oct 14 '17 at 13:00
  • $form['actions']['submit']['#submit'][] = 'MYMODULE_user_cancel_form_submit'; The form submit will get called – Shreya Shetty Oct 16 '17 at 6:01
  • @ShreyaShetty Thanks, that got me much farther. I have updated the question. – Patrick Kenny Oct 16 '17 at 6:29
3

Please use this code snippet it works for me

 function simple_pass_reset_form_user_cancel_form_alter(&$form, &$form_state, $form_id) {
  //  dsm($form);
  $form['actions']['submit']['#submit'][] = 'simple_pass_reset_user_cancel_form_submit';
}

function simple_pass_reset_user_cancel_form_submit(&$form, &$form_state) {
  $uid = $form['uid']['#value'];
  // exit;
  $user = \Drupal::currentUser();
  if (!$user->hasPermission('administer users') && $uid == $user->id()) {
    // Hide "A confirmation request to cancel your account has been sent to your email address."
    $messages = drupal_get_messages('status', FALSE);
    if (!empty($messages['status'])) {
      unset($_SESSION['messages']['status']);
    }

    // Cancel the account, from user_cancel_confirm()
    user_cancel($form_state->getValues(), $uid, $form_state->getValue('user_cancel_method'));

    // Since user_cancel() is not invoked via Form API, batch processing needs
    // to be invoked manually and will redirect to the front page after
    // completion.
//You need to run batch process
    $batch = &batch_get();
    $batch['progressive'] = FALSE;
    batch_process('');
  }
}

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.