Advertising sustains the DA. Ads are hidden for members. Join today

User Profile Fields

Last updated on
30 April 2025

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'.

/**
 * 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.

Help improve this page

Page status: Not set

You can: