You are here

function jqmulti_js_alter in jQuery Multi 7

Implements hook_js_alter().

This is where the magic happens. The new jQuery is added and noConflict() is run. Finally, scripts are rearranged or added so that the targeted scripts run with the newer jQuery.

File

./jqmulti.module, line 47
Code for the jQuery Multi module.

Code

function jqmulti_js_alter(&$javascript) {
  $jquery_path = jqmulti_jquery_path();
  if (!$jquery_path) {
    return;
  }

  // Get info about which files are needed.
  $files = jqmulti_get_files();

  // Check if we need to add libraries even if they have not been explicitly added.
  if (!variable_get('jqmulti_load_files_always', FALSE)) {
    $files = array_intersect_key($javascript, $files);
  }

  // If there are no files, check if we are to add jQuery anyway.
  if (empty($files)) {
    if (!variable_get('jqmulti_load_always', FALSE)) {
      return;
    }
  }

  // Give the files we're adding a special group value
  foreach ($files as $file => $options) {
    $files[$file]['group'] = JQMULTI_FILES_GROUP;
  }

  // Add jQuery and the switch script to the $files array
  $jquery = array(
    $jquery_path => array(
      'weight' => -100,
      'group' => JQMULTI_JQUERY_GROUP,
    ),
  );
  $switch = array(
    jqmulti_switch_path() => array(
      'weight' => 100,
      'group' => JQMULTI_SWITCH_GROUP,
    ),
  );
  $files = array_merge($jquery, $files, $switch);
  foreach ($files as $file => $options) {
    if (!array_key_exists($file, $javascript)) {
      $javascript[$file] = drupal_js_defaults();
      $javascript[$file]['data'] = $file;
    }
    if (isset($options['group'])) {
      $javascript[$file]['group'] = $options['group'];
    }
    if (isset($options['weight'])) {
      $javascript[$file]['weight'] = $options['weight'];
    }
  }
}