Why do we pass a node object into theme('username') ? (See arrow below)

I'm changing the site usernames with hook_username_alter(&$name, $account) and I was assuming that I would always be getting a user account here, not a node :/ I can't imagine I'm the first to identify this in D7, but I couldn't find anything in the core 7 issue queue. Seems like it should be loading the actual user and pass that as an argument. Is this a bug or just a badly documented feature/shortcut in the code?


function template_preprocess_node(&$variables) {
  $variables['view_mode'] = $variables['elements']['#view_mode'];
  // Provide a distinct $teaser boolean.
  $variables['teaser'] = $variables['view_mode'] == 'teaser';
  $variables['node'] = $variables['elements']['#node'];
  $node = $variables['node'];

  $variables['date']      = format_date($node->created);
  /*--->*/ $variables['name']      = theme('username', array('account' => $node));
   // ...

Comments

David_Rothstein’s picture

Version: 7.34 » 7.x-dev
Priority: Minor » Normal

It seems like a bug to me. However I am guessing this was done at some point for performance reasons (to avoid loading all those user account objects when they usually aren't needed) so I wonder what the effect on performance would be if this is fixed.

naushunaushad’s picture

I noticed some other cases are also using the same thing.

function comment_admin_overview($form, &$form_state, $arg):--comment.admin.inc
'author' => theme('username', array('account' => $comment))

function template_preprocess_comment(&$variables):--comment.module
$variables['author'] = theme('username', array('account' => $comment))

function dblog_overview():-dblog.admin.inc
theme('username', array('account' => $dblog))

I think theme('username') is calling for theme a user name. All those objects are enough for this.

But for avoiding uncertainty I think it is better to use user_load_by_name($name) for getting user object.

Status: Active » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.