You are here

function _user_cancel in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/user/user.module \_user_cancel()
  2. 7 modules/user/user.module \_user_cancel()
  3. 10 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 664
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('%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('%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());
  }
}