1 min read

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:

   $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:

// 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:

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);

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

1 min read

Google Now Showing More Search Results From a Single Domain

Big news from Google today - Google Software Engineer Samarth Keshava posted on the Webmaster Central Blog this morning that Google is now displaying more results from the same domain in its search...
2 min read

CommonPlaces Launches Kids-Only Social Networking Site

Safe and highly secure social networking site boasts many features for children
1 min read

CommonPlaces Gets "Award of Excellence" for WhatsWhat.me

Web design and development company chosen amongst 6,000 entries to receive highest distinction
4 min read

CommonPlaces Works with SNHS to Rebuild Company Site & Portal

SNHS Improved Company Portal Enhances Job Posting Process, Content Management, and Staff Workflows May 21, 2021; New Hampshire; Hillsborough County – CommonPlaces Interactive is pleased to announce...