Default callback to aggregate JavaScript files.

Having the browser load fewer JavaScript files results in much faster page loads than when it loads many small files. This function aggregates files within the same group into a single file unless the site-wide setting to do so is disabled (commonly the case during site development). To optimize download, it also compresses the aggregate files by removing comments, whitespace, and other unnecessary content.

Paramètres

array $js_groups: An array of JavaScript groups as returned by drupal_group_js(). For each group that is aggregated, this function sets the value of the group's 'data' key to the URI of the aggregate file.

Voir aussi

drupal_group_js()

drupal_pre_render_scripts()

1 call to _advagg_aggregate_js()
advagg_relocate_advagg_relocate_process_http_request_alter dans advagg_relocate/advagg_relocate.advagg.inc
Implements hook_advagg_relocate_process_http_request_alter().
2 string references to '_advagg_aggregate_js'
advagg_element_info_alter dans ./advagg.module
Implements hook_element_info_alter().
advagg_get_js dans ./advagg.module
Returns a themed presentation of all JavaScript code for the current page.

Fichier

./advagg.module, line 1684

Code

function _advagg_aggregate_js(array &$js_groups) {
    if (!advagg_enabled()) {
        if (function_exists('drupal_aggregate_js')) {
            return drupal_aggregate_js($js_groups);
        }
        else {
            return;
        }
    }
    if (variable_get('advagg_debug', ADVAGG_DEBUG)) {
        $GLOBALS['_advagg']['debug']['js_groups_before'][] = $js_groups;
    }
    $preprocess_js = advagg_file_aggregation_enabled('js');
    // Allow other modules to modify $js_groups right before it is processed.
    // Call hook_advagg_js_groups_alter().
    drupal_alter('advagg_js_groups', $js_groups, $preprocess_js);
    // For each group that needs aggregation, aggregate its items.
    $files_to_aggregate = array();
    // Only aggregate when the site is configured to do so, and not during an
    // update.
    $gap_counter = 0;
    if ($preprocess_js) {
        // Set boolean to TRUE if all JS in footer.
        $all_in_footer = FALSE;
        if (module_exists('advagg_mod') && variable_get('advagg_mod_js_footer', ADVAGG_MOD_JS_FOOTER) >= 2) {
            $all_in_footer = TRUE;
        }
        foreach ($js_groups as $key => &$group) {
            switch ($group['type']) {
                // If a file group can be aggregated into a single file, do so, and set
                // the group's data property to the file path of the aggregate file.
                case 'file':
                    if (!empty($group['preprocess'])) {
                        // Special handing for when all JS is in the footer.
                        if ($all_in_footer && $group['scope'] === 'footer' && $group['group'] > 9000) {
                            ++$gap_counter;
                            $all_in_footer = FALSE;
                        }
                        $files_to_aggregate[$gap_counter][$key] = $group;
                    }
                    else {
                        ++$gap_counter;
                    }
                    break;
                // Create a gap for inline JS.
                case 'inline':
                    ++$gap_counter;
                    break;
                // Create a gap for external JS.
                case 'external':
                    ++$gap_counter;
                    break;
            }
        }
        unset($group);
    }
    if (!empty($files_to_aggregate)) {
        $hooks_hash = advagg_get_current_hooks_hash();
        $serialize_function = variable_get('advagg_serialize', ADVAGG_SERIALIZE);
        $js_hash = drupal_hash_base64($serialize_function($files_to_aggregate));
        $cache_id = 'advagg:js:' . $hooks_hash . ':' . $js_hash;
        if (variable_get('advagg_cache_level', ADVAGG_CACHE_LEVEL) >= 1 && ($cache = cache_get($cache_id, 'cache_advagg_aggregates'))) {
            $plans = $cache->data;
        }
        else {
            module_load_include('inc', 'advagg', 'advagg');
            $plans = advagg_build_aggregate_plans($files_to_aggregate, 'js');
            if (!empty($plans) && variable_get('advagg_cache_level', ADVAGG_CACHE_LEVEL) >= 1) {
                cache_set($cache_id, $plans, 'cache_advagg_aggregates', CACHE_TEMPORARY);
            }
        }
        $js_groups = advagg_merge_plans($js_groups, $plans);
    }
    if (variable_get('advagg_debug', ADVAGG_DEBUG)) {
        $GLOBALS['_advagg']['debug']['js_groups_after'][] = $js_groups;
    }
}