The first one was easy. To hide any field from the user edit form simply set the '#access'
to this field to FALSE
or any other logic you desire. The second one was more difficult. Turned out the display name was still used on the password reset page. That's how we finally solved it:
/**
* Implements hook_form__alter().
*/
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'user_profile_form') {
$form['account']['name']['#access'] = FALSE;
}
if ($form_id == 'user_pass_reset') {
// Here we take the corresponding user ID from the URL, second argument
$user_id = arg(2);
// Then we load the user
$user = user_load($user_id);
// Now we have the mail
$user_mail = $user->mail;
// extracting the date string with a helper function
$sub_text = getContents($form['message']['#markup'], '<em class="placeholder">', '</em>');
$expiration_date = $sub_text[1];
// Now we alter the existing form
$form['message']['#markup'] = '<p>This is a one-time login for <em class="placeholder">'.$user_mail.'</em> and will expire on <em class="placeholder">'.$expiration_date.'</em>.</p><p>Click on this button to log in to the site and change your password.</p>';
}
}
/**
* Helper function to extract a string between two given delimiters
* Credit: http://stackoverflow.com/a/27078384/2199525
*/
function getContents($str, $startDelimiter, $endDelimiter) {
$contents = array();
$startDelimiterLength = strlen($startDelimiter);
$endDelimiterLength = strlen($endDelimiter);
$startFrom = $contentStart = $contentEnd = 0;
while (false !== ($contentStart = strpos($str, $startDelimiter, $startFrom))) {
$contentStart += $startDelimiterLength;
$contentEnd = strpos($str, $endDelimiter, $contentStart);
if (false === $contentEnd) {
break;
}
$contents[] = substr($str, $contentStart, $contentEnd - $contentStart);
$startFrom = $contentEnd + $endDelimiterLength;
}
return $contents;
}