Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[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.
114  *
115  * @param string|Drupal\Component\Render\MarkupInterface $name
116  *   The username that is displayed for a user. If a hook implementation changes
117  *   this to an object implementing MarkupInterface it is the responsibility of
118  *   the implementation to ensure the user's name is escaped properly. String
119  *   values will be autoescaped.
120  * @param \Drupal\Core\Session\AccountInterface $account
121  *   The user object on which the operation is being performed.
122  *
123  * @see \Drupal\Core\Session\AccountInterface::getDisplayName()
124  * @see sanitization
125  */
126 function hook_user_format_name_alter(&$name, AccountInterface $account) {
127   // Display the user's uid instead of name.
128   if ($account->id()) {
129     $name = t('User @uid', ['@uid' => $account->id()]);
130   }
131 }
132
133 /**
134  * The user just logged in.
135  *
136  * @param \Drupal\user\UserInterface $account
137  *   The user object on which the operation was just performed.
138  */
139 function hook_user_login(UserInterface $account) {
140   $config = \Drupal::config('system.date');
141   // If the user has a NULL time zone, notify them to set a time zone.
142   if (!$account->getTimezone() && $config->get('timezone.user.configurable') && $config->get('timezone.user.warn')) {
143     \Drupal::messenger()
144       ->addStatus(t('Configure your <a href=":user-edit">account time zone setting</a>.', [
145         ':user-edit' => $account->url('edit-form', [
146           'query' => \Drupal::destination()
147             ->getAsArray(),
148           'fragment' => 'edit-timezone',
149         ]),
150       ]));
151   }
152 }
153
154 /**
155  * The user just logged out.
156  *
157  * @param \Drupal\Core\Session\AccountInterface $account
158  *   The user object on which the operation was just performed.
159  */
160 function hook_user_logout(AccountInterface $account) {
161   db_insert('logouts')
162     ->fields([
163       'uid' => $account->id(),
164       'time' => time(),
165     ])
166     ->execute();
167 }
168
169 /**
170  * @} End of "addtogroup hooks".
171  */