<?php
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_delete($account) {
db_delete('mytable')
->condition('uid', $account->uid)
->execute();
}
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();
db_delete('history')
->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_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_categories() {
return array(
array(
'name' => 'account',
'title' => t('Account settings'),
'weight' => 1,
),
);
}
function hook_user_presave(&$edit, $account, $category) {
if (isset($edit['mymodule_foo'])) {
$edit['data']['mymodule_bar'] = $edit['mymodule_foo'];
}
}
function hook_user_insert(&$edit, $account, $category) {
db_insert('mytable')
->fields(array(
'myfield' => $edit['myfield'],
'uid' => $account->uid,
))
->execute();
}
function hook_user_update(&$edit, $account, $category) {
db_insert('user_changes')
->fields(array(
'uid' => $account->uid,
'changed' => time(),
))
->execute();
}
function hook_user_login(&$edit, $account) {
if (!$account->timezone && variable_get('configurable_timezones', 1) && variable_get('empty_timezone_message', 0)) {
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 (user_access('create blog content', $account)) {
$account->content['summary']['blog'] = array(
'#type' => 'user_profile_item',
'#title' => t('Blog'),
'#markup' => l(t('View recent blog entries'), "blog/{$account->uid}", array(
'attributes' => array(
'title' => t("Read !username's latest blog entries.", array(
'!username' => format_username($account),
)),
),
)),
'#attributes' => array(
'class' => array(
'blog',
),
),
);
}
}
function hook_user_view_alter(&$build) {
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();
}