2015-08-18 00:00:26 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Hooks provided by the User module.
|
|
|
|
*/
|
|
|
|
|
2018-11-23 12:29:20 +00:00
|
|
|
use Drupal\Core\Session\AccountInterface;
|
|
|
|
use Drupal\user\UserInterface;
|
|
|
|
|
2015-08-18 00:00:26 +00:00
|
|
|
/**
|
|
|
|
* @addtogroup hooks
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Act on user account cancellations.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* method. To react to that method, implement hook_ENTITY_TYPE_predelete() or
|
|
|
|
* hook_ENTITY_TYPE_delete() for user entities instead.
|
|
|
|
*
|
|
|
|
* Expensive operations should be added to the global account cancellation batch
|
|
|
|
* by using batch_set().
|
|
|
|
*
|
|
|
|
* @param array $edit
|
|
|
|
* The array of form values submitted by the user.
|
2018-11-23 12:29:20 +00:00
|
|
|
* @param \Drupal\user\UserInterface $account
|
2015-08-18 00:00:26 +00:00
|
|
|
* The user object on which the operation is being performed.
|
|
|
|
* @param string $method
|
|
|
|
* The account cancellation method.
|
|
|
|
*
|
|
|
|
* @see user_cancel_methods()
|
|
|
|
* @see hook_user_cancel_methods_alter()
|
|
|
|
*/
|
2018-11-23 12:29:20 +00:00
|
|
|
function hook_user_cancel($edit, UserInterface $account, $method) {
|
2015-08-18 00:00:26 +00:00
|
|
|
switch ($method) {
|
|
|
|
case 'user_cancel_block_unpublish':
|
|
|
|
// Unpublish nodes (current revisions).
|
|
|
|
module_load_include('inc', 'node', 'node.admin');
|
|
|
|
$nodes = \Drupal::entityQuery('node')
|
2015-10-22 04:44:50 +00:00
|
|
|
->condition('uid', $account->id())
|
2015-08-18 00:00:26 +00:00
|
|
|
->execute();
|
2017-04-13 14:53:35 +00:00
|
|
|
node_mass_update($nodes, ['status' => 0], NULL, TRUE);
|
2015-08-18 00:00:26 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'user_cancel_reassign':
|
|
|
|
// Anonymize nodes (current revisions).
|
|
|
|
module_load_include('inc', 'node', 'node.admin');
|
|
|
|
$nodes = \Drupal::entityQuery('node')
|
2015-10-22 04:44:50 +00:00
|
|
|
->condition('uid', $account->id())
|
2015-08-18 00:00:26 +00:00
|
|
|
->execute();
|
2017-04-13 14:53:35 +00:00
|
|
|
node_mass_update($nodes, ['uid' => 0], NULL, TRUE);
|
2015-08-18 00:00:26 +00:00
|
|
|
// Anonymize old revisions.
|
|
|
|
db_update('node_field_revision')
|
2017-04-13 14:53:35 +00:00
|
|
|
->fields(['uid' => 0])
|
2015-08-18 00:00:26 +00:00
|
|
|
->condition('uid', $account->id())
|
|
|
|
->execute();
|
|
|
|
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
|
|
|
|
* a method. If 'access' is defined, the method cannot be configured as
|
|
|
|
* default method.
|
|
|
|
*
|
|
|
|
* @param array $methods
|
|
|
|
* An array containing user account cancellation methods, keyed by method id.
|
|
|
|
*
|
|
|
|
* @see user_cancel_methods()
|
|
|
|
* @see \Drupal\user\Form\UserCancelForm
|
|
|
|
*/
|
|
|
|
function hook_user_cancel_methods_alter(&$methods) {
|
|
|
|
$account = \Drupal::currentUser();
|
|
|
|
// Limit access to disable account and unpublish content method.
|
|
|
|
$methods['user_cancel_block_unpublish']['access'] = $account->hasPermission('administer site configuration');
|
|
|
|
|
|
|
|
// Remove the content re-assigning method.
|
|
|
|
unset($methods['user_cancel_reassign']);
|
|
|
|
|
|
|
|
// Add a custom zero-out method.
|
2017-04-13 14:53:35 +00:00
|
|
|
$methods['mymodule_zero_out'] = [
|
2015-08-18 00:00:26 +00:00
|
|
|
'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.
|
|
|
|
'access' => $account->hasPermission('access zero-out account cancellation method'),
|
2017-04-13 14:53:35 +00:00
|
|
|
];
|
2015-08-18 00:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Alter the username that is displayed for a user.
|
|
|
|
*
|
2015-10-08 18:40:12 +00:00
|
|
|
* Called by $account->getDisplayName() to allow modules to alter the username
|
|
|
|
* that is displayed. Can be used to ensure user privacy in situations where
|
2019-01-24 08:00:03 +00:00
|
|
|
* $account->getDisplayName() is too revealing. This hook is invoked both for
|
|
|
|
* user entities and the anonymous user session object.
|
2015-08-18 00:00:26 +00:00
|
|
|
*
|
2017-02-03 00:28:38 +00:00
|
|
|
* @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
|
2019-01-24 08:00:03 +00:00
|
|
|
* The object on which the operation is being performed. This object may be a
|
|
|
|
* user entity. If the object is an implementation of UserInterface you can
|
|
|
|
* use instanceof operator before accessing user entity methods. For example:
|
|
|
|
* @code
|
|
|
|
* if ($account instanceof UserInterface) {
|
|
|
|
* // Access user entity methods.
|
|
|
|
* }
|
|
|
|
* @endcode
|
2015-08-18 00:00:26 +00:00
|
|
|
*
|
2016-09-07 20:26:21 +00:00
|
|
|
* @see \Drupal\Core\Session\AccountInterface::getDisplayName()
|
2017-02-03 00:28:38 +00:00
|
|
|
* @see sanitization
|
2015-08-18 00:00:26 +00:00
|
|
|
*/
|
2018-11-23 12:29:20 +00:00
|
|
|
function hook_user_format_name_alter(&$name, AccountInterface $account) {
|
2015-08-18 00:00:26 +00:00
|
|
|
// Display the user's uid instead of name.
|
|
|
|
if ($account->id()) {
|
2017-04-13 14:53:35 +00:00
|
|
|
$name = t('User @uid', ['@uid' => $account->id()]);
|
2015-08-18 00:00:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The user just logged in.
|
|
|
|
*
|
2018-11-23 12:29:20 +00:00
|
|
|
* @param \Drupal\user\UserInterface $account
|
2015-08-18 00:00:26 +00:00
|
|
|
* The user object on which the operation was just performed.
|
|
|
|
*/
|
2018-11-23 12:29:20 +00:00
|
|
|
function hook_user_login(UserInterface $account) {
|
2015-08-18 00:00:26 +00:00
|
|
|
$config = \Drupal::config('system.date');
|
|
|
|
// If the user has a NULL time zone, notify them to set a time zone.
|
|
|
|
if (!$account->getTimezone() && $config->get('timezone.user.configurable') && $config->get('timezone.user.warn')) {
|
2018-11-23 12:29:20 +00:00
|
|
|
\Drupal::messenger()
|
|
|
|
->addStatus(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',
|
|
|
|
]),
|
|
|
|
]));
|
2015-08-18 00:00:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The user just logged out.
|
|
|
|
*
|
2018-11-23 12:29:20 +00:00
|
|
|
* @param \Drupal\Core\Session\AccountInterface $account
|
2015-08-18 00:00:26 +00:00
|
|
|
* The user object on which the operation was just performed.
|
|
|
|
*/
|
2018-11-23 12:29:20 +00:00
|
|
|
function hook_user_logout(AccountInterface $account) {
|
2015-08-18 00:00:26 +00:00
|
|
|
db_insert('logouts')
|
2017-04-13 14:53:35 +00:00
|
|
|
->fields([
|
2015-08-18 00:00:26 +00:00
|
|
|
'uid' => $account->id(),
|
|
|
|
'time' => time(),
|
2017-04-13 14:53:35 +00:00
|
|
|
])
|
2015-08-18 00:00:26 +00:00
|
|
|
->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @} End of "addtogroup hooks".
|
|
|
|
*/
|