user.api.php

You are here

  1. 7 modules/user/user.api.php
  2. 8 core/modules/user/user.api.php

Hooks provided by the User module.

Functions

Namesort descending Description
hook_user_cancel Act on user account cancellations.
hook_user_cancel_methods_alter Modify account cancellation methods.
hook_user_categories Retrieve a list of user setting or profile information categories.
hook_user_delete Respond to user deletion.
hook_user_insert A user account was created.
hook_user_load Act on user objects when loaded from the database.
hook_user_login The user just logged in.
hook_user_logout The user just logged out.
hook_user_operations Add mass user operations.
hook_user_presave A user account is about to be created or updated.
hook_user_role_delete Inform other modules that a user role has been deleted.
hook_user_role_insert Inform other modules that a user role has been added.
hook_user_role_presave Inform other modules that a user role is about to be saved.
hook_user_role_update Inform other modules that a user role has been updated.
hook_user_update A user account was updated.
hook_user_view The user's account information is being displayed.
hook_user_view_alter The user was built; the module may modify the structured content.

File

modules/user/user.api.php
View source
  1. <?php
  2. /**
  3. * @file
  4. * Hooks provided by the User module.
  5. */
  6. /**
  7. * @addtogroup hooks
  8. * @{
  9. */
  10. /**
  11. * Act on user objects when loaded from the database.
  12. *
  13. * Due to the static cache in user_load_multiple() you should not use this
  14. * hook to modify the user properties returned by the {users} table itself
  15. * since this may result in unreliable results when loading from cache.
  16. *
  17. * @param $users
  18. * An array of user objects, indexed by uid.
  19. *
  20. * @see user_load_multiple()
  21. * @see profile_user_load()
  22. */
  23. function hook_user_load($users) {
  24. $result = db_query('SELECT uid, foo FROM {my_table} WHERE uid IN (:uids)', array(':uids' => array_keys($users)));
  25. foreach ($result as $record) {
  26. $users[$record->uid]->foo = $record->foo;
  27. }
  28. }
  29. /**
  30. * Respond to user deletion.
  31. *
  32. * This hook is invoked from user_delete_multiple() before field_attach_delete()
  33. * is called and before users are actually removed from the database.
  34. *
  35. * Modules should additionally implement hook_user_cancel() to process stored
  36. * user data for other account cancellation methods.
  37. *
  38. * @param $account
  39. * The account that is being deleted.
  40. *
  41. * @see user_delete_multiple()
  42. */
  43. function hook_user_delete($account) {
  44. db_delete('mytable')
  45. ->condition('uid', $account->uid)
  46. ->execute();
  47. }
  48. /**
  49. * Act on user account cancellations.
  50. *
  51. * This hook is invoked from user_cancel() before a user account is canceled.
  52. * Depending on the account cancellation method, the module should either do
  53. * nothing, unpublish content, or anonymize content. See user_cancel_methods()
  54. * for the list of default account cancellation methods provided by User module.
  55. * Modules may add further methods via hook_user_cancel_methods_alter().
  56. *
  57. * This hook is NOT invoked for the 'user_cancel_delete' account cancellation
  58. * method. To react on this method, implement hook_user_delete() instead.
  59. *
  60. * Expensive operations should be added to the global account cancellation batch
  61. * by using batch_set().
  62. *
  63. * @param $edit
  64. * The array of form values submitted by the user.
  65. * @param $account
  66. * The user object on which the operation is being performed.
  67. * @param $method
  68. * The account cancellation method.
  69. *
  70. * @see user_cancel_methods()
  71. * @see hook_user_cancel_methods_alter()
  72. */
  73. function hook_user_cancel($edit, $account, $method) {
  74. switch ($method) {
  75. case 'user_cancel_block_unpublish':
  76. // Unpublish nodes (current revisions).
  77. module_load_include('inc', 'node', 'node.admin');
  78. $nodes = db_select('node', 'n')
  79. ->fields('n', array('nid'))
  80. ->condition('uid', $account->uid)
  81. ->execute()
  82. ->fetchCol();
  83. node_mass_update($nodes, array('status' => 0));
  84. break;
  85. case 'user_cancel_reassign':
  86. // Anonymize nodes (current revisions).
  87. module_load_include('inc', 'node', 'node.admin');
  88. $nodes = db_select('node', 'n')
  89. ->fields('n', array('nid'))
  90. ->condition('uid', $account->uid)
  91. ->execute()
  92. ->fetchCol();
  93. node_mass_update($nodes, array('uid' => 0));
  94. // Anonymize old revisions.
  95. db_update('node_revision')
  96. ->fields(array('uid' => 0))
  97. ->condition('uid', $account->uid)
  98. ->execute();
  99. // Clean history.
  100. db_delete('history')
  101. ->condition('uid', $account->uid)
  102. ->execute();
  103. break;
  104. }
  105. }
  106. /**
  107. * Modify account cancellation methods.
  108. *
  109. * By implementing this hook, modules are able to add, customize, or remove
  110. * account cancellation methods. All defined methods are turned into radio
  111. * button form elements by user_cancel_methods() after this hook is invoked.
  112. * The following properties can be defined for each method:
  113. * - title: The radio button's title.
  114. * - description: (optional) A description to display on the confirmation form
  115. * if the user is not allowed to select the account cancellation method. The
  116. * description is NOT used for the radio button, but instead should provide
  117. * additional explanation to the user seeking to cancel their account.
  118. * - access: (optional) A boolean value indicating whether the user can access
  119. * a method. If #access is defined, the method cannot be configured as default
  120. * method.
  121. *
  122. * @param $methods
  123. * An array containing user account cancellation methods, keyed by method id.
  124. *
  125. * @see user_cancel_methods()
  126. * @see user_cancel_confirm_form()
  127. */
  128. function hook_user_cancel_methods_alter(&$methods) {
  129. // Limit access to disable account and unpublish content method.
  130. $methods['user_cancel_block_unpublish']['access'] = user_access('administer site configuration');
  131. // Remove the content re-assigning method.
  132. unset($methods['user_cancel_reassign']);
  133. // Add a custom zero-out method.
  134. $methods['mymodule_zero_out'] = array(
  135. 'title' => t('Delete the account and remove all content.'),
  136. 'description' => t('All your content will be replaced by empty strings.'),
  137. // access should be used for administrative methods only.
  138. 'access' => user_access('access zero-out account cancellation method'),
  139. );
  140. }
  141. /**
  142. * Add mass user operations.
  143. *
  144. * This hook enables modules to inject custom operations into the mass operations
  145. * dropdown found at admin/people, by associating a callback function with
  146. * the operation, which is called when the form is submitted. The callback function
  147. * receives one initial argument, which is an array of the checked users.
  148. *
  149. * @return
  150. * An array of operations. Each operation is an associative array that may
  151. * contain the following key-value pairs:
  152. * - "label": Required. The label for the operation, displayed in the dropdown menu.
  153. * - "callback": Required. The function to call for the operation.
  154. * - "callback arguments": Optional. An array of additional arguments to pass to
  155. * the callback function.
  156. *
  157. */
  158. function hook_user_operations() {
  159. $operations = array(
  160. 'unblock' => array(
  161. 'label' => t('Unblock the selected users'),
  162. 'callback' => 'user_user_operations_unblock',
  163. ),
  164. 'block' => array(
  165. 'label' => t('Block the selected users'),
  166. 'callback' => 'user_user_operations_block',
  167. ),
  168. 'cancel' => array(
  169. 'label' => t('Cancel the selected user accounts'),
  170. ),
  171. );
  172. return $operations;
  173. }
  174. /**
  175. * Retrieve a list of user setting or profile information categories.
  176. *
  177. * @return
  178. * An array of associative arrays. Each inner array has elements:
  179. * - "name": The internal name of the category.
  180. * - "title": The human-readable, localized name of the category.
  181. * - "weight": An integer specifying the category's sort ordering.
  182. * - "access callback": Name of the access callback function to use to
  183. * determine whether the user can edit the category. Defaults to using
  184. * user_edit_access(). See hook_menu() for more information on access
  185. * callbacks.
  186. * - "access arguments": Arguments for the access callback function. Defaults
  187. * to array(1).
  188. */
  189. function hook_user_categories() {
  190. return array(array(
  191. 'name' => 'account',
  192. 'title' => t('Account settings'),
  193. 'weight' => 1,
  194. ));
  195. }
  196. /**
  197. * A user account is about to be created or updated.
  198. *
  199. * This hook is primarily intended for modules that want to store properties in
  200. * the serialized {users}.data column, which is automatically loaded whenever a
  201. * user account object is loaded, modules may add to $edit['data'] in order
  202. * to have their data serialized on save.
  203. *
  204. * @param $edit
  205. * The array of form values submitted by the user. Assign values to this
  206. * array to save changes in the database.
  207. * @param $account
  208. * The user object on which the operation is performed. Values assigned in
  209. * this object will not be saved in the database.
  210. * @param $category
  211. * The active category of user information being edited.
  212. *
  213. * @see hook_user_insert()
  214. * @see hook_user_update()
  215. */
  216. function hook_user_presave(&$edit, $account, $category) {
  217. // Make sure that our form value 'mymodule_foo' is stored as
  218. // 'mymodule_bar' in the 'data' (serialized) column.
  219. if (isset($edit['mymodule_foo'])) {
  220. $edit['data']['mymodule_bar'] = $edit['mymodule_foo'];
  221. }
  222. }
  223. /**
  224. * A user account was created.
  225. *
  226. * The module should save its custom additions to the user object into the
  227. * database.
  228. *
  229. * @param $edit
  230. * The array of form values submitted by the user.
  231. * @param $account
  232. * The user object on which the operation is being performed.
  233. * @param $category
  234. * The active category of user information being edited.
  235. *
  236. * @see hook_user_presave()
  237. * @see hook_user_update()
  238. */
  239. function hook_user_insert(&$edit, $account, $category) {
  240. db_insert('mytable')
  241. ->fields(array(
  242. 'myfield' => $edit['myfield'],
  243. 'uid' => $account->uid,
  244. ))
  245. ->execute();
  246. }
  247. /**
  248. * A user account was updated.
  249. *
  250. * Modules may use this hook to update their user data in a custom storage
  251. * after a user account has been updated.
  252. *
  253. * @param $edit
  254. * The array of form values submitted by the user.
  255. * @param $account
  256. * The user object on which the operation is performed.
  257. * @param $category
  258. * The active category of user information being edited.
  259. *
  260. * @see hook_user_presave()
  261. * @see hook_user_insert()
  262. */
  263. function hook_user_update(&$edit, $account, $category) {
  264. db_insert('user_changes')
  265. ->fields(array(
  266. 'uid' => $account->uid,
  267. 'changed' => time(),
  268. ))
  269. ->execute();
  270. }
  271. /**
  272. * The user just logged in.
  273. *
  274. * @param $edit
  275. * The array of form values submitted by the user.
  276. * @param $account
  277. * The user object on which the operation was just performed.
  278. */
  279. function hook_user_login(&$edit, $account) {
  280. // If the user has a NULL time zone, notify them to set a time zone.
  281. if (!$account->timezone && variable_get('configurable_timezones', 1) && variable_get('empty_timezone_message', 0)) {
  282. drupal_set_message(t('Configure your <a href="@user-edit">account time zone setting</a>.', array('@user-edit' => url("user/$account->uid/edit", array('query' => drupal_get_destination(), 'fragment' => 'edit-timezone')))));
  283. }
  284. }
  285. /**
  286. * The user just logged out.
  287. *
  288. * Note that when this hook is invoked, the changes have not yet been written to
  289. * the database, because a database transaction is still in progress. The
  290. * transaction is not finalized until the save operation is entirely completed
  291. * and user_save() goes out of scope. You should not rely on data in the
  292. * database at this time as it is not updated yet. You should also note that any
  293. * write/update database queries executed from this hook are also not committed
  294. * immediately. Check user_save() and db_transaction() for more info.
  295. *
  296. * @param $account
  297. * The user object on which the operation was just performed.
  298. */
  299. function hook_user_logout($account) {
  300. db_insert('logouts')
  301. ->fields(array(
  302. 'uid' => $account->uid,
  303. 'time' => time(),
  304. ))
  305. ->execute();
  306. }
  307. /**
  308. * The user's account information is being displayed.
  309. *
  310. * The module should format its custom additions for display and add them to the
  311. * $account->content array.
  312. *
  313. * Note that when this hook is invoked, the changes have not yet been written to
  314. * the database, because a database transaction is still in progress. The
  315. * transaction is not finalized until the save operation is entirely completed
  316. * and user_save() goes out of scope. You should not rely on data in the
  317. * database at this time as it is not updated yet. You should also note that any
  318. * write/update database queries executed from this hook are also not committed
  319. * immediately. Check user_save() and db_transaction() for more info.
  320. *
  321. * @param $account
  322. * The user object on which the operation is being performed.
  323. * @param $view_mode
  324. * View mode, e.g. 'full'.
  325. * @param $langcode
  326. * The language code used for rendering.
  327. *
  328. * @see hook_user_view_alter()
  329. * @see hook_entity_view()
  330. */
  331. function hook_user_view($account, $view_mode, $langcode) {
  332. if (user_access('create blog content', $account)) {
  333. $account->content['summary']['blog'] = array(
  334. '#type' => 'user_profile_item',
  335. '#title' => t('Blog'),
  336. '#markup' => l(t('View recent blog entries'), "blog/$account->uid", array('attributes' => array('title' => t("Read !username's latest blog entries.", array('!username' => format_username($account)))))),
  337. '#attributes' => array('class' => array('blog')),
  338. );
  339. }
  340. }
  341. /**
  342. * The user was built; the module may modify the structured content.
  343. *
  344. * This hook is called after the content has been assembled in a structured array
  345. * and may be used for doing processing which requires that the complete user
  346. * content structure has been built.
  347. *
  348. * If the module wishes to act on the rendered HTML of the user rather than the
  349. * structured content array, it may use this hook to add a #post_render callback.
  350. * Alternatively, it could also implement hook_preprocess_user_profile(). See
  351. * drupal_render() and theme() documentation respectively for details.
  352. *
  353. * @param $build
  354. * A renderable array representing the user.
  355. *
  356. * @see user_view()
  357. * @see hook_entity_view_alter()
  358. */
  359. function hook_user_view_alter(&$build) {
  360. // Check for the existence of a field added by another module.
  361. if (isset($build['an_additional_field'])) {
  362. // Change its weight.
  363. $build['an_additional_field']['#weight'] = -10;
  364. }
  365. // Add a #post_render callback to act on the rendered HTML of the user.
  366. $build['#post_render'][] = 'my_module_user_post_render';
  367. }
  368. /**
  369. * Inform other modules that a user role is about to be saved.
  370. *
  371. * Modules implementing this hook can act on the user role object before
  372. * it has been saved to the database.
  373. *
  374. * @param $role
  375. * A user role object.
  376. *
  377. * @see hook_user_role_insert()
  378. * @see hook_user_role_update()
  379. */
  380. function hook_user_role_presave($role) {
  381. // Set a UUID for the user role if it doesn't already exist
  382. if (empty($role->uuid)) {
  383. $role->uuid = uuid_uuid();
  384. }
  385. }
  386. /**
  387. * Inform other modules that a user role has been added.
  388. *
  389. * Modules implementing this hook can act on the user role object when saved to
  390. * the database. It's recommended that you implement this hook if your module
  391. * adds additional data to user roles object. The module should save its custom
  392. * additions to the database.
  393. *
  394. * @param $role
  395. * A user role object.
  396. */
  397. function hook_user_role_insert($role) {
  398. // Save extra fields provided by the module to user roles.
  399. db_insert('my_module_table')
  400. ->fields(array(
  401. 'rid' => $role->rid,
  402. 'role_description' => $role->description,
  403. ))
  404. ->execute();
  405. }
  406. /**
  407. * Inform other modules that a user role has been updated.
  408. *
  409. * Modules implementing this hook can act on the user role object when updated.
  410. * It's recommended that you implement this hook if your module adds additional
  411. * data to user roles object. The module should save its custom additions to
  412. * the database.
  413. *
  414. * @param $role
  415. * A user role object.
  416. */
  417. function hook_user_role_update($role) {
  418. // Save extra fields provided by the module to user roles.
  419. db_merge('my_module_table')
  420. ->key(array('rid' => $role->rid))
  421. ->fields(array(
  422. 'role_description' => $role->description
  423. ))
  424. ->execute();
  425. }
  426. /**
  427. * Inform other modules that a user role has been deleted.
  428. *
  429. * This hook allows you act when a user role has been deleted.
  430. * If your module stores references to roles, it's recommended that you
  431. * implement this hook and delete existing instances of the deleted role
  432. * in your module database tables.
  433. *
  434. * @param $role
  435. * The $role object being deleted.
  436. */
  437. function hook_user_role_delete($role) {
  438. // Delete existing instances of the deleted role.
  439. db_delete('my_module_table')
  440. ->condition('rid', $role->rid)
  441. ->execute();
  442. }
  443. /**
  444. * @} End of "addtogroup hooks".
  445. */