3

I use this theme function to show the username of user:

 theme('username', array('account' => user_load($record->uid)))),

It works fine but the username is also a link to the user page. I just need to the username with no link at all.

I read the doc at:

http://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_username/7

and I see that there is the argument link_options to handle this, then I guessed it was a bolean value and tried with (but not working):

theme('username', array('account' => user_load($record->uid), 'link_options' => 0))),

2 Answers 2

3

theme_username() will only output the username as a link if the link_path variable has been set. This is done by default in template_preprocess_username(), and will happen as long as the user viewing the username has access to view the profile.

If you want to change that behaviour you just need to add your own preprocess function which removes the link_path variable:

function MYTHEME_preprocess_username(&$vars) {
  if (isset($vars['link_path'])) {
    unset($vars['link_path']);
  }
}

Bear in mind that the above will remove the link system wide. If you want to remove just for this specific instance, I think the best way would be to pass a custom variable to the theme function and check for it in your preprocess function:

$vars = array('account' = user_load($record->uid), 'surpress_link' => TRUE);
$name = theme('username', $vars);

function MYTHEME_preprocess_username(&$vars) {
  if (isset($vars['link_path']) && isset($vars['surpress_link']) && $vars['surpress_link']) {
    unset($vars['link_path']);
  }
}
1
  • good, preprocess function in my theme fitted my needs. Thanks.
    – chefnelone
    Jun 5, 2012 at 10:44
2

Depending on what you want to do exactly, there's also format_username() that allows you to return a properly formatted username (e.g. integrated with realname.module), without any kind of link/HTML.

Note that this function returns an unsafe string (as unsafe as $user->name is), so you need to check_plain() it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.