Example of how to theme the "username" in Drupal 7.

Public

Warning message

We recommend you to assign at least one category to this snippet so you could easily navigate it in the future. Edit your snippet here.

Get raw version
php
  1. <?php
  2.  
  3. # .tpl.php
  4.  
  5. $username = theme('username', array('account' => $author, 'type' => 'button'));
  6.  
  7.  
  8. # template.php
  9.  
  10. function mytheme_username($variables) {
  11. if (isset($variables['link_path'])) {
  12. // We have a link path, so we should generate a link using l().
  13. // Additional classes may be added as array elements like
  14. // $variables['link_options']['attributes']['class'][] = 'myclass';
  15. if (isset($variables['type'])) {
  16. switch ($variables['type']) {
  17. case 'button' :
  18. $variables['prefix']['icon'] = '<i class="icon-user"></i> ';
  19. $variables['link_options']['attributes']['class'][] = 'button';
  20. $output = l($variables['prefix']['icon'] . $variables['name'] . $variables['extra'], $variables['link_path'], $variables['link_options']);
  21. break;
  22. }
  23. }
  24. else {
  25. $output = l($variables['name'] . $variables['extra'], $variables['link_path'], $variables['link_options']);
  26. }
  27. }
  28. else {
  29. // Modules may have added important attributes so they must be included
  30. // in the output. Additional classes may be added as array elements like
  31. // $variables['attributes_array']['class'][] = 'myclass';
  32. $variables['attributes_array']['class'][] = 'button strapped';
  33. $output = '<span' . drupal_attributes($variables['attributes_array']) . '><i class="icon-user"></i> ' . $variables['name'] . $variables['extra'] . '</span>';
  34. }
  35. return $output;
  36. }
  37.  
  38.  
  39. ## what was overriden ..
  40.  
  41. function theme_username($variables) {
  42. if (isset($variables['link_path'])) {
  43. // We have a link path, so we should generate a link using l().
  44. // Additional classes may be added as array elements like
  45. // $variables['link_options']['attributes']['class'][] = 'myclass';
  46. $output = l($variables['name'] . $variables['extra'], $variables['link_path'], $variables['link_options']);
  47. }
  48. else {
  49. // Modules may have added important attributes so they must be included
  50. // in the output. Additional classes may be added as array elements like
  51. // $variables['attributes_array']['class'][] = 'myclass';
  52. $output = '<span' . drupal_attributes($variables['attributes_array']) . '>' . $variables['name'] . $variables['extra'] . '</span>';
  53. }
  54. return $output;
  55. }