user.api.php 5.4 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, array('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, array('uid' => 0), NULL, TRUE);
57
      // Anonymize old revisions.
58
      db_update('node_field_revision')
59
        ->fields(array('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
97
98
99
100

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

  // Add a custom zero-out method.
  $methods['mymodule_zero_out'] = array(
    '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
 * @param string $name
113
 *   The string that $account->getDisplayName() will return.
114
 *
115
 * @param $account
116
 *   The account object the name belongs to.
117
 *
118
 * @see \Drupal\Core\Session\AccountInterface::getDisplayName()
119
120
121
 */
function hook_user_format_name_alter(&$name, $account) {
  // Display the user's uid instead of name.
122
  if ($account->id()) {
123
    $name = t('User @uid', array('@uid' => $account->id()));
124
125
126
  }
}

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

/**
 * 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')
    ->fields(array(
150
      'uid' => $account->id(),
151
152
153
154
155
      'time' => time(),
    ))
    ->execute();
}

156
157
158
/**
 * @} End of "addtogroup hooks".
 */