EPSCoR / ERCore

ERcore content management system to assist with NSF EPSCoR reporting
4 stars 7 forks source link

show email on "Contact Information" page #55

Closed khuffman closed 8 years ago

khuffman commented 8 years ago

I can't figure this out! Do any of you know how to display a user's email on the page one gets when clicking "Contact Information" menu tab? Currently a user's email is shown when one clicks the menu tab "Account Information". I don't care if the email field is editable or not, I only want the user's email shown with the rest of their contact info. such as phone, work address etc.

cjallen-epscor commented 8 years ago

@khuffman - the easiest I know is to install the 'Token Filter' module at https://www.drupal.org/project/token_filter

From there you can add a markup field at */admin/config/people/accounts/fields

Then add the following to that markup field:

<strong>Email: </strong><br>
[current-user:mail]`

Ensure that field has the Text Format of "Tokens"

an example - http://dev-ercore.nmepscor.net/admin/config/people/accounts/fields/field_test2

I did this following at dev-ercore.nmepscor.net

kgolde commented 8 years ago

I tried by adding a new computed field to the form at ?q=admin/config/people/accounts/fields and setting the php code as follows:

$uid = $entity->uid;
$account = user_load($uid);
$entity_field[0]['value'] = $account->mail;

But php on my server crashed with a memory error. I increased the memory but then it timed out after 30 seconds. I suspect the call to user_load uses too many resources.

The user.mail field seems awkward to access. It is available when viewing a user from the node_logic.inc function er_user_view_alter in $build['field_er_lname']['#object']->mail (it is attached to any field name in the build array). It is also available when editing a user from the file form_logic.inc function er_form_user_profile_form_alter in $form['#user']->mail.

kgolde commented 8 years ago

okay, this worked without crashing. Add a computed field to ?q=admin/config/people/accounts/fields and use this PHP code:

$uid = $entity->uid;
$result = db_query('SELECT mail FROM {users} WHERE uid = :uid', array(':uid' => $uid));
$entity_field[0]['value'] = $result->fetchField();

You can name it say user_email (machine name field_user_email) and label it "E-Mail Address". Also uncheck the "Store value in the database" check box to avoid storing this duplicate value in the database.

tschet commented 8 years ago

Getting the email from the user entity rather than making a database query should be more efficient. I'd also suggest using a "label" tag rather than a "strong" tag around the label. The semantic meaning of "label" is more appropriate, since the text is a label rather than important (or strong). Matching the CSS class of the other labels on your page will allow you to match the style.

kgolde commented 8 years ago

I tried the following code segment in the computed field to try to get the user entity:

$uid = $entity->uid;
$wrapper = entity_metadata_wrapper('user', $uid);
$entity_field[0]['value'] = $wrapper->value()->mail;

It caused the white screen of death due to "PHP Fatal error: Maximum execution time of 30 seconds exceeded", same problem as call to user_load in above message. The function call to entity_metadata_wrapper did work inside a php file. I successfully used it as a test in the file node_logic.inc function er_user_view_alter and printed the result in the debug message area.

tschet commented 8 years ago

I would consider that something that should be resolved on the theme layer, rather than data, so I would be inclined to use display suite, panels, or one of the theme .tpls to display that. If you did take the computed field approach, you should be able to use "$entity_field[0]['value'] = $entity->mail;" to display the email address of a user.

khuffman commented 8 years ago

We decided to go with computed field and this as php code: $entity_field[0]['value'] = $entity->mail; So we think our issue is resolved and I will close this issue. Thank you @tschet and @cjallen-epscor for your suggestions.