Same name and namespace in other branches
  1. 7.x modules/user/user.module \_user_cancel()
  2. 8.9.x core/modules/user/user.module \_user_cancel()
  3. 9 core/modules/user/user.module \_user_cancel()

Implements callback_batch_operation().

Last step for cancelling a user account.

Since batch and session API require a valid user account, the actual cancellation of a user account needs to happen last.

Parameters

array $edit: An array of submitted form values.

\Drupal\user\UserInterface $account: The user ID of the user account to cancel.

string $method: The account cancellation method to use.

See also

user_cancel()

1 string reference to '_user_cancel'
user_cancel in core/modules/user/user.module
Cancel a user account.

File

core/modules/user/user.module, line 668
Enables the user registration and login system.

Code

function _user_cancel($edit, $account, $method) {
  $logger = \Drupal::logger('user');
  switch ($method) {
    case 'user_cancel_block':
    case 'user_cancel_block_unpublish':
    default:

      // Send account blocked notification if option was checked.
      if (!empty($edit['user_cancel_notify'])) {
        _user_mail_notify('status_blocked', $account);
      }
      $account
        ->block();
      $account
        ->save();
      \Drupal::messenger()
        ->addStatus(t('Account %name has been disabled.', [
        '%name' => $account
          ->getDisplayName(),
      ]));
      $logger
        ->notice('Blocked user: %name %email.', [
        '%name' => $account
          ->getAccountName(),
        '%email' => '<' . $account
          ->getEmail() . '>',
      ]);
      break;
    case 'user_cancel_reassign':
    case 'user_cancel_delete':

      // Send account canceled notification if option was checked.
      if (!empty($edit['user_cancel_notify'])) {
        _user_mail_notify('status_canceled', $account);
      }
      $account
        ->delete();
      \Drupal::messenger()
        ->addStatus(t('Account %name has been deleted.', [
        '%name' => $account
          ->getDisplayName(),
      ]));
      $logger
        ->notice('Deleted user: %name %email.', [
        '%name' => $account
          ->getAccountName(),
        '%email' => '<' . $account
          ->getEmail() . '>',
      ]);
      break;
  }

  // After cancelling account, ensure that user is logged out. We can't destroy
  // their session though, as we might have information in it, and we can't
  // regenerate it because batch API uses the session ID, we will regenerate it
  // in _user_cancel_session_regenerate().
  if ($account
    ->id() == \Drupal::currentUser()
    ->id()) {
    \Drupal::currentUser()
      ->setAccount(new AnonymousUserSession());
  }
}