How to show Real Names in Drupal 7


/ Published in: PHP
Save to your folder(s)

By Shankar Dhanasekaran
On opendrops.com, I wanted to show myreal name for all my posts in the submitted information instead of my username. In D6, one could do this with the wonderful module RealName. However, there is no D7 version as of now. Nevertheless, the logic behind showing the realname is not so much complicated. Here is how I did it.

1. I created a new field for user object with the system name real_name.
2. Used the below code in my theme's template.php. (My theme name is krish, and you would want to change the function name below to indicate your themename, otherwise, this wont work.)
3. Clear cache.


Copy this code and paste it in your HTML
  1. /**
  2. * Implements theme_preprocess_username().
  3. */
  4. function sky_preprocess_username(&$variables) {
  5. $variables['extra'] = '';
  6. $account = $variables['account'];
  7. if (empty($account->uid)) {
  8. $variables['uid'] = 0;
  9. if (theme_get_setting('toggle_comment_user_verification')) {
  10. $variables['extra'] = ' (' . t('not verified') . ')';
  11. }
  12. }
  13. else {
  14. $variables['uid'] = (int) $account->uid;
  15. $user = user_load($account->uid);
  16. if (isset($user->field_displayname ['und'][0]['value'])) {
  17. $name = $variables['name_raw'] = $user->field_displayname ['und'][0]['safe_value'];
  18. } else {
  19. // Set the name to a formatted name that is safe for printing and
  20. // that won't break tables by being too long. Keep an unshortened,
  21. // unsanitized version, in case other preprocess functions want to implement
  22. // their own shortening logic or add markup. If they do so, they must ensure
  23. // that $variables['name'] is safe for printing.
  24. $name = $variables['name_raw'] = format_username($account);
  25. }
  26. }
  27. if (
  28.  
  29. drupal_strlen($name) > 20) {
  30. $name = drupal_substr($name, 0, 15) . '...';
  31. }
  32. $variables['name'] = check_plain($name);
  33.  
  34.  
  35. $variables['profile_access'] = user_access('access user profiles');
  36. $variables['link_attributes'] = array();
  37. // Populate link path and attributes if appropriate.
  38. if ($variables['uid'] && $variables['profile_access']) {
  39. // We are linking to a local user.
  40. $variables['link_attributes'] = array('title' => t('View user profile.'));
  41. $variables['link_path'] = 'user/' . $variables['uid'];
  42. }
  43. elseif (!empty($account->homepage)) {
  44. // Like the 'class' attribute, the 'rel' attribute can hold a
  45. // space-separated set of values, so initialize it as an array to make it
  46. // easier for other preprocess functions to append to it.
  47. $variables['link_attributes'] = array('rel' => array('nofollow'));
  48. $variables['link_path'] = $account->homepage;
  49. $variables['homepage'] = $account->homepage;
  50. }
  51. // We do not want the l() function to check_plain() a second time.
  52. $variables['link_options']['html'] = TRUE;
  53. // Set a default class.
  54. $variables['attributes_array'] = array('class' => array('username'));
  55. }
  56. ?>

URL: http://opendrops.com/blog/how-show-real-names-drupal-7

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.