You are here

function js_injector_init in JS injector 7.2

Same name and namespace in other branches
  1. 6.2 js_injector.module \js_injector_init()
  2. 6 js_injector.module \js_injector_init()
  3. 7 js_injector.module \js_injector_init()

Implements hook_init().

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 132
js_injector.module

Code

function js_injector_init() {

  // load all the rules
  ctools_include('export');
  $rules = ctools_export_load_object('js_injector_rule');
  foreach ($rules as $name => $rule) {

    // check if the rule is disabled in the ctools UI, if so skip it
    if (isset($rule->disabled) && $rule->disabled == TRUE) {
      continue;
    }

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

    // add the js
    $code = $rule->inline == 1 ? $rule->js : _js_injector_rule_path($rule->crid);
    drupal_add_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 200 + the crid, which is
      // currently higher than Bartik's JS.
      'weight' => 200 + $rule->crid,
    ));
    _js_inject_active_rules($rule);
  }
}