5 * Hooks provided by the User module.
14 * Act on user account cancellations.
16 * This hook is invoked from user_cancel() before a user account is canceled.
17 * Depending on the account cancellation method, the module should either do
18 * nothing, unpublish content, or anonymize content. See user_cancel_methods()
19 * for the list of default account cancellation methods provided by User module.
20 * Modules may add further methods via hook_user_cancel_methods_alter().
22 * This hook is NOT invoked for the 'user_cancel_delete' account cancellation
23 * method. To react to that method, implement hook_ENTITY_TYPE_predelete() or
24 * hook_ENTITY_TYPE_delete() for user entities instead.
26 * Expensive operations should be added to the global account cancellation batch
27 * by using batch_set().
30 * The array of form values submitted by the user.
31 * @param \Drupal\Core\Session\AccountInterface $account
32 * The user object on which the operation is being performed.
33 * @param string $method
34 * The account cancellation method.
36 * @see user_cancel_methods()
37 * @see hook_user_cancel_methods_alter()
39 function hook_user_cancel($edit, $account, $method) {
41 case 'user_cancel_block_unpublish':
42 // Unpublish nodes (current revisions).
43 module_load_include('inc', 'node', 'node.admin');
44 $nodes = \Drupal::entityQuery('node')
45 ->condition('uid', $account->id())
47 node_mass_update($nodes, ['status' => 0], NULL, TRUE);
50 case 'user_cancel_reassign':
51 // Anonymize nodes (current revisions).
52 module_load_include('inc', 'node', 'node.admin');
53 $nodes = \Drupal::entityQuery('node')
54 ->condition('uid', $account->id())
56 node_mass_update($nodes, ['uid' => 0], NULL, TRUE);
57 // Anonymize old revisions.
58 db_update('node_field_revision')
59 ->fields(['uid' => 0])
60 ->condition('uid', $account->id())
67 * Modify account cancellation methods.
69 * By implementing this hook, modules are able to add, customize, or remove
70 * account cancellation methods. All defined methods are turned into radio
71 * button form elements by user_cancel_methods() after this hook is invoked.
72 * The following properties can be defined for each method:
73 * - title: The radio button's title.
74 * - description: (optional) A description to display on the confirmation form
75 * if the user is not allowed to select the account cancellation method. The
76 * description is NOT used for the radio button, but instead should provide
77 * additional explanation to the user seeking to cancel their account.
78 * - access: (optional) A boolean value indicating whether the user can access
79 * a method. If 'access' is defined, the method cannot be configured as
82 * @param array $methods
83 * An array containing user account cancellation methods, keyed by method id.
85 * @see user_cancel_methods()
86 * @see \Drupal\user\Form\UserCancelForm
88 function hook_user_cancel_methods_alter(&$methods) {
89 $account = \Drupal::currentUser();
90 // Limit access to disable account and unpublish content method.
91 $methods['user_cancel_block_unpublish']['access'] = $account->hasPermission('administer site configuration');
93 // Remove the content re-assigning method.
94 unset($methods['user_cancel_reassign']);
96 // Add a custom zero-out method.
97 $methods['mymodule_zero_out'] = [
98 'title' => t('Delete the account and remove all content.'),
99 'description' => t('All your content will be replaced by empty strings.'),
100 // access should be used for administrative methods only.
101 'access' => $account->hasPermission('access zero-out account cancellation method'),
106 * Alter the username that is displayed for a user.
108 * Called by $account->getDisplayName() to allow modules to alter the username
109 * that is displayed. Can be used to ensure user privacy in situations where
110 * $account->getDisplayName() is too revealing.
112 * @param string|Drupal\Component\Render\MarkupInterface $name
113 * The username that is displayed for a user. If a hook implementation changes
114 * this to an object implementing MarkupInterface it is the responsibility of
115 * the implementation to ensure the user's name is escaped properly. String
116 * values will be autoescaped.
117 * @param \Drupal\Core\Session\AccountInterface $account
118 * The user object on which the operation is being performed.
120 * @see \Drupal\Core\Session\AccountInterface::getDisplayName()
123 function hook_user_format_name_alter(&$name, $account) {
124 // Display the user's uid instead of name.
125 if ($account->id()) {
126 $name = t('User @uid', ['@uid' => $account->id()]);
131 * The user just logged in.
133 * @param object $account
134 * The user object on which the operation was just performed.
136 function hook_user_login($account) {
137 $config = \Drupal::config('system.date');
138 // If the user has a NULL time zone, notify them to set a time zone.
139 if (!$account->getTimezone() && $config->get('timezone.user.configurable') && $config->get('timezone.user.warn')) {
140 drupal_set_message(t('Configure your <a href=":user-edit">account time zone setting</a>.', [':user-edit' => $account->url('edit-form', ['query' => \Drupal::destination()->getAsArray(), 'fragment' => 'edit-timezone'])]));
145 * The user just logged out.
148 * The user object on which the operation was just performed.
150 function hook_user_logout($account) {
153 'uid' => $account->id(),
160 * @} End of "addtogroup hooks".