user.api.php 5.9 KB

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