/ Published in: PHP
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.
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.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/** * Implements theme_preprocess_username(). */ function sky_preprocess_username(&$variables) { $variables['extra'] = ''; $account = $variables['account']; $variables['uid'] = 0; if (theme_get_setting('toggle_comment_user_verification')) { $variables['extra'] = ' (' . t('not verified') . ')'; } } else { $variables['uid'] = (int) $account->uid; $user = user_load($account->uid); $name = $variables['name_raw'] = $user->field_displayname ['und'][0]['safe_value']; } else { // Set the name to a formatted name that is safe for printing and // that won't break tables by being too long. Keep an unshortened, // unsanitized version, in case other preprocess functions want to implement // their own shortening logic or add markup. If they do so, they must ensure // that $variables['name'] is safe for printing. $name = $variables['name_raw'] = format_username($account); } } if ( drupal_strlen($name) > 20) { $name = drupal_substr($name, 0, 15) . '...'; } $variables['name'] = check_plain($name); $variables['profile_access'] = user_access('access user profiles'); // Populate link path and attributes if appropriate. if ($variables['uid'] && $variables['profile_access']) { // We are linking to a local user. $variables['link_path'] = 'user/' . $variables['uid']; } // Like the 'class' attribute, the 'rel' attribute can hold a // space-separated set of values, so initialize it as an array to make it // easier for other preprocess functions to append to it. $variables['link_path'] = $account->homepage; $variables['homepage'] = $account->homepage; } // We do not want the l() function to check_plain() a second time. $variables['link_options']['html'] = TRUE; // Set a default class. } ?>
URL: http://opendrops.com/blog/how-show-real-names-drupal-7