Commit ec5d43ad authored by Dave Reid's avatar Dave Reid
Browse files

by Dave Reid: Added an 'Update real name' user bulk operation

by Dave Reid: Split out real name generation from realname_load_multiple() into realname_update().
parent 4933177b
<?php
/**
* @file
* Hooks provided by the Real name module.
*/
/**
* @addtogroup hooks
* @{
*/
/**
* Alter the pattern for a user's real name prior to generation.
*
* @param $pattern
* The real name pattern string prior to token replacement.
* @param $account
* A user account object.
*
* @see realname_update()
*
* @ingroup realname
*/
function hook_realname_pattern_alter(&$pattern, $account) {
}
function hook_realname_clear($account = NULL) {
/**
* Alter a user's real name before it is saved to the database.
*
* @param $realname
* The user's generated real name.
* @param $account
* A user account object.
*
* @see realname_update()
*
* @ingroup realname
*/
function hook_realname_alter(&$realname, $account) {
}
/**
* Respond to updates to an account's real name.
*
* @see realname_update()
*
* @ingroup realname
*/
function hook_realname_update($realname, $account) {
}
}
\ No newline at end of file
/**
* @} End of "addtogroup hooks".
*/
<?php
// @todo Alter the username autocomplete
// @todo Allow users to login with their real name
// @todo Disable the username field
// @todo Allow users to override their real name
/**
* @file
* Provides token-based name displays for users.
*
* @todo Alter the username autocomplete
* @todo Allow users to login with their real name
* @todo Disable the username field
* @todo Allow users to override their real name
*/
/**
* @defgroup realname Real name API
*/
/**
* Implements hook_permission().
......@@ -75,6 +84,35 @@ function realname_user_delete(stdClass $account) {
realname_delete($account->uid);
}
/**
* Implements hook_user_operations().
*/
function realname_user_operations() {
$operations['realname_update'] = array(
'label' => t('Update real name'),
'callback' => 'realname_user_operations_realname_update',
);
return $operations;
}
/**
* Callback function for admin mass generating user real names.
*
* @param $uids
* An array of user IDs.
*/
function realname_user_operations_realname_update(array $uids) {
$accounts = user_load_multiple($uids);
foreach ($accounts as $account) {
realname_update($account);
}
}
/**
* @addtogroup realname
* @{
*/
/**
* Loads a real name.
*
......@@ -107,32 +145,52 @@ function realname_load_multiple(array $accounts) {
// real name.
foreach ($new_accounts as $uid => $account) {
if (!isset($realnames[$uid]) || !drupal_strlen($realnames[$uid])) {
// Get the default pattern and allow other modules to alter it.
$pattern = variable_get('realname_pattern', '[user:name-raw]');
drupal_alter('realname_pattern', $pattern, $account);
// Perform token replacement on the real name pattern.
$realname = token_replace($pattern, array('user' => $account), array('clear' => TRUE));
$realname = trim($realname);
module_invoke_all('realname_load', $realname, $account);
// Save to the database and the static cache.
db_merge('realname')
->key(array('uid' => $uid))
->fields(array(
'realname' => $realname,
'created' => REQUEST_TIME,
))
->execute();
$realnames[$uid] = $realname;
$realnames[$uid] = realname_update($account);
}
}
}
return array_intersect_key($realnames, $accounts);
}
/**
* Update the realname for a user account.
*
* @param $account
* A user account object.
*
* @see hook_realname_pattern_alter()
* @see hook_realname_alter()
* @see hook_realname_update()
*/
function realname_update($account) {
// Get the default pattern and allow other modules to alter it.
$pattern = variable_get('realname_pattern', '[user:name-raw]');
drupal_alter('realname_pattern', $pattern, $account);
// Perform token replacement on the real name pattern.
$realname = token_replace($pattern, array('user' => $account), array('clear' => TRUE));
$realname = trim($realname);
// Allow other modules to alter the generated realname.
drupal_alter('realname', $realname, $account);
// Save to the database and the static cache.
db_merge('realname')
->key(array('uid' => $account->uid))
->fields(array(
'realname' => $realname,
'created' => REQUEST_TIME,
))
->execute();
// Allow modules to react to the realname being updated.
module_invoke_all('realname_update', $realname, $account);
$account->realname = $realname;
return $realname;
}
/**
* Delete a real name.
*
......@@ -161,3 +219,7 @@ function realname_delete_all() {
db_delete('realname')->execute();
drupal_static_reset('realname_load_multiple');
}
/**
* @} End of "addtogroup realname".
*/
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment