8

Email is now an included field in core, but the stock email for users seems to be set to private; only users with administrator privileges or the user themselves can see.

I'd like to allow other users to see this basic field, without having to create yet another email field where someone has to copy the information over from the other other simply for the ability for it to be visible.

This should be a fairly easy/common practice, but I can't find anything yet.

How do I allow regular non-admin users to view other users' emails?

3
  • 1
    In 15 years of developing for the web, I've literally never once been on a website where people's email addresses are made public. I wouldn't consider this to be common practice at all, might be why you're struggling to find an existing solution. Altering the access of that element in whatever context(s) you need is probably quite straight forward with code, but where should users be "seeing" this information? On the profile page with other fields for example?
    – Clive
    Jun 26, 2016 at 10:56
  • The fact the email is not visible to other users is simply to avoid spamming. Also, it is a bad idea to show the email used to create an account. I agree with @Clive tha showing the email address is not such a common practice.
    – apaderno
    Jun 26, 2016 at 11:08
  • 2
    I do not want to make users' email fields available to the general public. But, in my 12 years of working with several federal departments, a host of NGOs, and numerous school systems - I can assure you it is very common practice to make your colleagues' general contact information, including their primary email, available to other authenticated users. If your case is that the standard method for providing this email is as a separate entity than the user's registered account email, I can accept that - though it seems a bit unnecessarily redundant for a closed system.
    – W01F
    Jun 27, 2016 at 0:26

3 Answers 3

2

I had a similar use case. Here's another solution: I added a new email field to users' profiles, and in a custom module (I did this is Drupal v8.1), use hook_user_presaveSee Drupal Api:

function MYMODULE_user_presave(User $user){ $user->set('field_profile_email', $user->getEmail()); }

Then you can use this new field in Views, for example, in a department contact list, as was mentioned in a comment to the question. You could use Field Permissions to further enhance this as well.

This does have the downside of creating a second field, but the function avoids having to manually copy it, and it's now available anywhere.

2
  • 1
    this will copy the email once it's saved or updated but will not copy it if it already exists in the email field, right ?
    – usethe23
    Feb 3, 2017 at 19:19
  • Correct. This wouldn't affect any users already in the database. To do that, you'd need resave them.
    – beltouche
    May 9, 2017 at 19:00
7

One route you can take is to allow users the View user information permission and display this information on the user profile page.

To do so, override user.html.twig in your theme (copy it from core/modules/user/templates/user.html.twig to your theme's template directory) then inject the email into the template via hook_preprocess_user() in your mytheme.theme file:

/**
 * Implements hook_preprocess_user()
 */
function mytheme_preprocess_user(&$variables) {
  $variables['mail'] = $variables['user']->getEmail();
}

In this example, you'll be able to user {{ mail }} to insert the email variable into your user profile template.

3
  • Thanks, that works great! Is there a way to enable the email field to show using views, though? The user directory is the primary way users look up contact information.
    – W01F
    Jun 27, 2016 at 12:44
  • 1
    The equivalent workaround for views would be something like hook_preprocess_views_view_field to inject the user email into your views field.
    – Shawn Conn
    Jun 27, 2016 at 22:54
  • 1
    $variables['user']->getEmail(); this returns only currently logined users email.
    – tasqyn
    Feb 8, 2019 at 8:12
0

There is a D8 Vore feature request to add a permission to see other users' mail address: Add new permission to view user email field [#2799049] \| Drupal.org

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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