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