You are here

function user_cancel in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/user/user.module \user_cancel()

Cancel a user account.

Since the user cancellation process needs to be run in a batch, either Form API will invoke it, or batch_process() needs to be invoked after calling this function and should define the path to redirect to.

Parameters

array $edit: An array of submitted form values.

int $uid: The user ID of the user account to cancel.

string $method: The account cancellation method to use.

See also

_user_cancel()

3 calls to user_cancel()
UserCancelForm::submitForm in core/modules/user/src/Form/UserCancelForm.php
This is the default entity object builder function. It is called before any other submit handler to build the new entity object to be used by the following submit handlers. At this point of the form workflow the entity is validated and the form state…
UserController::confirmCancel in core/modules/user/src/Controller/UserController.php
Confirms cancelling a user account via an email link.
UserMultipleCancelConfirm::submitForm in core/modules/user/src/Form/UserMultipleCancelConfirm.php
Form submission handler.

File

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

Code

function user_cancel($edit, $uid, $method) {
  $account = User::load($uid);
  if (!$account) {
    drupal_set_message(t('The user account %id does not exist.', array(
      '%id' => $uid,
    )), 'error');
    \Drupal::logger('user')
      ->error('Attempted to cancel non-existing user account: %id.', array(
      '%id' => $uid,
    ));
    return;
  }

  // Initialize batch (to set title).
  $batch = array(
    'title' => t('Cancelling account'),
    'operations' => array(),
  );
  batch_set($batch);

  // When the 'user_cancel_delete' method is used, user_delete() is called,
  // which invokes hook_ENTITY_TYPE_predelete() and hook_ENTITY_TYPE_delete()
  // for the user entity. Modules should use those hooks to respond to the
  // account deletion.
  if ($method != 'user_cancel_delete') {

    // Allow modules to add further sets to this batch.
    \Drupal::moduleHandler()
      ->invokeAll('user_cancel', array(
      $edit,
      $account,
      $method,
    ));
  }

  // Finish the batch and actually cancel the account.
  $batch = array(
    'title' => t('Cancelling user account'),
    'operations' => array(
      array(
        '_user_cancel',
        array(
          $edit,
          $account,
          $method,
        ),
      ),
    ),
  );

  // After cancelling account, ensure that user is logged out.
  if ($account
    ->id() == \Drupal::currentUser()
    ->id()) {

    // Batch API stores data in the session, so use the finished operation to
    // manipulate the current user's session id.
    $batch['finished'] = '_user_cancel_session_regenerate';
  }
  batch_set($batch);

  // Batch processing is either handled via Form API or has to be invoked
  // manually.
}