You are here

function fb_stream_post_options in Drupal for Facebook 7.3

Same name and namespace in other branches
  1. 6.3 fb_stream.module \fb_stream_post_options()

Helper function to add post options to a form.

2 calls to fb_stream_post_options()
fb_stream_form_alter in ./fb_stream.module
Implements hook_form_alter().
fb_stream_node_settings_form in ./fb_stream.module
Helper function for hook_form_alter() renders the settings per node-type.

File

./fb_stream.module, line 248
Support for Facebook's Stream API.

Code

function fb_stream_post_options($token, &$to_options, &$from_options, &$from_tokens) {
  if (!$token) {
    return;
  }
  try {

    // TODO: consolodate graph api, use batch. And cache.
    $me = fb_graph('me', array(
      'access_token' => $token,
    ));

    // Ideally, we could inspect $me to determine whether it is a user account or something else (i.e. a page).  Until we figure out how to do that, we use the fact that me/accounts can be queried for users, but pages will throw exception.
    // "to" options are facebook ids.
    $to_options[$me['id']] = _fb_get_name($me);

    // "from" options are ids.
    $from_options[$me['id']] = _fb_get_name($me);

    // We will also need the access token that corresponds to "from" options.
    $from_tokens[$me['id']] = $token;
    try {
      $accounts = fb_graph('me/accounts', array(
        'access_token' => $token,
      ));
      foreach ($accounts['data'] as $account) {

        // @TODO add only if access_token found
        if (!isset($account['name'])) {

          // @TODO handle applications more smarter.
          $name = $account['category'] . ' ' . $account['id'];
        }
        else {
          $name = $account['name'];
        }
        $to_options[$account['id']] = $name;
        if (!empty($account['access_token'])) {
          $from_options[$account['id']] = $name;
          $from_tokens[$account['id']] = $account['access_token'];
        }
      }
    } catch (Exception $e) {

      // If we could not query me/accounts, we only have one option.
    }
  } catch (Exception $e) {

    // TODO: link to token admin page.
    fb_log_exception($e, t('Failed to get facebook post options.'));
  }
}