user.api.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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, ['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, ['uid' => 0], NULL, TRUE);
  53. // Anonymize old revisions.
  54. db_update('node_field_revision')
  55. ->fields(['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'] = [
  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|Drupal\Component\Render\MarkupInterface $name
  105. * The username that is displayed for a user. If a hook implementation changes
  106. * this to an object implementing MarkupInterface it is the responsibility of
  107. * the implementation to ensure the user's name is escaped properly. String
  108. * values will be autoescaped.
  109. * @param \Drupal\Core\Session\AccountInterface $account
  110. * The user object on which the operation is being performed.
  111. *
  112. * @see \Drupal\Core\Session\AccountInterface::getDisplayName()
  113. * @see sanitization
  114. */
  115. function hook_user_format_name_alter(&$name, $account) {
  116. // Display the user's uid instead of name.
  117. if ($account->id()) {
  118. $name = t('User @uid', ['@uid' => $account->id()]);
  119. }
  120. }
  121. /**
  122. * The user just logged in.
  123. *
  124. * @param object $account
  125. * The user object on which the operation was just performed.
  126. */
  127. function hook_user_login($account) {
  128. $config = \Drupal::config('system.date');
  129. // If the user has a NULL time zone, notify them to set a time zone.
  130. if (!$account->getTimezone() && $config->get('timezone.user.configurable') && $config->get('timezone.user.warn')) {
  131. 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'])]));
  132. }
  133. }
  134. /**
  135. * The user just logged out.
  136. *
  137. * @param $account
  138. * The user object on which the operation was just performed.
  139. */
  140. function hook_user_logout($account) {
  141. db_insert('logouts')
  142. ->fields([
  143. 'uid' => $account->id(),
  144. 'time' => time(),
  145. ])
  146. ->execute();
  147. }
  148. /**
  149. * @} End of "addtogroup hooks".
  150. */