You are here

function modernizr_page_build in Modernizr 7.3

Same name and namespace in other branches
  1. 8 modernizr.module \modernizr_page_build()

Implements hook_page_build().

We used to use hook_init(), but that loads the JS files needlessly on AJAX requests, private file requests, etc.

File

./modernizr.module, line 57
Main module file for Modernizr

Code

function modernizr_page_build(&$page) {
  global $base_url;
  $modernizr_js_settings = _modernizr_js_settings();

  // Load Modernizr on the page by invoking our implementation of
  // hook_libraries_info().
  //
  // We can only use this method when Libraries API 2.0 is installed. Since Libraries 1.0
  // did not contain a function called libraries_load(), we must explicitly check for a
  // valid function to avoid fatal errors.
  //
  // @see http://drupal.org/node/1919796
  if (module_exists('libraries') && function_exists('libraries_load')) {
    libraries_load('modernizr');
  }
  else {

    // First, figure out if we're inlining.
    if (in_array(variable_get('modernizr_type', MODERNIZR_TYPE_DEFAULT), array(
      'sync',
      'defer',
    ))) {

      // We are loading external script. Load file path.
      $modernizr_file = modernizr_get_path();
    }
    else {

      // We are inlining. Load contents of file instead of path.
      $modernizr_file = file_get_contents(modernizr_get_path());
    }

    // With no Libraries API, load the regular way.
    drupal_add_js($modernizr_file, $modernizr_js_settings);
  }

  // We want yepnope() commands to be issued immediately after the call
  // to Modernizr so that they download while the page renders. The overrides
  // to $inline_js_settings will format the output as inline JS.
  if ($output = _modernizr_load_generate()) {

    // Modernizr v3 removed the ability to include yepnope.js directly in the
    // custom builds. To ensure that previous users of this module can continue
    // without breaking changes, we need to load a copy of yepnope manually,
    // which Modernizr detects and aliases to yepnope().
    if ($output && variable_get('modernizr_cb_load', MODERNIZR_YEPNOPE_DEFAULT)) {
      $yepnope_settings = $modernizr_js_settings;
      $yepnope_settings['type'] = 'inline';
      $yepnope_settings['weight'] = MODERNIZR_SCRIPT_WEIGHT - 1;

      // yepnope.js
      drupal_add_js(file_get_contents(drupal_get_path('module', 'modernizr') . '/js/yepnope.js'), $yepnope_settings);
      $inline_js_settings = $modernizr_js_settings;
      $inline_js_settings['type'] = 'inline';
      $inline_js_settings['weight'] = MODERNIZR_SCRIPT_WEIGHT + 1;

      // yepnope() statements
      drupal_add_js($output, $inline_js_settings);
    }
    else {
      if ($output && !variable_get('modernizr_cb_load', MODERNIZR_YEPNOPE_DEFAULT)) {
        drupal_add_js('console.warn("The Modernizr module is receiving requests to use yepnope.js but that option is currently disabled. Please enable yepnope.js by loading ' . $base_url . '/admin/config/development/modernizr/settings' . '");', array(
          'type' => 'inline',
        ));
      }
    }
  }
}