<?php
use Drupal\Core\Entity\EntityInterface;
function hook_user_load($users) {
$result = db_query('SELECT uid, foo FROM {my_table} WHERE uid IN (:uids)', array(
':uids' => array_keys($users),
));
foreach ($result as $record) {
$users[$record->uid]->foo = $record->foo;
}
}
function hook_user_predelete($account) {
db_delete('mytable')
->condition('uid', $account->uid)
->execute();
}
function hook_user_delete($account) {
drupal_set_message(t('User: @name has been deleted.', array(
'@name' => $account->name,
)));
}
function hook_user_cancel($edit, $account, $method) {
switch ($method) {
case 'user_cancel_block_unpublish':
module_load_include('inc', 'node', 'node.admin');
$nodes = db_select('node', 'n')
->fields('n', array(
'nid',
))
->condition('uid', $account->uid)
->execute()
->fetchCol();
node_mass_update($nodes, array(
'status' => 0,
));
break;
case 'user_cancel_reassign':
module_load_include('inc', 'node', 'node.admin');
$nodes = db_select('node', 'n')
->fields('n', array(
'nid',
))
->condition('uid', $account->uid)
->execute()
->fetchCol();
node_mass_update($nodes, array(
'uid' => 0,
));
db_update('node_revision')
->fields(array(
'uid' => 0,
))
->condition('uid', $account->uid)
->execute();
break;
}
}
function hook_user_cancel_methods_alter(&$methods) {
$methods['user_cancel_block_unpublish']['access'] = user_access('administer site configuration');
unset($methods['user_cancel_reassign']);
$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' => user_access('access zero-out account cancellation method'),
);
}
function hook_user_format_name_alter(&$name, $account) {
if (isset($account->uid)) {
$name = t('User !uid', array(
'!uid' => $account->uid,
));
}
}
function hook_user_operations() {
$operations = array(
'unblock' => array(
'label' => t('Unblock the selected users'),
'callback' => 'user_user_operations_unblock',
),
'block' => array(
'label' => t('Block the selected users'),
'callback' => 'user_user_operations_block',
),
'cancel' => array(
'label' => t('Cancel the selected user accounts'),
),
);
return $operations;
}
function hook_user_presave($account) {
if (isset($account->mymodule_foo)) {
$account->mymodule_foo = (array) $account->mymodule_foo;
}
}
function hook_user_insert($account) {
db_insert('user_changes')
->fields(array(
'uid' => $account->uid,
'created' => time(),
))
->execute();
}
function hook_user_update($account) {
db_insert('user_changes')
->fields(array(
'uid' => $account->uid,
'changed' => time(),
))
->execute();
}
function hook_user_login(&$edit, $account) {
$config = config('system.date');
if (!$account->timezone && $config
->get('timezone.user.configurable') && $config
->get('timezone.user.warn')) {
drupal_set_message(t('Configure your <a href="@user-edit">account time zone setting</a>.', array(
'@user-edit' => url("user/{$account->uid}/edit", array(
'query' => drupal_get_destination(),
'fragment' => 'edit-timezone',
)),
)));
}
}
function hook_user_logout($account) {
db_insert('logouts')
->fields(array(
'uid' => $account->uid,
'time' => time(),
))
->execute();
}
function hook_user_view($account, $view_mode, $langcode) {
if (!isset($account->content['summary'])) {
$account->content['summary'] = array();
}
$account->content['summary'] += array(
'#type' => 'user_profile_category',
'#title' => t('History'),
'#attributes' => array(
'class' => array(
'user-member',
),
),
'#weight' => 5,
);
$account->content['summary']['member_for'] = array(
'#type' => 'user_profile_item',
'#title' => t('Member for'),
'#markup' => format_interval(REQUEST_TIME - $account->created),
);
}
function hook_user_view_alter(&$build, Drupal\user\User $account) {
if (isset($build['an_additional_field'])) {
$build['an_additional_field']['#weight'] = -10;
}
$build['#post_render'][] = 'my_module_user_post_render';
}
function hook_user_role_presave($role) {
if (empty($role->uuid)) {
$role->uuid = uuid_uuid();
}
}
function hook_user_role_insert($role) {
db_insert('my_module_table')
->fields(array(
'rid' => $role->rid,
'role_description' => $role->description,
))
->execute();
}
function hook_user_role_update($role) {
db_merge('my_module_table')
->key(array(
'rid' => $role->rid,
))
->fields(array(
'role_description' => $role->description,
))
->execute();
}
function hook_user_role_delete($role) {
db_delete('my_module_table')
->condition('rid', $role->rid)
->execute();
}