Last updated December 22, 2011. Created by Dave Cohen on December 22, 2011.
Log in to edit this page.

Let's say you've defined Drupal profile fields, and the information you want in them might be available from the Facebook APIs. It would be nice to save those profile fields automatically when Drupal for Facebook creates a user account.

Drupal for Facebook does not know how you've named your profile fields. It also doesn't know if the user has granted all the extended permissions that you might need to access that information. So you'll need a custom module with a small snippet of code that puts the right data in the place where Drupal's profile module expects to find it.

Drupal 6.x Example

In this example our profile fields have been named 'profile_name_first' and 'profile_name_last'. In data pulled from facebook, the values are labelled 'first_name' and 'last_name'.

<?php
/**
* Implements hook_fb_user().
*
* fb_user.module will call this before and after creating a local Drupal account.
*
* When $op == FB_USER_OP_PRE_USER, this function has a chance to add or
* change the values that will be passed into user_save().  The $return
* variable contains the values.
*
* @param $data
* fb_user.module will fill this with information pulled from the facebook API.
* If you need additional information, call fb_api() to get it.
*/
function CUSTOM_fb_user($op, $data, &$return) {
  if (
$op == FB_USER_OP_PRE_USER) {
   
// fb_user.module is going to call user_save().  We have a chance to add data.
    // If the names of these profile fields change, breaks this code.
   
$return['profile_name_first'] = $data['info']['first_name'];
   
$return['profile_name_last'] = $data['info']['last_name'];
  }
}
?>

Drupal 7.x Example

TODO! In Drupal 7.x it works in much the same way, but the data structure expected by profile module is wildly more complicated.

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

Has anyone got around to creating an example of this for Drupal 7 yet?

For drupal 7 i updated custom user fields in the user entity like this:

<?php
function wb_connect_fb_user($op, $data, &$return) {
    global
$user;
    if (
$op == FB_USER_OP_PRE_USER) {
       
$return['field_voornaam']['und'][0]['value'] = $data['info']['first_name'];
       
$return['field_achternaam']['und'][0]['value'] =$data['info']['last_name'];
     }
}
?>

If you want to know wich fields are available, enable the facebook devel module.

http://www.wbase.be twitter: @wbase_be

i've been trying this snippet in my own module and it doesn't seem to work, is there any indepth instructions fro 7.x? I cant seem to pass first and last name into the user account on connection. Can any one point me to a place with more in depth instructions? the forums and documentations on drupalforfacebook.org didn't help.

function CUSTOM_fb_user($op, $data, &$return) {
  global $user;
  if($op == FB_USER_OP_PRE_USER) {
    $return['field_first_name']['und'][0]['value'] = $data['info']['first_name'];
    $return['field_last_name']['und'][0]['value'] = $data['info']['last_name'];
  }
}

I had the need to create a Profile2 object after a user was created via Drupal for Facebook with Address Field.

$data has completely different info based on PRE_USER or POST_USER.

function CUSTOM_fb_user($op, $data, &$return) {
global $user;
if ($op == FB_USER_OP_POST_USER) {
$CUSTOM_fb = fb_api('me');
    // create profile object
    $profile = profile_create(array('user' => $data['account'], 'type' => 'user_profile'));
    // populate profile fields
    $profile->field_first_name['und'][0]['value'] = $CUSTOM_fb['first_name'];
    $profile->field_last_name['und'][0]['value'] = $CUSTOM_fb['last_name'];
// populate address fields
$locality = explode(", ", $CUSTOM_fb['location']['name']);
$state = CUSTOM_simple_state_lookup($locality[1]);
    $profile->field_address['und'][0]['country'] = $state['country'];
    $profile->field_address['und'][0]['locality'] = $locality[0];
    $profile->field_address['und'][0]['administrative_area'] = $state['state'];
    // save profile
    profile2_save($profile);
  }
}

Is it possible using rules to create an action to populate the profile fields from the users facebook profile with php code filter or using tokens?