theme_username

5 theme.inc theme_username($object)
6 theme.inc theme_username($object)
7 theme.inc theme_username($variables)
8 user.module theme_username($variables)

Returns HTML for a username, potentially linked to the user's page.

Parameters

$variables: An associative array containing:

  • account: The user object to format.
  • name: The user's name, sanitized.
  • extra: Additional text to append to the user's name, sanitized.
  • link_path: The path or URL of the user's profile page, home page, or other desired page to link to for more information about the user.
  • link_options: An array of options to pass to the l() function's $options parameter if linking the user's name to the user's page.
  • attributes_array: An array of attributes to pass to the drupal_attributes() function if not linking to the user's page.

See also

template_preprocess_username()

template_process_username()

Related topics

23 theme calls to theme_username()

File

includes/theme.inc, line 2123
The theme system, which controls the output of Drupal.

Code

function theme_username($variables) {
  if (isset($variables['link_path'])) {
    // We have a link path, so we should generate a link using l().
    // Additional classes may be added as array elements like
    // $variables['link_options']['attributes']['class'][] = 'myclass';
    $output = l($variables['name'] . $variables['extra'], $variables['link_path'], $variables['link_options']);
  }
  else {
    // Modules may have added important attributes so they must be included
    // in the output. Additional classes may be added as array elements like
    // $variables['attributes_array']['class'][] = 'myclass';
    $output = '<span' . drupal_attributes($variables['attributes_array']) . '>' . $variables['name'] . $variables['extra'] . '</span>';
  }
  return $output;
}

Comments

This print the current logged

This print the current logged in user's with link:

<?php
global $user;
print
t('!username logged in', array('!username' => theme('username', array('account' => $user)))),
?>

$variables['name'] and $variables['extra'] do not work

Setting values into $variables['name'] or $variables['extra'] (array('name' => ..., 'extra' => ...) ) won't work under the default implementation, due to template_preprocess_username(). It has code:

$variables['extra'] = '';
...
$name = $variables['name_raw'] = format_username($account);
...
$variables['name'] = check_plain($name);

This code overrides anything set in $variables['name'] and $variables['extra'].

The only ways I can see to put in a custom name would be to either

  1. Remove the preprocess function from the registry so it doesn't run
  2. Change the name property on the account before sending it to $variables['account'];
  3. Make up some other properties in the $variables array (like 'name_real' and 'other_real') to send in a custom implementation of theme_username and use them.

None of these options are ideal.

Edit: $variables['link_path'] is also overridden.

If you just want custom

If you just want custom output then you need to override this function in your theme. I tried overriding the theme_username function and it works as expected.
Please refer http://drupal.org/node/173880 for more information.

My point is that, if using

My point is that, if using the theme_username's default implementation does not respect the values sent in the associative array. If I have

<?php
theme
('username', array('name' => 'Custom Name'));
?>
that "Custom Name" will not be displayed because the $variables array will be run through template_preprocess_username() and have its array's values will be altered. (Same with $variables['link_path'] and $variables['extra'].) Yes, I could make a custom theme override function for theme_username() and template_preprocess_username() and make it work, one way or another, but in my opinion, if the default implementation isn't going to work the way it is documented, either the function should be made so it does work, or the documentation should be updated to state that these values are set in the preprocess function, not by the associative array sent to the function.

Use hook_username_alter()

To change the default display of your user account, change it with hook_username_alter

Login or register to post comments