3

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 function mymodule_fboauth_actions() { $actions['mymodule_friend_import'] = array( 'title' => t('Imports your friends), 'callback' => 'mymodule_fboauth_action_friend_import', 'permissions' => array( 'user_friends', ), ); return $actions; } function mymodule_fboauth_action_friend_import($app_id, $access_token) { $result = fboauth_graph_query('me/friends?fields=name', $access_token); drupal_set_message(t('Import complete!')); } ?>

This will import all the friends of a user as an object into $result. 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 my website.

3
+150

The code you have there is returning friend's names which could be problematic if any of the user's friends have the same name (names like John Smith and Mohammed Abdul spring to mind).

You should change your friend import function to get the facebook user id like this:

function mymodule_fboauth_action_friend_import($app_id, $access_token) {
  $result = fboauth_graph_query('me/friends?fields=id', $access_token);
  drupal_set_message(t('Import complete!'));
}

As you rightly pointed out fboauth has a useful API including this method which turns facebook user ids into Drupal user ids:

/**
 * 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;
}

To make an array of Drupal user profiles you could write this in your code:

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

How you then use those profiles depends on where you want to display them, but something like this should do the trick:

foreach($accounts as $account){
  print l($account->name, 'user/'.$account->uid);
}

If you put it all together you will have something like this - you may need to adjust some of the specifics as I do not know how you wish the information to be displayed:

<?php
function mymodule_fboauth_actions() {
  $actions['mymodule_friend_import'] = array(
    'title' => t('Imports your friends'),
    'callback' => 'mymodule_fboauth_action_friend_import',
    'arguments' => array)
      'app_id' => MISSING,
      'access_token' => MISSING,
    ),
    'permissions' => array(
      'user_friends',
    ),
);
  return $actions;
}

function mymodule_fboauth_action_friend_import($app_id, $access_token) {
  $result = fboauth_graph_query('me/friends?fields=id', $access_token);
  drupal_set_message(t('Import complete!'));

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

  foreach($accounts as $account){
    print l($account->name, 'user/'.$account->uid);
  }
}

If this doesn't display anything that means print is the wrong function to use in this context. How you get the items to display depends entirely on where and how you are calling this function. If it's part of a hook such as hook_node_view() you can edit the node entity before it is rendered, but if (as I suspect) this function is being run in the background without refreshing the screen you will likely have to employ some JQuery or AJAX to get the relevant items to display.

I would start with this post on Javascript and JQuery for an explanation of how Drupal uses JQuery, and this post on building AJAX into non-form elements in Drupal.

  • dpm($account) brings values of the user account. Seems like print is not the function to do it. – Rana Prathap Sep 24 '14 at 6:46
  • Yep, afraid I can't help you there, much too big a question, the links I have provided should help, and then maybe you could write a follow up question with a different title to attract some people with that kind of experience. – Darvanen Sep 24 '14 at 6:51
  • 1
    I think i will try to take it from here. Thanks a lot for all the help. I suggest that you contribute this to the community as a module, a submodule to fboauth even though this may be an easy thing to do with the API for most people. I have creatd an issue under fboauth for this feature request. Someone can post this there and probably will be included as a patch in the next developement release of the module. – Rana Prathap Sep 24 '14 at 6:55
  • You're welcome, I hope you achieve your goal with this, I know what it's like to be absolutely stuck with no idea what to do next. – Darvanen Sep 24 '14 at 7:03
  • Thanks a lot for all the help. I have achieved my goal and more with this, without using ajax. Please find the thread here drupal.org/node/2340875 – Rana Prathap Jan 29 '15 at 9:15

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.