You are here

function fb_connect_footer in Drupal for Facebook 5.2

Same name and namespace in other branches
  1. 6.2 fb_connect.module \fb_connect_footer()

Include facebook javascript in the footer of the current page.

File

./fb_connect.module, line 177
Support for Facebook Connect features

Code

function fb_connect_footer($is_front) {

  // Do nothing on FBML pages
  if (function_exists('fb_canvas_is_fbml') && fb_canvas_is_fbml()) {
    return;
  }
  global $base_path;
  $feature_data = fb_connect_require_feature();
  $option_data = fb_connect_init_option();
  if (count($feature_data)) {
    foreach ($feature_data as $data) {

      // Give other modules a chance to add javascript which executes after init.
      $extra = fb_invoke(FB_OP_CONNECT_JS_INIT, $data, array());
      $extra_js = implode("\n", $extra);
      $fb_app = $data['fb_app'];
      $features = $data['features'];
      $options = json_encode($option_data[$fb_app->apikey]);

      // Hack!  What's the way to json_encode a function name?
      $options = str_replace('"{', '', $options);
      $options = str_replace('}"', '', $options);

      // drupal_add_js cannot add external javascript, so we use hook_footer instead.
      $output = '<script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php" type="text/javascript"></script>';
      $output .= "\n";
      $feature_list = '["' . implode('","', $features) . '"]';

      // Put together the URL for the receiver.  The prefix must be identical to the apps callback URL.
      $receiver = fb_get_callback_url($fb_app) . "fb_connect/receiver";
      $output .= "\n<script type=\"text/javascript\">\n  \$(document).ready(function() {\n    FB_RequireFeatures({$feature_list}, function () {\n\n      //FB.FBDebug.logLevel = 4;\n      //FB.FBDebug.isEnabled = true;\n\n      FB.init(\"{$fb_app->apikey}\", \"{$receiver}\", {$options});\n    });\n  });\n";

      // Extra JS after successful fbConnect init.
      $output .= "\n  FB.ensureInit(function()\n    {\n      fb_connect_init();\n      {$extra_js}\n    });\n";
      $output .= "\n</script>\n";
    }
  }
  return $output;
}