You are here

function google_plusone_page_alter in Google Plus One Button | Google+ Badge 7

Implements hook_page_alter(). Adds JavaScript to the appropriate scope/region of the page.

Note: It can't be added through drupal_add_js() due of the JavaScript object declared inside the <script> element.

1 call to google_plusone_page_alter()
google_plusone_demo_badge_preview in ./google_plusone.admin.inc
Page callback for previewing the Google+ badge in block configuration page. It's meant to be called from an iframe element

File

./google_plusone.module, line 249

Code

function google_plusone_page_alter(&$page) {

  // Add Javascript only in case there is a button to render.
  // Flag is set in the node_view hook.
  $add_js =& drupal_static('google_plusone_js_added', FALSE);
  if ($add_js) {
    $default_advanced = array(
      'lang' => '',
      'scope' => 'page_bottom',
      'parsetags' => 'onload',
      'async' => 1,
    );
    $config = array_merge($default_advanced, variable_get('google_plusone_advanced_settings', array()));
    $script_options = array();
    if (!empty($config['lang'])) {
      $lang = google_plusone_button_negotiate_language($config['lang']);
      $script_options[] = 'lang:"' . $lang . '"';
    }
    if ($config['parsetags'] !== 'onload') {
      $script_options[] = 'parsetags:"' . $config['parsetags'] . '"';
    }
    if ($config['async']) {
      $script = "<script>";
      $script .= empty($script_options) ? '' : "window.___gcfg = {" . implode(',', $script_options) . "};";
      $script .= "(function() {";
      $script .= "var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;";
      $script .= "po.src = 'https://apis.google.com/js/plusone.js';";
      $script .= "var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);";
      $script .= "})();</script>";
    }
    else {
      $script = '<script type="text/javascript" src="https://apis.google.com/js/plusone.js">';
      $script .= empty($script_options) ? '' : '{' . implode(',', $script_options) . '}';
      $script .= '</script>';
    }
    $page[$config['scope']]['google_plusone'] = array(
      '#markup' => $script,
    );
  }
}