Community Documentation

NNNN@facebook

Last updated November 8, 2009. Created by Dave Cohen on November 8, 2009.
Log in to edit this page.

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 no longer be a problem, once the modules are ported to 7.x (volunteers?).

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:

<?php
/**                                                                            
* 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));
    }
  } 
}
?>

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.

Comments

A couple more suggestions

I had a few more places where the nnn@facebook.com still showed up. I ended up modifying my page.tpl.php in 2 places with the following:

  <head>
    <title><?php global $user; print str_replace($user->name, fb_format_username($user), $head_title); ?></title>

and (in the body):

                <?php if ($title): ?>
                <h1 class="title"><?php global $user; print str_replace($user->name, fb_format_username($user), $title); ?></h1>
                <?php endif; ?>

This is a bit heavy handed, as it applies to all pages, but it works. fb_format_username() will get the user's full name from FB, if possible. Note that as of this writing, you need a patch which can be found in #1066486: Need to pass token to FQL query in fb_users_getInfo.

I also needed to show a proper user name in a node view. Using User:Name just showed the nnn@facebook.com, so I added a views-view-field--name.tpl.php file that included the following:

  // First get a proper display name. For better performance we check
  // if this is a Facebook Connect user and if so we get the FB user name
  if ( strpos($output, '@facebook') !== false) {
$account = user_load($row->users_uid);
$name = fb_format_username($account);
  } else {
    $name = $output;
  }
  print $name;

You need to include the User:Uid field in the view (so it's accessible for the user_load() call) but can exclude it from display. Also, you need to copy views-view.tpl.php from modules/views/theme to your theme folder, otherwise it won't catch any field-specific theme files.

Page status

No known problems

Log in to edit this page

About this page

Drupal version
Drupal 5.x, Drupal 6.x
Audience
Developers and coders, Site administrators

Site Building Guide

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.
nobody click here