Display user email on the user profile view page to administrator users

  • 9 May 2013
  • mohit.aghera


Recently working with administrator account i encountered a problem. When i visit the profile of other user, i was not able to see the email id of the registered user.
To view the user email id either i have to goto the user profile and then click "Edit" tab for that account that will show the account for the user.
Now i wanted to show the email id of the register user to be displayed when administrator views the profile.
After little bit googling  i found a hook that can alter the view of the user profile
As we can see the description of  hook_user_view() this hook is called when user's account information is being displayed.
The module should format its custom additions for display and add them to the $account->content array.
So i created a hook for the module and added code snippet to display the account email id.
function modulealter_user_view($account, $view_mode, $langcode) {
  global $user;
  // Only administrators can see the email id of other users. 
  // Checking whether logged in user is adminstrator or not
  if (in_array('administrator', $user->roles)) {
    $account->content['e-mail'] = array(
       '#type' => 'user_profile_category',
       '#title' => t('User e-mail address'),
       '#weight' => -9,
    $account->content['e-mail']['email-id'] = array(
      '#type' => 'user_profile_item',
      '#title' => t('E-mail'),
      '#markup' => $account->mail, // print the email id in the markup, so while rendering the content it will be displayed.




First of all I would like to say fantastic blog!

I had a quick question in which I'd like to ask if you do not mind. I was curious to know how you center yourself and clear your thoughts before writing. I have had a difficult time clearing my mind in getting my thoughts out there. I do take pleasure in writing however it just seems like the first 10 to 15 minutes are wasted just trying to figure out how to begin. Any recommendations or hints? Many thanks!

mohit.aghera's picture

I think you should plan regarding what you want to write and you only require basic vocabulary and some technical knowledge to write blog.

However you want to write about some technical thing then you have to do some research for that..

web page's picture

“Display user email on the user profile view page
to administrator users | Mohit Aghera” was indeed
a terrific blog post, can not wait to examine far more of ur blog posts.
Time to squander numerous time on the web haha. Thanks a lot -Wilfredo

Miller's picture


Hey/... That is good work.... I have often faced similar issues and I never cared to go after it. I will definitely try out this code snippet. But I could not completely understand the concept of hook_user_view. Thanks a lot for sharing this beneficial article....!!!



When someone writes an article he/she maintains the idea of
a user in his/her mind that how a user can be aware of it.
So that's why this piece of writing is great. Thanks!

Thiis paragrwph is actսally a nice օne it assists ոew web viewers, ԝho
aare wishing for blogging.

Add new comment