1

I'm building a site that leverages the user email address for the user login instead of a username (via modules Logintoboggan & Email Registration). I'm also using Real Name to display the "first name" field of the user instead of the username.

My solution is working perfectly apart from when the user is on the edit user account page (and some native drupal system notification message), which displays a "Display Name" field instead of the desired "first name" field.

Question: How can I remove/delete the "Display Name" field from the edit profile page and Drupal system messages/notification fields?

"Display name" field on user registration form

system notification message showing display name "penny_144"

2
  • Where do the First Name, Last Name, Display name fields come from? Did you create them? Or are they brought by any module you mentioned?
    – leymannx
    Sep 28, 2015 at 8:02
  • I defined them.
    – Andy
    Sep 28, 2015 at 8:04

1 Answer 1

3

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;
}
12
  • Thank you but where do I specifically add this code? I am a front end developer with zero experience in this area of Drupal ☺️
    – Andy
    Sep 28, 2015 at 8:51
  • What about tokens or string overrides? Could I leverage any of these modules to assist?
    – Andy
    Sep 28, 2015 at 8:52
  • @Andy - Tokens or String Overrides. You have to find out. Don't know any of these modules. The code I have in my answer needs to be put into a very simple custom module. All it needs is a .info and a .module file. Inside the .module file you have to place that code. Look for tutorials about creating custom modules in Drupal. There are plenty. Believe me, it's worth learning and it's not so difficult as it sounds first.
    – leymannx
    Sep 28, 2015 at 8:59
  • Thank you so much for taking the time to help me. I really appreciate it.
    – Andy
    Sep 28, 2015 at 9:00
  • @Andy - If you have a custom backend theme you could also paste that code in this theme's template.php and name it function MYTHEME_form_user_profile_form_alter(...). But if not, go with the module approach. You then later also can put other custom code inside there. Also have a look at the Devel module. After installation you have a function dpm(). Calling dpm($form) inside the code of my answer will output you the correct name of your field and much more. You're welcome!
    – leymannx
    Sep 28, 2015 at 9:07

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.