hook_user_cancel

hook_user_cancel($edit, $account, $method)

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\Core\Session\AccountInterface $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

Hooks
Define functions that alter the behavior of Drupal core.

File

core/modules/user/user.api.php, line 39
Hooks provided by the User module.

Code

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

    case 'user_cancel_reassign':
      // Anonymize nodes (current revisions).
      module_load_include('inc', 'node', 'node.admin');
      $nodes = \Drupal::entityQuery('node')
        ->condition('uid', $account->id())
        ->execute();
      node_mass_update($nodes, array('uid' => 0), NULL, TRUE);
      // Anonymize old revisions.
      db_update('node_field_revision')
        ->fields(array('uid' => 0))
        ->condition('uid', $account->id())
        ->execute();
      break;
  }
}
doc_Drupal
2016-10-29 09:18:36
Comments
Leave a Comment

Please login to continue.