You are here

function js_injector_page_build in JS injector 8

Same name and namespace in other branches
  1. 7.2 js_injector.module \js_injector_page_build()

Implements hook_page_build().

Checks to see whether any JS files should be added to the current page, based on rules configured by the site administrator.

File

./js_injector.module, line 60
js_injector.module

Code

function js_injector_page_build(&$page) {

  // Load all the js_injector rules. These are loaded automatically by weight.
  $rules = entity_load_multiple('js_injector_rule');
  foreach ($rules as $id => $rule) {

    // Check if the rule is disabled in the administrative UI.
    if ($rule->status == FALSE) {
      continue;
    }

    // Check the page visibility settings.
    if (_js_injector_visibility_pages($rule) == FALSE) {
      continue;
    }

    // Add the JavaScript to the page.
    $code = $rule->inline == 1 ? $rule->js : _js_injector_rule_path($id);
    $page['#attached']['js'][$code] = array(
      'type' => $rule->inline == 1 ? 'inline' : 'file',
      'scope' => $rule->position,
      // this group has the highest weight
      'group' => JS_THEME,
      'every_page' => FALSE,
      // safe guard to ensure inline files are never preprocessed
      'preprocess' => $rule->inline == 1 ? FALSE : $rule->preprocess,
      // since we're trying to give the administrator complete control, we'll
      // move JS that this module has added to a high weight, higher even than
      // the theme's JS files. Currently the weight is 250 + the weight, which
      // is currently higher than Bartik's JS.
      'weight' => 250 + $rule->weight,
    );
  }
}