Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /**
|
Chris@0
|
4 * @file
|
Chris@0
|
5 * Hooks provided by the User module.
|
Chris@0
|
6 */
|
Chris@0
|
7
|
Chris@0
|
8 /**
|
Chris@0
|
9 * @addtogroup hooks
|
Chris@0
|
10 * @{
|
Chris@0
|
11 */
|
Chris@0
|
12
|
Chris@0
|
13 /**
|
Chris@0
|
14 * Act on user account cancellations.
|
Chris@0
|
15 *
|
Chris@0
|
16 * This hook is invoked from user_cancel() before a user account is canceled.
|
Chris@0
|
17 * Depending on the account cancellation method, the module should either do
|
Chris@0
|
18 * nothing, unpublish content, or anonymize content. See user_cancel_methods()
|
Chris@0
|
19 * for the list of default account cancellation methods provided by User module.
|
Chris@0
|
20 * Modules may add further methods via hook_user_cancel_methods_alter().
|
Chris@0
|
21 *
|
Chris@0
|
22 * This hook is NOT invoked for the 'user_cancel_delete' account cancellation
|
Chris@0
|
23 * method. To react to that method, implement hook_ENTITY_TYPE_predelete() or
|
Chris@0
|
24 * hook_ENTITY_TYPE_delete() for user entities instead.
|
Chris@0
|
25 *
|
Chris@0
|
26 * Expensive operations should be added to the global account cancellation batch
|
Chris@0
|
27 * by using batch_set().
|
Chris@0
|
28 *
|
Chris@0
|
29 * @param array $edit
|
Chris@0
|
30 * The array of form values submitted by the user.
|
Chris@0
|
31 * @param \Drupal\Core\Session\AccountInterface $account
|
Chris@0
|
32 * The user object on which the operation is being performed.
|
Chris@0
|
33 * @param string $method
|
Chris@0
|
34 * The account cancellation method.
|
Chris@0
|
35 *
|
Chris@0
|
36 * @see user_cancel_methods()
|
Chris@0
|
37 * @see hook_user_cancel_methods_alter()
|
Chris@0
|
38 */
|
Chris@0
|
39 function hook_user_cancel($edit, $account, $method) {
|
Chris@0
|
40 switch ($method) {
|
Chris@0
|
41 case 'user_cancel_block_unpublish':
|
Chris@0
|
42 // Unpublish nodes (current revisions).
|
Chris@0
|
43 module_load_include('inc', 'node', 'node.admin');
|
Chris@0
|
44 $nodes = \Drupal::entityQuery('node')
|
Chris@0
|
45 ->condition('uid', $account->id())
|
Chris@0
|
46 ->execute();
|
Chris@0
|
47 node_mass_update($nodes, ['status' => 0], NULL, TRUE);
|
Chris@0
|
48 break;
|
Chris@0
|
49
|
Chris@0
|
50 case 'user_cancel_reassign':
|
Chris@0
|
51 // Anonymize nodes (current revisions).
|
Chris@0
|
52 module_load_include('inc', 'node', 'node.admin');
|
Chris@0
|
53 $nodes = \Drupal::entityQuery('node')
|
Chris@0
|
54 ->condition('uid', $account->id())
|
Chris@0
|
55 ->execute();
|
Chris@0
|
56 node_mass_update($nodes, ['uid' => 0], NULL, TRUE);
|
Chris@0
|
57 // Anonymize old revisions.
|
Chris@0
|
58 db_update('node_field_revision')
|
Chris@0
|
59 ->fields(['uid' => 0])
|
Chris@0
|
60 ->condition('uid', $account->id())
|
Chris@0
|
61 ->execute();
|
Chris@0
|
62 break;
|
Chris@0
|
63 }
|
Chris@0
|
64 }
|
Chris@0
|
65
|
Chris@0
|
66 /**
|
Chris@0
|
67 * Modify account cancellation methods.
|
Chris@0
|
68 *
|
Chris@0
|
69 * By implementing this hook, modules are able to add, customize, or remove
|
Chris@0
|
70 * account cancellation methods. All defined methods are turned into radio
|
Chris@0
|
71 * button form elements by user_cancel_methods() after this hook is invoked.
|
Chris@0
|
72 * The following properties can be defined for each method:
|
Chris@0
|
73 * - title: The radio button's title.
|
Chris@0
|
74 * - description: (optional) A description to display on the confirmation form
|
Chris@0
|
75 * if the user is not allowed to select the account cancellation method. The
|
Chris@0
|
76 * description is NOT used for the radio button, but instead should provide
|
Chris@0
|
77 * additional explanation to the user seeking to cancel their account.
|
Chris@0
|
78 * - access: (optional) A boolean value indicating whether the user can access
|
Chris@0
|
79 * a method. If 'access' is defined, the method cannot be configured as
|
Chris@0
|
80 * default method.
|
Chris@0
|
81 *
|
Chris@0
|
82 * @param array $methods
|
Chris@0
|
83 * An array containing user account cancellation methods, keyed by method id.
|
Chris@0
|
84 *
|
Chris@0
|
85 * @see user_cancel_methods()
|
Chris@0
|
86 * @see \Drupal\user\Form\UserCancelForm
|
Chris@0
|
87 */
|
Chris@0
|
88 function hook_user_cancel_methods_alter(&$methods) {
|
Chris@0
|
89 $account = \Drupal::currentUser();
|
Chris@0
|
90 // Limit access to disable account and unpublish content method.
|
Chris@0
|
91 $methods['user_cancel_block_unpublish']['access'] = $account->hasPermission('administer site configuration');
|
Chris@0
|
92
|
Chris@0
|
93 // Remove the content re-assigning method.
|
Chris@0
|
94 unset($methods['user_cancel_reassign']);
|
Chris@0
|
95
|
Chris@0
|
96 // Add a custom zero-out method.
|
Chris@0
|
97 $methods['mymodule_zero_out'] = [
|
Chris@0
|
98 'title' => t('Delete the account and remove all content.'),
|
Chris@0
|
99 'description' => t('All your content will be replaced by empty strings.'),
|
Chris@0
|
100 // access should be used for administrative methods only.
|
Chris@0
|
101 'access' => $account->hasPermission('access zero-out account cancellation method'),
|
Chris@0
|
102 ];
|
Chris@0
|
103 }
|
Chris@0
|
104
|
Chris@0
|
105 /**
|
Chris@0
|
106 * Alter the username that is displayed for a user.
|
Chris@0
|
107 *
|
Chris@0
|
108 * Called by $account->getDisplayName() to allow modules to alter the username
|
Chris@0
|
109 * that is displayed. Can be used to ensure user privacy in situations where
|
Chris@0
|
110 * $account->getDisplayName() is too revealing.
|
Chris@0
|
111 *
|
Chris@0
|
112 * @param string|Drupal\Component\Render\MarkupInterface $name
|
Chris@0
|
113 * The username that is displayed for a user. If a hook implementation changes
|
Chris@0
|
114 * this to an object implementing MarkupInterface it is the responsibility of
|
Chris@0
|
115 * the implementation to ensure the user's name is escaped properly. String
|
Chris@0
|
116 * values will be autoescaped.
|
Chris@0
|
117 * @param \Drupal\Core\Session\AccountInterface $account
|
Chris@0
|
118 * The user object on which the operation is being performed.
|
Chris@0
|
119 *
|
Chris@0
|
120 * @see \Drupal\Core\Session\AccountInterface::getDisplayName()
|
Chris@0
|
121 * @see sanitization
|
Chris@0
|
122 */
|
Chris@0
|
123 function hook_user_format_name_alter(&$name, $account) {
|
Chris@0
|
124 // Display the user's uid instead of name.
|
Chris@0
|
125 if ($account->id()) {
|
Chris@0
|
126 $name = t('User @uid', ['@uid' => $account->id()]);
|
Chris@0
|
127 }
|
Chris@0
|
128 }
|
Chris@0
|
129
|
Chris@0
|
130 /**
|
Chris@0
|
131 * The user just logged in.
|
Chris@0
|
132 *
|
Chris@0
|
133 * @param object $account
|
Chris@0
|
134 * The user object on which the operation was just performed.
|
Chris@0
|
135 */
|
Chris@0
|
136 function hook_user_login($account) {
|
Chris@0
|
137 $config = \Drupal::config('system.date');
|
Chris@0
|
138 // If the user has a NULL time zone, notify them to set a time zone.
|
Chris@0
|
139 if (!$account->getTimezone() && $config->get('timezone.user.configurable') && $config->get('timezone.user.warn')) {
|
Chris@0
|
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'])]));
|
Chris@0
|
141 }
|
Chris@0
|
142 }
|
Chris@0
|
143
|
Chris@0
|
144 /**
|
Chris@0
|
145 * The user just logged out.
|
Chris@0
|
146 *
|
Chris@0
|
147 * @param $account
|
Chris@0
|
148 * The user object on which the operation was just performed.
|
Chris@0
|
149 */
|
Chris@0
|
150 function hook_user_logout($account) {
|
Chris@0
|
151 db_insert('logouts')
|
Chris@0
|
152 ->fields([
|
Chris@0
|
153 'uid' => $account->id(),
|
Chris@0
|
154 'time' => time(),
|
Chris@0
|
155 ])
|
Chris@0
|
156 ->execute();
|
Chris@0
|
157 }
|
Chris@0
|
158
|
Chris@0
|
159 /**
|
Chris@0
|
160 * @} End of "addtogroup hooks".
|
Chris@0
|
161 */
|