user.api.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. \Drupal::database()->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. This hook is invoked both for
  105. * user entities and the anonymous user session object.
  106. *
  107. * @param string|Drupal\Component\Render\MarkupInterface $name
  108. * The username that is displayed for a user. If a hook implementation changes
  109. * this to an object implementing MarkupInterface it is the responsibility of
  110. * the implementation to ensure the user's name is escaped properly. String
  111. * values will be autoescaped.
  112. * @param \Drupal\Core\Session\AccountInterface $account
  113. * The object on which the operation is being performed. This object may be a
  114. * user entity. If the object is an implementation of UserInterface you can
  115. * use instanceof operator before accessing user entity methods. For example:
  116. * @code
  117. * if ($account instanceof UserInterface) {
  118. * // Access user entity methods.
  119. * }
  120. * @endcode
  121. *
  122. * @see \Drupal\Core\Session\AccountInterface::getDisplayName()
  123. * @see sanitization
  124. */
  125. function hook_user_format_name_alter(&$name, AccountInterface $account) {
  126. // Display the user's uid instead of name.
  127. if ($account->id()) {
  128. $name = t('User @uid', ['@uid' => $account->id()]);
  129. }
  130. }
  131. /**
  132. * The user just logged in.
  133. *
  134. * @param \Drupal\user\UserInterface $account
  135. * The user object on which the operation was just performed.
  136. */
  137. function hook_user_login(UserInterface $account) {
  138. $config = \Drupal::config('system.date');
  139. // If the user has a NULL time zone, notify them to set a time zone.
  140. if (!$account->getTimezone() && $config->get('timezone.user.configurable') && $config->get('timezone.user.warn')) {
  141. \Drupal::messenger()
  142. ->addStatus(t('Configure your <a href=":user-edit">account time zone setting</a>.', [
  143. ':user-edit' => $account->toUrl('edit-form', [
  144. 'query' => \Drupal::destination()
  145. ->getAsArray(),
  146. 'fragment' => 'edit-timezone',
  147. ])->toString(),
  148. ]));
  149. }
  150. }
  151. /**
  152. * The user just logged out.
  153. *
  154. * @param \Drupal\Core\Session\AccountInterface $account
  155. * The user object on which the operation was just performed.
  156. */
  157. function hook_user_logout(AccountInterface $account) {
  158. \Drupal::database()->insert('logouts')
  159. ->fields([
  160. 'uid' => $account->id(),
  161. 'time' => time(),
  162. ])
  163. ->execute();
  164. }
  165. /**
  166. * @} End of "addtogroup hooks".
  167. */