Default callback to group JavaScript items.

This function arranges the JavaScript items that are in the #items property of the scripts element into groups. When aggregation is enabled, files within a group are aggregated into a single file, significantly improving page loading performance by minimizing network traffic overhead.

This function puts multiple items into the same group if they are groupable and if they are for the same browsers. Items of the 'file' type are groupable if their 'preprocess' flag is TRUE. Items of the 'inline', 'settings', or 'external' type are not groupable.

This function also ensures that the process of grouping items does not change their relative order. This requirement may result in multiple groups for the same type and browsers, if needed to accommodate other items in between.

Paramètres

array $javascript: An array of JavaScript items, as returned by drupal_add_js(), but after alteration performed by drupal_get_js().

Return value

array An array of JavaScript groups. Each group contains the same keys (e.g., 'data', etc.) as a JavaScript item from the $javascript parameter, with the value of each key applying to the group as a whole. Each group also contains an 'items' key, which is the subset of items from $javascript that are in the group.

Voir aussi

drupal_pre_render_scripts()

1 call to advagg_group_js()
advagg_relocate_advagg_relocate_process_http_request_alter dans advagg_relocate/advagg_relocate.advagg.inc
Implements hook_advagg_relocate_process_http_request_alter().
1 string reference to 'advagg_group_js'
advagg_element_info_alter dans ./advagg.module
Implements hook_element_info_alter().

Fichier

./advagg.module, line 4565

Code

function advagg_group_js(array $javascript) {
    $groups = array();
    // If a group can contain multiple items, we track the information that must
    // be the same for each item in the group, so that when we iterate the next
    // item, we can determine if it can be put into the current group, or if a
    // new group needs to be made for it.
    $current_group_keys = NULL;
    $index = -1;
    foreach ($javascript as $key => $item) {
        if (empty($item)) {
            continue;
        }
        // The browsers for which the JavaScript item needs to be loaded is part of
        // the information that determines when a new group is needed, but the order
        // of keys in the array doesn't matter, and we don't want a new group if all
        // that's different is that order.
        if (isset($item['browsers'])) {
            ksort($item['browsers']);
        }
        else {
            $item['browsers'] = array();
        }
        // Fix missing types.
        if (empty($item['type'])) {
            // Setting is easy.
            if ($key === 'settings') {
                $item['type'] = 'setting';
            }
            elseif (stripos($item['data'], 'http://') === 0 || stripos($item['data'], 'https://') === 0 || strpos($item['data'], '//') === 0 && strpos($item['data'], '///') === FALSE) {
                $item['type'] = 'external';
            }
            elseif (strpos($item['data'], ';') !== FALSE || strpos($item['data'], "\n") || strpos($item['data'], "\$") || strpos($item['data'], "'") || strpos($item['data'], '"')) {
                $item['type'] = 'inline';
            }
            elseif (stripos(strrev($item['data']), strrev('.js')) === 0) {
                $item['type'] = 'file';
            }
        }
        switch ($item['type']) {
            case 'file':
                // Group file items if their 'preprocess' flag is TRUE.
                // Help ensure maximum reuse of aggregate files by only grouping
                // together items that share the same 'group' value and 'every_page'
                // flag. See drupal_add_js() for details about that.
                $group_keys = !empty($item['preprocess']) ? array(
                    $item['type'],
                    $item['group'],
                    $item['every_page'],
                    $item['browsers'],
                ) : FALSE;
                break;
            case 'external':
            case 'setting':
            case 'inline':
                // Do not group external, settings, and inline items.
                $group_keys = FALSE;
                break;
            default:
                // Define this here so we don't get undefined alerts down below.
                $group_keys = NULL;
                // Log the error as well.
                watchdog('advagg', 'Bad javascript was added. Type is unknown. @key - @item', array(
                    '@key' => $key,
                    '@item' => print_r($item, TRUE),
                ), WATCHDOG_NOTICE);
                break;
        }
        // If the group keys don't match the most recent group we're working with,
        // then a new group must be made.
        if ($group_keys !== $current_group_keys) {
            ++$index;
            // Initialize the new group with the same properties as the first item
            // being placed into it. The item's 'data' and 'weight' properties are
            // unique to the item and should not be carried over to the group.
            $groups[$index] = $item;
            unset($groups[$index]['data'], $groups[$index]['weight']);
            $groups[$index]['items'] = array();
            $current_group_keys = $group_keys ? $group_keys : NULL;
        }
        // Add the item to the current group.
        $groups[$index]['items'][] = $item;
    }
    return $groups;
}