You are here

function fb_example_fb in Drupal for Facebook 7.3

Same name and namespace in other branches
  1. 6.3 contrib/fb_example.module \fb_example_fb()

Implements hook_fb().

This hook provides an opportunity to customize the behavior of Facebook Applications.

Parameters

$op: Indicates what operation is currently being performed, or which behavior can be customized. There are a number of these. In some cases, modules/fb is informing other modules, and in other operations it is asking for information.

  • FB_OP_INITIALIZE - The facebook sdk has been initialized. This tells a facebook application is enable for the current request.
  • FB_OP_APP_IS_AUTHORIZED - The visitor to this page has authorized the application.

$data: Associative array of information specific to this operation. Usually, but not always, contains:

  • 'fb' - The API provided by the facebook-php-sdk.
  • 'fb_app' - The data about this application stored by fb_app.module.
  • 'fbu' - If the current user is known, their facebook id.

$return: An op-specific return value. Your hook should change this reference variable, and not return it. Some operations return an array of data, which may be collaboratively built by multiple implementations of this hook.

Note, some example code is disabled. Change the FALSE to TRUE in variable_get calls to test the code.

File

contrib/fb_example.module, line 302
Example customizations to modules/fb.

Code

function fb_example_fb($op, $data, &$return) {
  $fb_app = isset($data['fb_app']) ? $data['fb_app'] : NULL;
  $fb = isset($data['fb']) ? $data['fb'] : NULL;

  // Uncomment next line to figure out when this hook is called and what it is
  // passed.
  // dpm(func_get_args(), "fb_example_fb($op) called"); // debug.
  if ($op == FB_OP_AJAX_EVENT) {

    // We get FB_OP_AJAX_EVENT when fb.js calls us in reponse to a javascript
    // event.
    if ($data['event_type'] == 'edge.create' && variable_get('fb_example_like_thanks', FALSE)) {

      // Facebook calls this event 'edge.create', because there's a new 'edge'
      // in the open graph.
      drupal_set_message(t('Thanks for clicking Like!'));

      // Reloading allows user to see message.  This example is contrived,
      // there's no real reason to reload page here, and it prevents users
      // from adding a comment.  In practice, you could put some other
      // javascript here.
      $return[] = "FB_JS.reload();";
    }
    if ($data['event_type'] == 'session_change' && variable_get('fb_example_session_change_redirect', FALSE)) {
      if (!isset($GLOBALS['fb_example_new_user'])) {

        // The user has clicked the connect button, or logged into/out-of
        // facebook in another browser window, then refreshed.
        if ($fbu = fb_facebook_user()) {

          // The user has connected (as opposed to logged out).  Let's redirect
          // them to our 'welcome' page.  Replace 'welcome' with the path you
          // really want.
          $url = url('welcome', array(
            'absolute' => TRUE,
            'fb_canvas' => fb_is_canvas(),
          ));

          // We return javascript to be evaluated by fb.js.
          $return[] = "FB_JS.reload('{$url}');";
        }
      }
      elseif ($GLOBALS['fb_example_new_user']) {

        // Send the user to their edit page.
        $url = url('user/' . $GLOBALS['user']->uid . '/edit', array(
          'absolute' => TRUE,
          'fb_canvas' => fb_is_canvas(),
        ));

        // We return javascript to be evaluated by fb.js.
        $return[] = "FB_JS.reload('{$url}');";
      }
    }
  }
}