Connecting Facebook with Drupal, the easy way.

There are a few heavyweight modules contending to provide Facebook integration for Drupal. Which module is right for you? Simply looking at the Reported installs and Maintenance Status won't present a clear answer.

Choices

A recent project of mine required the use of Facebook's OAuth service. Here are the three modules that I considered:

Decisions

I installed and configured each of these modules before coming to a decision. Here's a quick summary of what I found:

  • Drupal for Facebook
    is still very much in development. It isn't production ready, so if you're not willing to contribute, you may want to cross it off the list. Beyond that, it's important to realize that DFF's strength lies in providing a rough framework for using Drupal to build Facebook Applications. It is not ideal (or intended) for providing polished Facebook features to a pre-existing Drupal site. If you're looking for a good launch pad for some serious Facebook App development, give it a shot-- it may be just what you need. Otherwise, you may want to consider another module.

  • Facebook Connect
    provides a number of unique out-of-the-box Facebook features, like finding Facebook friends on your Drupal site, or publishing a customizable message on their Facebook feed. However, this module is still in beta— it is not in a stable state. It should also be noted that FC uses the Facebook Javascript SDK, which means that you need to be familiar with the SDK if you'd like to extend the module's functionality.

  • Facebook OAuth
    is best described in its author's own words, "This module is built with simplicity and flexibility in mind, it provides login services (and does it well), and an API for performing any other actions you may want to write yourself to query against Facebook's APIs."

The Winner (for me)

After attempting to extend Drupal for Facebook and Facebook Connect, Facebook OAuth was like a breath of fresh air. It's simple and flexible, and best of all, you don't need to learn Facebook's Javascript SDK to use it! Facebook OAuth has a well documented and well designed API that any PHP (particularly Drupal) programmer would be comfortable with. Some key features include:

  • One-click login through Facebook.
  • Automatic import of user e-mail and profile information during initial login.
  • A flexible and direct API for modules to get authenticated and query Facebook's APIs (plus extensive documentation).
  • Does not require any external libraries or downloads.

Harnessing the Power (the fun part)

Before I jump in, take note that you can find detailed information about all of FBOAuth's API functions in fboauth.api.php, which is bundled with the module. And don't forget to read the README.txt!

Now let's run through a quick example of how you can utilize the API.

Facebook OAuth allows you to import values from a user's facebook account and map them to that user's Drupal account during registration. In addition to providing a nice GUI for this mapping, it also allows you to define custom field types and callbacks for accomplishing the mapping and for processing the data. Most popular field types are supported by default.

For my site, I wanted to import a user's facebook address to an addressfield and geocode the location for storage in a geofield. By default, FBOAuth does not support mapping to fields of type addressfield. No problem! We'll use a few handy hooks to add that ability.

First, I want to let FBOAuth know that it can map Facebook location information to fields of type addressfield. I'll use hook_fboauth_user_properties_alter() to do that:

/**
 * Implements hook_fboauth_user_properties_alter().
 */
function grasmash_fboauth_user_properties_alter(&$properties) {
  // Allow the location property to be mapped to Addressfield typed fields.
  $properties['location']['field_types'][] = 'addressfield';
}

That was easy. Some detail:

  • The first array key 'location' specifies the Facebook source field. Change that to another acceptable value in order to map a different source field to a new field type.
  • You can add as many target 'field_types' as you'd like.
  • For a full list of possible source field values, just take a look at fboauth_user_properties().

Next, let's tell FBOAuth what it should do with data that's been mapped to an addressfield by defining a callback that will process the data.

/**
 * Implements hook_fboauth_field_convert_info().
 */
function grasmash_fboauth_field_convert_info_alter(&$convert_info) {
  $convert_info += array(
    'addressfield' => array(
      'label' => t('Address Field'),
      'callback' => 'grasmash_fboauth_field_convert_location',
    ),
  );
}

If you're familiar with Drupal's array-heavy architecture, this snippet shouldn't surprise you at all. We're just adding another row to $convert_info. It specifies the name of the function that FBOAuth should look for when dealing with an addressfield. Now let's actually create that function!

/**
 * Facebook data conversion function.
 * Converts an incoming Facebook location (which is an object) into an array compatible with the addressfield module.
 */
function grasmash_fboauth_field_convert_location($facebook_property_name, $fbuser, $field, $instance) {
  $value = NULL;
  if ($field['type'] == 'addressfield' && isset($fbuser->location) && module_exists('geocoder')) {
    $geodata = geocoder('google', $fbuser->location->name);
    foreach ($geodata->data['geocoder_address_components'] as $key => $components) {
      switch($components->types[0]) {
        // Set city.
        case 'locality':
          $value['locality'] = $components->short_name;
          break;
        // Set state.
        case 'administrative_area_level_1':
          $value['administrative_area'] = $components->short_name;
          break;
         // Set country.
         case 'country':
          $value['country'] = $components->short_name;
          break;
      }
    }
  }
  return $value;
}

Clearly, this will be completely different for fields of any other type. The example above is specifically tailored for populating addressfield fields, and it relies entirely on the output returned by the geocoder module. If you're looking for some guidance in creating your own callback, just look at some of the default callbacks in fboauth.field.inc, like fboauth_field_convert_text().

The last step is to simply configure which field on the user object FBOAuth should actually import the data to. That is done through FBOAuth's admin page, located at admin/config/people/fboauth.

Other Awesome Things

From the README.txt:
"The Facebook OAuth module provides an API for executing queries against Facebook's vast store of user data." The README.txt goes on to explain the workflow for this process, and how you can write your own custom actions.

I plan to write a subsequent blog post about how you can programmatically execute these custom actions, but that's for another day!

Enjoy!

Drupal Version Compatibility: 

Comments

Thanks for sharing!

ITS VERY HELP FULL

I literally tried Drupal for Facebook and Facebook Connect yesterday and they didn't provide what we required. First time hearing about Facebook OAuth when reading this and just tried it out - BRILLIANT module!

Just what I needed, been struggling with Drupal for Facebook and other similar modules (basically was stuck on external libraries and errors around them). Great to see this straightforward module. Thanks again!

There is an existing wiki over at http://groups.drupal.org/node/65268 and I would love to see it get some love to help out the next people facing down this decision.

I have implemented the Facebook OAuth module to my site..On clicking the logout '/user/logout' link user gets logged out of Drupal but on clicking the fb button that user gets logged in.........facebook logins are not asked..which means the user didnot get logged out from fb.... how to do this logout?

Thanks for this article!

I get the mytheme_fboauth_user_properties_alter() function to fire, but using a watchdog() alert, it seems that the mytheme_fboauth_field_convert_info_alter() never seems to execute.

As such it allows me to select the field I want to map, but it never maps it.

Anyone experiencing the same problem?

I'm using Drupal 7.14 with AdressField 7.x-1.0-beta3, with geoPHP 7.x-1.4 and geoCoder 7.x-1.1

Thanks!

@bluhring

Have you tried placing the hook in a module rather than in a theme template.php file?

I have not tried that. Still relatively new to Drupal, but come from a strong programming background... I didn't know that if a hook didn't fire in template.php that it might as a module. Will try to give that a shot.

Thanks for the advice!

I've moved the two functions to a module (and enabled it), but they still don't fire... When in a module, do I need to do anything other than name them correctly?

I'm also looking at your "Part 2" article on importing photos... Using watchdog() alerts I see that the mymodule_fboauth_actions() method fires just fine, and it adds the photo setting in the FBOauth settings area.

But the mymodule_fboauth_field_convert_info_alter() and mymodule_fboauth_field_convert_location() never fire the watchdog() alert, nor do I ever see it change my user data.

...thought... If my user account was already created, and then I connect it to FB, does the xxxx_field_convert_info_alter() still apply... or is it just for new accounts?

Sorry for the questions. I actually got this working now. Had an issue with one of my functions having a typo in the name... I think it went unnoticed because I was using a pre-existing account, but when I tried a clean user never having been on the site, it worked like a charm!

Working on something for hours, only realize that you made a typo. I've been there.

Hi madmatter23!

Thanks for sharing this -- it's been incredibly useful. I'm trying to do the same type of integration you did with addressfield with Name Field, but I'm missing a bit of a step. One of your code comment reads "Converts an incoming Facebook location (which is an object) into an **array compatible with the addressfield module**." If you have a moment, would you explain where one would look to find the array format necessary for a field type defined in another contrib module (say, for example drupal.org/project/name)

Thanks!

First, I created a new node using the field in question. Then, I used the devel module to look at the node's properties. That reveals the structure of each field. If you need to quickly recreate the array structure in php, devel's dvm() function is a great improvement to print_r().

Nice tutorial. Is there any way to open fb-login window separately.

Thanks for the tutorial. This is one of the issue I haven't touched yet seriously. I didn't expect that it is easy :)

Hi there!
Thanks for a so very much needed explanation of these modules!!
I didnĀ“t understand something though: May I use FB OAuth to let anonymous users (ie. comment) logging in in their FB account but without creating an account in the Drupal site?
Thanks!
Rosamunda

I implemented the facebook OAuth module to my current project. I get it some error after installing this module..On clicking the logout '/user/logout' link user gets logged out of Drupal but on clicking the fb button that user gets logged in.

Can You share with me a complete video for Facebook Integration In MY site PLz

can you share your custom module?

You forgot to mention where to put these codes.

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.