You are here

function advagg_process_js in Advanced CSS/JS Aggregation 6

Same name and namespace in other branches
  1. 7 advagg.module \advagg_process_js()

Returns a themed presentation of all JavaScript code for the current page.

References to JavaScript files are placed in a certain order: first, all 'core' files, then all 'module' and finally all 'theme' JavaScript files are added to the page. Then, all settings are output, followed by 'inline' JavaScript code. If running update.php, all preprocessing is disabled.

Parameters

$js_code: An array with all JavaScript code. Key it the region

$noagg: (optional) Bool indicating that aggregation should be disabled if TRUE.

Return value

All JavaScript code segments and includes for the scope as HTML tags.

See also

drupal_get_js()

2 calls to advagg_process_js()
advagg_get_js_css_get_array in ./advagg.module
Return a large array of the CSS & JS files loaded on this page.
advagg_processor in ./advagg.module
Process variables for page.tpl.php

File

./advagg.module, line 2375
Advanced CSS/JS aggregation module

Code

function advagg_process_js($master_set, $noagg = FALSE) {
  global $conf;
  if ((!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update') && function_exists('locale_update_js_files')) {
    locale_update_js_files();
  }

  // Get useful info.
  list($preprocess_js, $public_downloads, $query_string) = advagg_process_css_js_prep($noagg, 'js');
  $advagg_json_encode_function = variable_get('advagg_json_encode_function', 'advagg_drupal_to_js');
  $output = array();
  foreach ($master_set as $scope => $javascript) {
    if ($scope != 'header' && $scope != 'footer' && empty($javascript)) {
      continue;
    }

    // Invoke hook_advagg_js_pre_alter() to give installed modules a chance to
    // modify the data in the $javascript array if necessary.
    drupal_alter('advagg_js_pre', $javascript, $preprocess_js, $public_downloads, $scope);
    $master_set[$scope] = $javascript;
  }

  // Invoke hook_advagg_js_header_footer_alter() to give installed modules a chance to
  // modify the data in the header and footer JS if necessary.
  drupal_alter('advagg_js_header_footer', $master_set, $preprocess_js, $public_downloads);
  foreach ($master_set as $scope => $javascript) {
    if (empty($javascript)) {
      continue;
    }

    // Set variables.
    $setting_no_preprocess = array();
    $inline_no_preprocess = array();
    $external_no_preprocess = array();
    $output_no_preprocess = array(
      'core' => array(),
      'module' => array(),
      'theme' => array(),
    );
    $output_preprocess = array();
    $preprocess_list = array();
    $js_settings_array = array();
    $inline_included = array();
    $files_included = array();
    $files_aggregates_included = array();

    // Process input.
    foreach ($javascript as $type => $data) {
      if (empty($data)) {
        continue;
      }

      // Add the prefix and suffix to the info array if not there.
      if ($type != 'setting') {
        foreach ($data as &$info) {
          $info['prefix'] = isset($info['prefix']) ? $info['prefix'] : '';
          $info['suffix'] = isset($info['suffix']) ? $info['suffix'] : '';
        }

        // $info needs to be unset, otherwise foreach loops below will break.
        unset($info);
      }
      switch ($type) {
        case 'setting':
          $data = call_user_func_array('array_merge_recursive', $data);
          $js_settings_array[] = $data;
          $js_settings = $advagg_json_encode_function($data);
          $js_settings = preg_replace(array(
            '/"DRUPAL_JS_RAW\\:/',
            '/\\:DRUPAL_JS_RAW"/',
          ), array(
            '',
            '',
          ), $js_settings);
          $setting_no_preprocess[] = 'jQuery.extend(Drupal.settings, ' . $js_settings . ");";
          break;
        case 'inline':
          foreach ($data as $info) {

            // Invoke hook_advagg_js_inline_alter() to give installed modules a
            // chance to modify the contents of $info['code'] if necessary.
            drupal_alter('advagg_js_inline', $info['code']);
            $inline_no_preprocess[] = array(
              $info['code'],
              $info['defer'],
              $info['prefix'],
              $info['suffix'],
            );
            $inline_included[] = $info['code'];
          }
          break;
        case 'external':
          foreach ($data as $path => $info) {
            $external_no_preprocess[] = array(
              $path,
              $info['defer'],
              $info['prefix'],
              $info['suffix'],
            );
            $files_included[$path] = TRUE;
          }
          break;
        default:

          // If JS preprocessing is off, we still need to output the scripts.
          // Additionally, go through any remaining scripts if JS preprocessing is on and output the non-cached ones.
          foreach ($data as $path => $info) {
            if (!$info['preprocess'] || !$public_downloads || !$preprocess_js) {
              $output_no_preprocess[$type][] = array(
                advagg_build_uri($path) . ($info['cache'] ? $query_string : '?' . time()),
                $info['defer'],
                $info['prefix'],
                $info['suffix'],
              );
              $files_included[$path] = $info['preprocess'];
            }
            else {
              $preprocess_list[$path] = $info;
            }
          }
          break;
      }
    }

    // Aggregate any remaining JS files that haven't already been output.
    if ($public_downloads && $preprocess_js && count($preprocess_list) > 0) {
      $files_aggregates_included = $files_included;
      $files = array();
      foreach ($preprocess_list as $path => $info) {
        if ($info['preprocess']) {
          $files[] = $path;
          $files_included[$path] = TRUE;
        }
      }
      if (!empty($files)) {
        $preprocess_files = advagg_css_js_file_builder('js', $files, $query_string);
        if (!empty($preprocess_files)) {
          $good = TRUE;
          foreach ($preprocess_files as $preprocess_file => $extra) {

            // Empty aggregate, skip
            if (empty($preprocess_file)) {
              continue;
            }
            if ($extra !== FALSE && is_array($extra)) {
              $prefix = $extra['prefix'];
              $suffix = $extra['suffix'];
              $output_preprocess[] = array(
                advagg_build_uri($preprocess_file),
                $prefix,
                $suffix,
              );
              $files_aggregates_included[$preprocess_file] = $extra;
            }
            else {
              $good = FALSE;
              break;
            }
          }
        }
        if (empty($good)) {

          // Redo with aggregation turned off and return the new value.
          watchdog('advagg', 'JS aggregation failed. %filename could not be saved correctly.', array(
            '%filename' => $preprocess_file,
          ), WATCHDOG_ERROR);
          $data = advagg_process_js($master_set, TRUE);
          return $data;
        }
      }
    }

    // Default function called: advagg_js_builder
    $function = variable_get('advagg_js_render_function', ADVAGG_JS_RENDER_FUNCTION);
    $output[$scope] = $function($external_no_preprocess, $output_preprocess, $output_no_preprocess, $setting_no_preprocess, $inline_no_preprocess, $scope, $js_settings_array, $inline_included, $files_included, $files_aggregates_included);
  }
  return $output;
}