1 min read

Real Name vs. Username - On Node Creation

Real Name vs. Username - On Node Creation

There are a lot of requests on the Drupal.org site about how to change the places where the username appears in order to display the "real name" (it could be first name, last name, or any other information, like dog's name).

What if in the "Authored By" field when you are first creating a node, you want to look for the real name because you don't know what crazy things users have inserted as their usernames? HmmmŒƒ Is there a way to look for a real name instead looking for the username? Yes. Drupal is very well structured, and allows you to improve this field in order to better fit your needs.

It's important that you have some MySQL and Drupal knowledge in order to accomplish this change. This method is not quite as simple as installing a module, but this is the way I have implemented it in the past.

The first step is you should have a custom module that you are playing with in order to modify some things for your website. Then in this module you need to add a new menu item like this in your hook menu function:

<?php
   $items['custom/user_autocomplete'] = array(
      'title' => 'User autocomplete',
      'page callback' => 'custom_user_autocomplete',
      'access callback' => 'user_access',
      'access arguments' => array('access user profiles'),
      'type' => MENU_CALLBACK,
   );
?>

Drupal by default calls this path "user/autocomplete" in the user module and this is what you are modifying now, NOT touching the core.

The second step is to add a condition on hook form_alter that redirects the default request to the one that we created in hook menu:

<?php
// Changing the authoring information in order to call another ajax function instead of core
    if ($form['author']['name']['#autocomplete_path']) {
		$form['author']['name']['#autocomplete_path'] = 'custom/user_autocomplete';
	}
?>

Third step - "the query". All that you need to do now is implement your query inside the function that you are calling in your custom menu item that calls custom_user_autocomplete. As I'm using the core profile module to build this example, the query that I did looks for the field 1 in the profile_values table which is my "real name" and shows it in the autocomplete choices. The good thing is that while the user sees the real name in the "Authored by" field, the field gets the username value from the users table, so you don't need to perform other validations to ensure that Drupal will recognize the username that is being inserted as the author of the node. Take a look at the code and the images below:

<?php
function custom_user_autocomplete($string = '') {
  $matches = array();
  if ($string) {
    $result = db_query_range("SELECT u.name, pv.value FROM {users} u, {profile_values} pv WHERE pv.fid = %d AND pv.uid = u.uid AND LOWER(pv.value) LIKE LOWER('%s%%')", 1, $string, 0, 10);
    while ($user = db_fetch_object($result)) {
      $matches[$user->name] = check_plain($user->value);
    }
  }
  drupal_json($matches);
}
?>

Here I am looking for my real name, "Lemuel Santos":

Entering a real name

After choosing a real name, it gives you back the username, in order to pass the validation with a valid username:

Drupal returns the username


Related Posts

3 min read

Will Internet Fast Lanes be the End of Net Neutrality?

We've been chronicling the issue of net neutrality for several months, and recognize the significance of, and the interest in, this subject. We welcome today's guest blogger Daniel Brenton, offering...
1 min read

Nauset Construction Launches New Website After 10 Years

Nauset Construction, a construction management and design-build firm in Needham, MA, reached out to CommonPlaces to redesign and redevelop their website that they first built for them ten years ago....
1 min read

Does Your Drupal Website Need CPR?

Is your Drupal Website "broke"? Missing functionality? Crashing?
4 min read

CommonPlaces Works with New Futures to Rebuild Company Website

New Futures: Improved Company Website Enhances Site Navigation and Provides Ease of Content Management. October 5, 2021; New Hampshire; Merrimack County – CommonPlaces Interactive is pleased to...