Search code examples
facebookfacebook-graph-apidrupaldrupal-7

Facebook Friend List Import using fboauth, see for local account and display using views


I am using fboauth for enabling login with facebook for the website. Here is an overview of how I achieve the functionality:

When the user clicks on the facebook login button on the website, he or she is taken to a facebook login page. After logging in with facebook, the user is taken to the app authorization page where the user asks for permission to connect with the app. Once necessary permissions are granted, a local account (account at my website) is automatically created for the user and the user is brought back to a welcome page to set password. The user won't even have to verify their email address. On subsequent visits, when the user clicks on the login link, he/she is taken to the same facebook login page where they supply their login credentials. On successful login, they are brought back to the website. So far, everything works fine with the fboauth module.

What I am trying to achieve now is a functionality similar to what is found with the fbconnect module. The user is provided with a block/page where the user can import the list of his friends who has authorized with the app, and see the links to their local accounts (accounts at the website). How to achieve this functionality? The fboauth module has its own API which can be utilized. Here is what I already have, written using the API of fboauth.

<?php
module_load_include('inc', 'fboauth', 'includes/fboauth.fboauth');
module_load_include('module', 'fboauth', 'fboauth');
module_load_include('php', 'fboauth', fboauth.api');
module_load_include('inc', 'fboauth', 'includes/fboauth.field');
module_load_include('inc', 'fboauth', 'includes/fboauth.pages');
module_load_include('inc', 'fboauth', 'includes/fboauth.profile');
/**
 * Implements hook_menu().
 */
function mymodule_menu() {
  $items['my-friends'] = array(
    'title' => t('Your Friends'),
    'page callback' => 'friend_import',
    'access callback' => TRUE;
  );
  return $items;
}
function friend_import() {
          $result = fboauth_graph_query('me/friends?fields=id', $access_token);
          drupal_set_message(t('Import complete!'));
          $accounts = array();
          $output = "";
          foreach($result->data as $fbuid){
            $accounts[] = user_load(fboauth_uid_load($fbuid->id));
            $output = l($account->name, "/user/" . $account->uid);
          }
          dpm($accounts);


   return $output;
}

This will import all the friends of a user who have authenticated for the app as an object into $result (having name and user id). However, what I am finding it difficult is to display those names with the user's corresponding local accounts (accounts at the website). This is because of my lack of knowledge in php. What I am looking for is here is the exact lines of code that can be inserted after $result recieves its value so that the names of the users are displayed along with the links to their profile pages at the website.


Solution

  • Ok, so, if you look at the fbconnect module, you will see that there is a table that contains both the fid and uid (FB's and Drupal's). There is also a function you can user, to save you from the work of writing the query yourself:

    /**
     * Load a Drupal User ID given a Facebook ID.
     */
    function fboauth_uid_load($fbid) {
      $result = db_query("SELECT uid FROM {fboauth_users} WHERE fbid = :fbid", array(':fbid' => $fbid));
      $uid = $result->fetchField();
      return $uid ? (int) $uid : FALSE;
    }
    

    So, in order to get all friends, you would first need to call the the function that returns user's friends in FB:

    $result = fboauth_graph_query('me/friends?fields=id', $access_token);
    

    Then, iterate through that $result and get the local data:

    $accounts = array();
      foreach($result->data as $fbuid){
        $accounts[] = user_load(fboauth_uid_load($fbuid->id));
      }
    

    So far, I think you had already figured this out.

    Next thing we do, depends on how you want the module to behave. If you need a custom page, with an url, implement hook_menu to create that page:

    /**
     * Implements hook_menu().
     */
    function mymodule_menu() {
      $items = array();
    
      $items['desired/path'] = array(
        'title' => t('My friends'),
        'page callback' => 'fb_friends',    <-- This function you need to create now
         ....
      );
    
    function fb_friends() {
    
              $result = fboauth_graph_query('me/friends?fields=id', $access_token);
    
              $accounts = array();
              $output = "";
              foreach($result->data as $fbuid){
                $accounts[] = user_load(fboauth_uid_load($fbuid->id);
                $output .= l($account->name, "/user/" . $account->uid) . </br>;
              }
    
              return $output;
    }
    

    For better formatting, instead of building the $output yourself, you would call theme_table, that does it for you: https://api.drupal.org/api/drupal/includes!theme.inc/function/theme_table/7

    Lastly, I recommend saving the relationships to a user and his friends in a local table, so next time you don't have to go to FB. I think you can take it from the example above. If your problem is showing the friends, it will do, although not very efficient because of going to FB everytime. Hope it helps.