You are here

function fb_social_page_alter in Facebook social plugins integration 7.2

Implements hook_page_alter().

File

./fb_social.module, line 583

Code

function fb_social_page_alter(&$page) {
  global $language, $_fb_script_init, $base_url;

  // If their is no fb plugin enabled on this page, do not load fb script then
  if (!$_fb_script_init) {
    return;
  }
  $languages = _map_active_languages();
  if (fb_social_auto_language()) {
    if (array_key_exists($language->language, $languages)) {
      $fb_locale = $languages[$language->language];
    }
    else {
      drupal_set_message(t("There is no mapping for the current language. Using the default locale."));
    }
  }
  else {
    $fb_locale = variable_get('fb_social_locale', 'en_US');
  }
  $appid = variable_get('fb_social_appid', '');
  $channel_url = $base_url . "/fb_social/channel";
  $output = '<div id="fb-root"></div>';
  $output .= "<script type=\"text/javascript\">\n     window.fbAsyncInit = function() {\n       FB.init({\n         appId: " . drupal_json_encode($appid) . ",\n         status: true,\n         cookie: true,\n         xfbml: true,\n         oauth : true,\n         channelUrl: " . drupal_json_encode($channel_url) . "\n         });\n  \n         ";

  // let each module subscribe to different events
  $output .= implode("\n", module_invoke_all('fb_social_event_subscribe'));
  $output .= "\n     };\n     (function() {\n       var e = document.createElement('script');\n       e.async = true;\n       e.src = document.location.protocol + '//connect.facebook.net/" . $fb_locale . "/all.js';\n       document.getElementById('fb-root').appendChild(e);\n     }());\n  </script>";

  // Allow the location to be changed, if necessary, but default to load at the
  // top of the page per the official Facebook JavaScript SDK documentation:
  // http://developers.facebook.com/docs/reference/javascript/
  if (variable_get('fb_social_fbroot_top', TRUE)) {
    $location = 'page_top';
  }
  else {
    $location = 'page_bottom';
  }
  $page[$location]['fb_social'] = array(
    '#markup' => $output,
  );
  if (variable_get('fb_social_opengraph', 1)) {
    $content = drupal_get_title();
    $content = $content ? strip_tags($content) : variable_get('site_name', 'Drupal');

    //add fbml header meta information
    $element = array(
      '#tag' => 'meta',
      '#attributes' => array(
        'property' => 'og:title',
        'content' => $content,
      ),
    );
    drupal_add_html_head($element, 'fb_social_open_graph_title');
    $element = array(
      '#tag' => 'meta',
      '#attributes' => array(
        'property' => 'og:site_name',
        'content' => variable_get('site_name', 'Drupal'),
      ),
    );
    drupal_add_html_head($element, 'fb_social_open_graph_sitename');
  }

  /**
   * from facebook ..
   * If your site has many comments boxes, we strongly recommend you specify a Facebook app id
   * as the administrator (all administrators of the app will be able to moderate comments).
   * Doing this enables a moderator interface on Facebook where comments from all plugins
   * administered by your app id can be easily moderated together.
   */
  $element = array(
    '#tag' => 'meta',
    '#attributes' => array(
      'property' => 'fb:app_id',
      'content' => check_plain(variable_get('fb_social_appid', '')),
    ),
  );
  drupal_add_html_head($element, 'fb_social_fbadmin');
}