You are here

function realname_update in Real Name 7

Same name and namespace in other branches
  1. 8 realname.module \realname_update()
  2. 2.x realname.module \realname_update()

Update the realname for a user account.

Parameters

object $account: A user account object.

Return value

string A string with the real name.

See also

hook_realname_pattern_alter()

hook_realname_alter()

hook_realname_update()

Related topics

5 calls to realname_update()
realname_action_realname_update in ./realname.module
Action callback to update a user's realname.
realname_load_multiple in ./realname.module
Loads multiple real names.
realname_profile2_update in ./realname.module
Implements hook_profile2_update().
realname_user_operations_realname_update in ./realname.module
Callback function for admin mass generating user real names.
realname_user_update in ./realname.module
Implements hook_user_update().

File

./realname.module, line 320
Provides token-based name displays for users.

Code

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,
    'sanitize' => FALSE,
  ));

  // Remove any HTML tags.
  $realname = strip_tags(decode_entities($realname));

  // Remove double spaces (if a token had no value).
  $realname = preg_replace('/ {2,}/', ' ', $realname);

  // Allow other modules to alter the generated realname.
  drupal_alter('realname', $realname, $account);

  // The name must be trimmed to 255 characters before inserting into the
  // database.
  $realname = truncate_utf8(trim($realname), 255);

  // 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);

  // Only if the real name is a non-empty string the name is altered.
  if (drupal_strlen($realname)) {
    $account->realname = $realname;
  }

  // Clear the entity cache.
  entity_get_controller('user')
    ->resetCache(array(
    $account->uid,
  ));
  return $realname;
}