0

How can I populate an authorized user's Facebook data into my Drupal registration form? Note: user is authorized from my Facebook application and allowed my Facebook app to access his data.

Update: I am using Drupal for Facebook, and to populate the user info I am implementing fb_form_alter inside fb.module. My code is

function fb_form__alter(&$form, &$form_state, $form_id) {
if(!($form_id == 'user_register_form')) {
return;
}
if ($GLOBALS['_fb'] && $fbu = fb_get_fbu($account)) { // @TODO - is fb_get_fbu() a performance hit here?
    try {
      $data = fb_fql_query($GLOBALS['_fb'], "SELECT name FROM user WHERE uid=$fbu", array(
                             'access_token' => fb_get_token($GLOBALS['_fb']),
                           ));
      if (count($data) && isset($data[0]['name'])) {
        $form['account']['name']['#default_value'] = $data[0]['name'];
      }
    }
    catch (Exception $e) {
      fb_log_exception($e, t('Failed to alter username for facebook user %fbu', array(
                               '%fbu' => $fbu)));
    }
}
}

I put the above code after the form_username_alter() function but with no success :(

1

If you're using Drupal for Facebook, it should be doing that for you already. Make sure to enable the FB User module and configure the Form Alters at admin/build/fb/fb_user. Once you've got that configured, visitors to the Create new account page will have the form prepopulated, including the profile picture.

To see how it's done, have a look at fb_user.module's hook_form_alter(). To see if it's all working, you can create a simple PHP block to display the Facebook data collected for the current visitor:

$data = _fb_user_facebook_data($GLOBALS['_fb']);
print '<pre>' . print_r($data, TRUE) . '</pre>';
5
  • i printed the Fb data of user in a block using above code. Data appears in block but it couldn't set in user register form as default values although the fb_user module includes the code of form alter as i required. Even i made another module, in which i alter the register form but all in vain Aug 15 '11 at 22:21
  • Hm... Did you check Form Alters on admin/build/fb/fb_user page?
    – Jan Tomka
    Aug 16 '11 at 2:02
  • yes altering all forms are checked. Aug 16 '11 at 18:06
  • what could be the problem as data not only populating in register form? as my all related modules are enabled. i have checked form alters on admin/build/fb/fb_user. $data prints info on any block. Aug 18 '11 at 23:32
  • fixed thanks again. These were the lines !$form['account']['name']['#default_value'] and !$form['account']['email']['#default_value'] creating problem. Aug 19 '11 at 1:07
1

The Dupal for Facebook module kind of does the trick.

The problem is that it uses soon to be deprecated FB APIs, and it isn't exactly Drupal 7 ready yet (though it's very well maintained and I am sure it'll catch up soon), so you might as well want to write your own module from scratch. These are the basic info you should check if you want to get into this

  1. Get user Authentication and Permissions. (You will probably need to also look into offline_access permissions.)
  2. Access to the FB user object using the PHP SDK for Basic profile informations. It is session based, so you can access it from anywhere in your site, for instance to pre-populate you registration form. (Don't forget to include the PHP SDK to your script.)
  3. Access or write other user data (e.g., activities, picture, wall feed) via the Graph API.
1
  • sorry i didn't mention betfore, i am already using this module. But implementing fb_form_alter does not help me. Gonna update my question please have a look at it Aug 12 '11 at 0:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.