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
Comment #1
David_Rothstein commentedIt 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.
Comment #2
naushunaushad commentedI 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.