NNNN@facebook

Last updated on
30 April 2025

Why do I see 1234@facebook instead of a username?

This is a reasonable question. This is a problem in Drupal 5.x and 6.x. Improvements have been made to Drupal core in 7.x, so this should not be a problem in D7 and higher.

Background

Facebook allows users to log in with an email address (which must be unique). The user's full name does not have to be unique. You might know two "John Smiths" on Facebook, hopefully you can tell them apart by their profile picture.

Drupal does not distinguish between (a) username used for logging in and (b) you name as shown to other users. For logging in, usernames must be unique.

So Drupal and Facebook have different uniqueness requirements for usernames. To simply save the username as it appears on Facebook would lead to problems, because Drupal can't have two "John Smiths".

And before you say "'John Smith 2' is a reasonable username," let me just say: it isn't, and more importantly it violates Facebook's terms of service for the application to save the user's name. I repeat: it is a violation of the terms of service for your application to save to your local database the user's name as returned from Facebook's APIs. So Drupal for Facebook will never do this.

Instead, when Drupal for Facebook creates a new user, for a username it uses [user id on facebook] + "@facebook". It does this because (a) it stands an awfully good chance of being a unique name on your Drupal site, and (b) by inspecting the string, it can learn the user's id on facebook. Internally, Drupal for Facebook refers to facebook user ids as fbu, not to be confused with uid, which is the Drupal user id.

Problem, and one solution

So given that we have an ugly user name, like 1234@facebook, we never want to show it to the users of our site. What can we do? Fortunately, well behaved Drupal modules never display a username directly. Instead they call theme('username',...), and we can control what this function displays.

In Drupal 5.x, your theme must explicitly override the theme_username() function.

Drupal 6.x is more flexible, allowing modules to change the behavior of theme functions. So fb_connect.module contains code which changes theme('username', ...). See fb_connect_theme_registry_alter() and related function to see how this is done, for both user name and pictures. If your Drupal 6.x theme overrides theme('username', ...), take close look at fb_connect_theme_username_override() and do something similar to support facebook names.

But I still see NNNN@facebook

Yeah, I know.

Remember when I said "well behaved Drupal modules never display a username directly?" Well Drupal core (5.x, 6.x) is not well behaved. Fun!

In a number of places, Drupal core spits out $account->name without bothering to call theme('username', ...) or give modules any other way to change its output. These places include page titles on user/... pages, email sent to users, and probably many others I've lost track of. It's probably impossible to catch all the places where Drupal does this, but you could try, like the Real Name module tries to. For example in 6.x, write a module with this hook to fix problems with user profile pages:

/**                                                                             
 * Implementation of hook_user().                                               
 *                                                                              
 * On profile pages where drupal sets the title to be the username, we          
 * get the awkward-looking NNNNNNNN@facebook for Facebook Connect               
 * users.  Here we run fb_format_username to get the best possible name.        
 */           
function CUSTOM_user($op, &$edit, &$account, $category = NULL) {
  if ($op == 'form' || $op == 'view') {
    if ($account->name == drupal_get_title()) {
      drupal_set_title(fb_format_username($account));
    }
  }  
}

For those willing to patch Drupal Core

This relatively modest patch will make Drupal 6.x behave more like Drupal 7.x. It introduces format_username() and calls it in several important places (there may be others the patch misses). If you're willing to patch your user.module, this might be a good solution. http://www.drupalforfacebook.org is one example of a site using this patch.

Index: user.module
===================================================================
--- user.module	(revision 2341)
+++ user.module	(revision 2342)
@@ -10,1 +10,1 @@
 define('EMAIL_MAX_LENGTH', 64);

 /**
+ * format_username will appear in Drupal 7.x.
+ *
+ * See http://drupal.org/node/192056
+ */
+function format_username($account) {
+  $name = !empty($account->name) ? $account->name : variable_get('anonymous', t('Anonymous'));
+  drupal_alter('username', $name, $account);
+  return $name;
+}
+
+/**
  * Invokes hook_user() in every module.
  *
  * We cannot use module_invoke() for this, because the arguments need to
@@ -734,7 +745,7 @@

       case 1:
         if ($menu = menu_tree()) {
-          $block['subject'] = $user->uid ? check_plain($user->name) : t('Navigation');
+          $block['subject'] = $user->uid ? check_plain(format_username($user)) : t('Navigation');
           $block['content'] = $menu;
         }
         return $block;
Index: user.pages.inc
===================================================================
--- user.pages.inc	(revision 2341)
+++ user.pages.inc	(revision 2342)
@@ -162,7 +162,7 @@
  * Menu callback; Displays a user or user profile page.
  */
 function user_view($account) {
-  drupal_set_title(check_plain($account->name));
+  drupal_set_title(check_plain(format_username($account)));
   // Retrieve all profile fields and attach to $account->content.
   user_build_content($account);

@@ -233,7 +233,7 @@
  * @see user_edit_submit()
  */
 function user_edit($account, $category = 'account') {
-  drupal_set_title(check_plain($account->name));
+  drupal_set_title(check_plain(format_username($account)));
   return drupal_get_form('user_profile_form', $account, $category);
 }

(Apply the patch in the modules/user/ directory.)

I'm still not satified

Man, you're tough to please! Here's what else you can do...

  • Disable the Create Local Account checkbox on your Facebook Application. Now, Drupal for Facebook will not create user accounts with ugly names. It won't create accounts at all! Instead, give your users a link to user/register where they can create the account.
  • Give the user a link to user/UID/edit page. After Drupal for Facebook creates the account, the user can edit it to change their username, and provide whatever other details your site asks of users.
  • Implement hook_fb() in your custom module. This hook will be called with $op == FB_OP_PRE_USER right before the new user account is created. In this hook you can change the user's name and other details about them.

Help improve this page

Page status: Not set

You can: