user.api.php 5.68 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
<?php

/**
 * @file
 * Hooks provided by the User module.
 */

/**
 * @addtogroup hooks
 * @{
 */

13 14 15
/**
 * Act on user account cancellations.
 *
16 17 18 19 20 21 22
 * This hook is invoked from user_cancel() before a user account is canceled.
 * Depending on the account cancellation method, the module should either do
 * nothing, unpublish content, or anonymize content. See user_cancel_methods()
 * for the list of default account cancellation methods provided by User module.
 * Modules may add further methods via hook_user_cancel_methods_alter().
 *
 * This hook is NOT invoked for the 'user_cancel_delete' account cancellation
23 24
 * method. To react to that method, implement hook_ENTITY_TYPE_predelete() or
 * hook_ENTITY_TYPE_delete() for user entities instead.
25
 *
26 27
 * Expensive operations should be added to the global account cancellation batch
 * by using batch_set().
28
 *
29
 * @param array $edit
30
 *   The array of form values submitted by the user.
31
 * @param \Drupal\Core\Session\AccountInterface $account
32
 *   The user object on which the operation is being performed.
33
 * @param string $method
34 35 36 37 38
 *   The account cancellation method.
 *
 * @see user_cancel_methods()
 * @see hook_user_cancel_methods_alter()
 */
39
function hook_user_cancel($edit, $account, $method) {
40 41 42 43
  switch ($method) {
    case 'user_cancel_block_unpublish':
      // Unpublish nodes (current revisions).
      module_load_include('inc', 'node', 'node.admin');
44
      $nodes = \Drupal::entityQuery('node')
45
        ->condition('uid', $account->id())
46
        ->execute();
47
      node_mass_update($nodes, ['status' => 0], NULL, TRUE);
48 49 50 51 52
      break;

    case 'user_cancel_reassign':
      // Anonymize nodes (current revisions).
      module_load_include('inc', 'node', 'node.admin');
53
      $nodes = \Drupal::entityQuery('node')
54
        ->condition('uid', $account->id())
55
        ->execute();
56
      node_mass_update($nodes, ['uid' => 0], NULL, TRUE);
57
      // Anonymize old revisions.
58
      db_update('node_field_revision')
59
        ->fields(['uid' => 0])
60
        ->condition('uid', $account->id())
61
        ->execute();
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
      break;
  }
}

/**
 * Modify account cancellation methods.
 *
 * By implementing this hook, modules are able to add, customize, or remove
 * account cancellation methods. All defined methods are turned into radio
 * button form elements by user_cancel_methods() after this hook is invoked.
 * The following properties can be defined for each method:
 * - title: The radio button's title.
 * - description: (optional) A description to display on the confirmation form
 *   if the user is not allowed to select the account cancellation method. The
 *   description is NOT used for the radio button, but instead should provide
 *   additional explanation to the user seeking to cancel their account.
 * - access: (optional) A boolean value indicating whether the user can access
79 80
 *   a method. If 'access' is defined, the method cannot be configured as
 *   default method.
81
 *
82
 * @param array $methods
83 84 85
 *   An array containing user account cancellation methods, keyed by method id.
 *
 * @see user_cancel_methods()
86
 * @see \Drupal\user\Form\UserCancelForm
87 88
 */
function hook_user_cancel_methods_alter(&$methods) {
89
  $account = \Drupal::currentUser();
90
  // Limit access to disable account and unpublish content method.
91
  $methods['user_cancel_block_unpublish']['access'] = $account->hasPermission('administer site configuration');
92 93 94 95 96

  // Remove the content re-assigning method.
  unset($methods['user_cancel_reassign']);

  // Add a custom zero-out method.
97
  $methods['mymodule_zero_out'] = [
98 99 100
    'title' => t('Delete the account and remove all content.'),
    'description' => t('All your content will be replaced by empty strings.'),
    // access should be used for administrative methods only.
101
    'access' => $account->hasPermission('access zero-out account cancellation method'),
102
  ];
103 104
}

105 106 107
/**
 * Alter the username that is displayed for a user.
 *
108 109
 * Called by $account->getDisplayName() to allow modules to alter the username
 * that is displayed. Can be used to ensure user privacy in situations where
110
 * $account->getDisplayName() is too revealing.
111
 *
112 113 114 115 116 117 118
 * @param string|Drupal\Component\Render\MarkupInterface $name
 *   The username that is displayed for a user. If a hook implementation changes
 *   this to an object implementing MarkupInterface it is the responsibility of
 *   the implementation to ensure the user's name is escaped properly. String
 *   values will be autoescaped.
 * @param \Drupal\Core\Session\AccountInterface $account
 *   The user object on which the operation is being performed.
119
 *
120
 * @see \Drupal\Core\Session\AccountInterface::getDisplayName()
121
 * @see sanitization
122 123 124
 */
function hook_user_format_name_alter(&$name, $account) {
  // Display the user's uid instead of name.
125
  if ($account->id()) {
126
    $name = t('User @uid', ['@uid' => $account->id()]);
127 128 129
  }
}

130 131 132
/**
 * The user just logged in.
 *
133
 * @param object $account
134 135
 *   The user object on which the operation was just performed.
 */
136
function hook_user_login($account) {
137
  $config = \Drupal::config('system.date');
138
  // If the user has a NULL time zone, notify them to set a time zone.
139
  if (!$account->getTimezone() && $config->get('timezone.user.configurable') && $config->get('timezone.user.warn')) {
140
    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'])]));
141 142 143 144 145 146 147 148 149 150 151
  }
}

/**
 * The user just logged out.
 *
 * @param $account
 *   The user object on which the operation was just performed.
 */
function hook_user_logout($account) {
  db_insert('logouts')
152
    ->fields([
153
      'uid' => $account->id(),
154
      'time' => time(),
155
    ])
156 157 158
    ->execute();
}

159 160 161
/**
 * @} End of "addtogroup hooks".
 */