Pull merge.
[yaffs-website] / web / core / modules / user / user.api.php
1 <?php
2
3 /**
4  * @file
5  * Hooks provided by the User module.
6  */
7
8 use Drupal\Core\Session\AccountInterface;
9 use Drupal\user\UserInterface;
10
11 /**
12  * @addtogroup hooks
13  * @{
14  */
15
16 /**
17  * Act on user account cancellations.
18  *
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().
24  *
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.
28  *
29  * Expensive operations should be added to the global account cancellation batch
30  * by using batch_set().
31  *
32  * @param array $edit
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.
38  *
39  * @see user_cancel_methods()
40  * @see hook_user_cancel_methods_alter()
41  */
42 function hook_user_cancel($edit, UserInterface $account, $method) {
43   switch ($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())
49         ->execute();
50       node_mass_update($nodes, ['status' => 0], NULL, TRUE);
51       break;
52
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())
58         ->execute();
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())
64         ->execute();
65       break;
66   }
67 }
68
69 /**
70  * Modify account cancellation methods.
71  *
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
83  *   default method.
84  *
85  * @param array $methods
86  *   An array containing user account cancellation methods, keyed by method id.
87  *
88  * @see user_cancel_methods()
89  * @see \Drupal\user\Form\UserCancelForm
90  */
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');
95
96   // Remove the content re-assigning method.
97   unset($methods['user_cancel_reassign']);
98
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'),
105   ];
106 }
107
108 /**
109  * Alter the username that is displayed for a user.
110  *
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.
115  *
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:
125  *   @code
126  *   if ($account instanceof UserInterface) {
127  *      // Access user entity methods.
128  *   }
129  *   @endcode
130  *
131  * @see \Drupal\Core\Session\AccountInterface::getDisplayName()
132  * @see sanitization
133  */
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()]);
138   }
139 }
140
141 /**
142  * The user just logged in.
143  *
144  * @param \Drupal\user\UserInterface $account
145  *   The user object on which the operation was just performed.
146  */
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')) {
151     \Drupal::messenger()
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()
155             ->getAsArray(),
156           'fragment' => 'edit-timezone',
157         ]),
158       ]));
159   }
160 }
161
162 /**
163  * The user just logged out.
164  *
165  * @param \Drupal\Core\Session\AccountInterface $account
166  *   The user object on which the operation was just performed.
167  */
168 function hook_user_logout(AccountInterface $account) {
169   db_insert('logouts')
170     ->fields([
171       'uid' => $account->id(),
172       'time' => time(),
173     ])
174     ->execute();
175 }
176
177 /**
178  * @} End of "addtogroup hooks".
179  */