user.api.php 5.4 KB

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