function hook_user_cancel

Same name and namespace in other branches
  1. 7.x modules/user/user.api.php \hook_user_cancel()
  2. 9 core/modules/user/user.api.php \hook_user_cancel()
  3. 8.9.x core/modules/user/user.api.php \hook_user_cancel()
  4. 11.x core/modules/user/user.api.php \hook_user_cancel()

Act on user account cancellations.

This hook is invoked from user_cancel() before a user account is canceled. Depending on the account cancellation method, the module should either do nothing, unpublish content, or anonymize content. See user_cancel_methods() for the list of default account cancellation methods provided by User module. Modules may add further methods via hook_user_cancel_methods_alter().

This hook is NOT invoked for the 'user_cancel_delete' account cancellation method. To react to that method, implement hook_ENTITY_TYPE_predelete() or hook_ENTITY_TYPE_delete() for user entities instead.

Expensive operations should be added to the global account cancellation batch by using batch_set().

Parameters

array $edit: The array of form values submitted by the user.

\Drupal\user\UserInterface $account: The user object on which the operation is being performed.

string $method: The account cancellation method.

See also

user_cancel_methods()

hook_user_cancel_methods_alter()

Related topics

7 functions implement hook_user_cancel()

Note: the procedural functions in this list are found by pattern matching, so the list may include some functions that are not actually implementations of this hook.

CommentHooks::userCancel in core/modules/comment/src/Hook/CommentHooks.php
Implements hook_user_cancel().
comment_user_cancel in core/modules/comment/comment.module
Implements hook_user_cancel().
HistoryHooks::userCancel in core/modules/history/src/Hook/HistoryHooks.php
Implements hook_user_cancel().
history_user_cancel in core/modules/history/history.module
Implements hook_user_cancel().
NodeHooks::userCancelBlockUnpublish in core/modules/node/src/Hook/NodeHooks.php
Implements hook_user_cancel().

... See full list

1 invocation of hook_user_cancel()
user_cancel in core/modules/user/user.module
Cancel a user account.

File

core/modules/user/user.api.php, line 42

Code

function hook_user_cancel($edit, UserInterface $account, $method) {
  switch ($method) {
    case 'user_cancel_block_unpublish':
      // Unpublish nodes (current revisions).
      \Drupal::moduleHandler()->loadInclude('node', 'inc', 'node.admin');
      $nodes = \Drupal::entityQuery('node')->accessCheck(FALSE)
        ->condition('uid', $account->id())
        ->execute();
      node_mass_update($nodes, [
        'status' => 0,
      ], NULL, TRUE);
      break;

    case 'user_cancel_reassign':
      // Anonymize nodes (current revisions).
      \Drupal::moduleHandler()->loadInclude('node', 'inc', 'node.admin');
      $nodes = \Drupal::entityQuery('node')->accessCheck(FALSE)
        ->condition('uid', $account->id())
        ->execute();
      node_mass_update($nodes, [
        'uid' => 0,
      ], NULL, TRUE);
      // Anonymize old revisions.
      \Drupal::database()->update('node_field_revision')
        ->fields([
        'uid' => 0,
      ])
        ->condition('uid', $account->id())
        ->execute();
      break;

  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.