Mercurial > hg > cmmr2012-drupal-site
comparison core/modules/user/user.api.php @ 4:a9cd425dd02b
Update, including to Drupal core 8.6.10
author | Chris Cannam |
---|---|
date | Thu, 28 Feb 2019 13:11:55 +0000 |
parents | c75dbcec494b |
children | 12f9dff5fda9 |
comparison
equal
deleted
inserted
replaced
3:307d7a7fd348 | 4:a9cd425dd02b |
---|---|
2 | 2 |
3 /** | 3 /** |
4 * @file | 4 * @file |
5 * Hooks provided by the User module. | 5 * Hooks provided by the User module. |
6 */ | 6 */ |
7 | |
8 use Drupal\Core\Session\AccountInterface; | |
9 use Drupal\user\UserInterface; | |
7 | 10 |
8 /** | 11 /** |
9 * @addtogroup hooks | 12 * @addtogroup hooks |
10 * @{ | 13 * @{ |
11 */ | 14 */ |
26 * Expensive operations should be added to the global account cancellation batch | 29 * Expensive operations should be added to the global account cancellation batch |
27 * by using batch_set(). | 30 * by using batch_set(). |
28 * | 31 * |
29 * @param array $edit | 32 * @param array $edit |
30 * The array of form values submitted by the user. | 33 * The array of form values submitted by the user. |
31 * @param \Drupal\Core\Session\AccountInterface $account | 34 * @param \Drupal\user\UserInterface $account |
32 * The user object on which the operation is being performed. | 35 * The user object on which the operation is being performed. |
33 * @param string $method | 36 * @param string $method |
34 * The account cancellation method. | 37 * The account cancellation method. |
35 * | 38 * |
36 * @see user_cancel_methods() | 39 * @see user_cancel_methods() |
37 * @see hook_user_cancel_methods_alter() | 40 * @see hook_user_cancel_methods_alter() |
38 */ | 41 */ |
39 function hook_user_cancel($edit, $account, $method) { | 42 function hook_user_cancel($edit, UserInterface $account, $method) { |
40 switch ($method) { | 43 switch ($method) { |
41 case 'user_cancel_block_unpublish': | 44 case 'user_cancel_block_unpublish': |
42 // Unpublish nodes (current revisions). | 45 // Unpublish nodes (current revisions). |
43 module_load_include('inc', 'node', 'node.admin'); | 46 module_load_include('inc', 'node', 'node.admin'); |
44 $nodes = \Drupal::entityQuery('node') | 47 $nodes = \Drupal::entityQuery('node') |
105 /** | 108 /** |
106 * Alter the username that is displayed for a user. | 109 * Alter the username that is displayed for a user. |
107 * | 110 * |
108 * Called by $account->getDisplayName() to allow modules to alter the username | 111 * 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 | 112 * that is displayed. Can be used to ensure user privacy in situations where |
110 * $account->getDisplayName() is too revealing. | 113 * $account->getDisplayName() is too revealing. This hook is invoked both for |
114 * user entities and the anonymous user session object. | |
111 * | 115 * |
112 * @param string|Drupal\Component\Render\MarkupInterface $name | 116 * @param string|Drupal\Component\Render\MarkupInterface $name |
113 * The username that is displayed for a user. If a hook implementation changes | 117 * 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 | 118 * this to an object implementing MarkupInterface it is the responsibility of |
115 * the implementation to ensure the user's name is escaped properly. String | 119 * the implementation to ensure the user's name is escaped properly. String |
116 * values will be autoescaped. | 120 * values will be autoescaped. |
117 * @param \Drupal\Core\Session\AccountInterface $account | 121 * @param \Drupal\Core\Session\AccountInterface $account |
118 * The user object on which the operation is being performed. | 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 | |
119 * | 130 * |
120 * @see \Drupal\Core\Session\AccountInterface::getDisplayName() | 131 * @see \Drupal\Core\Session\AccountInterface::getDisplayName() |
121 * @see sanitization | 132 * @see sanitization |
122 */ | 133 */ |
123 function hook_user_format_name_alter(&$name, $account) { | 134 function hook_user_format_name_alter(&$name, AccountInterface $account) { |
124 // Display the user's uid instead of name. | 135 // Display the user's uid instead of name. |
125 if ($account->id()) { | 136 if ($account->id()) { |
126 $name = t('User @uid', ['@uid' => $account->id()]); | 137 $name = t('User @uid', ['@uid' => $account->id()]); |
127 } | 138 } |
128 } | 139 } |
129 | 140 |
130 /** | 141 /** |
131 * The user just logged in. | 142 * The user just logged in. |
132 * | 143 * |
133 * @param object $account | 144 * @param \Drupal\user\UserInterface $account |
134 * The user object on which the operation was just performed. | 145 * The user object on which the operation was just performed. |
135 */ | 146 */ |
136 function hook_user_login($account) { | 147 function hook_user_login(UserInterface $account) { |
137 $config = \Drupal::config('system.date'); | 148 $config = \Drupal::config('system.date'); |
138 // If the user has a NULL time zone, notify them to set a time zone. | 149 // 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')) { | 150 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'])])); | 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 ])); | |
141 } | 159 } |
142 } | 160 } |
143 | 161 |
144 /** | 162 /** |
145 * The user just logged out. | 163 * The user just logged out. |
146 * | 164 * |
147 * @param $account | 165 * @param \Drupal\Core\Session\AccountInterface $account |
148 * The user object on which the operation was just performed. | 166 * The user object on which the operation was just performed. |
149 */ | 167 */ |
150 function hook_user_logout($account) { | 168 function hook_user_logout(AccountInterface $account) { |
151 db_insert('logouts') | 169 db_insert('logouts') |
152 ->fields([ | 170 ->fields([ |
153 'uid' => $account->id(), | 171 'uid' => $account->id(), |
154 'time' => time(), | 172 'time' => time(), |
155 ]) | 173 ]) |