Same name in other branches
  1. 7.x-1.x advagg.module \advagg_get_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.

Note that hook_js_alter(&$javascript) is called during this function call to allow alterations of the JavaScript during its presentation. Calls to drupal_add_js() from hook_js_alter() will not be added to the output presentation. The correct way to add JavaScript during hook_js_alter() is to add another element to the $javascript array, deriving from drupal_js_defaults(). See locale_js_alter() for an example of this.

Paramètres

string $scope: (optional) The scope for which the JavaScript rules should be returned. Defaults to 'header'.

array $javascript: (optional) An array with all JavaScript code. Defaults to the default JavaScript array for the given scope.

bool $ajax: (optional) If set to TRUE, this function will not output Drupal.settings.

Return value

array An array ready to be passed into drupal_render() containing all JavaScript code segments and includes for the scope as HTML tags.

Voir aussi

drupal_add_js()

locale_js_alter()

drupal_js_defaults()

5 calls to advagg_get_js()
AdvAggJavaScriptTestCase::testJavaScriptAlwaysUsejQuery dans tests/advagg.test
Test the 'javascript_always_use_jquery' variable.
AdvAggThemeTestCase::testCssOverride dans tests/advagg.test
Check theme and ajax functions and commands.
advagg_ajax_render_alter dans ./advagg.module
Implements hook_ajax_render_alter().
imce-page.tpl.php dans tpl/imce-page.tpl.php
_advagg_build_js_arrays_for_rendering dans ./advagg.module
Builds the arrays needed for js rendering and caching.
1 string reference to 'advagg_get_js'
advagg_test_reset_statics dans tests/advagg.test
Resets static variables related to adding CSS or JS to a page.

Fichier

./advagg.module, line 2910

Code

function advagg_get_js($scope = 'header', array $javascript = array(), $ajax = FALSE) {
    // Keep track of js added for ajaxPageState.
    $page_state =& drupal_static(__FUNCTION__, array());
    // Add in javascript if none was passed in.
    if (empty($javascript) && !$ajax) {
        $javascript = advagg_get_full_js();
    }
    // Return an empty array if no javascript is used.
    if (empty($javascript)) {
        return array();
    }
    // Filter out elements of the given scope.
    $items = array();
    foreach ($javascript as $key => $item) {
        if (!empty($item['scope']) && $item['scope'] === $scope) {
            $items[$key] = $item;
        }
    }
    // Sort the JavaScript so that it appears in the correct order.
    advagg_drupal_sort_css_js_stable($items);
    // In Drupal 8, there's a JS_SETTING group for making setting variables
    // appear last after libraries have loaded. In Drupal 7, this is forced
    // without that group. We do not use the $key => $item type of iteration,
    // because PHP uses an internal array pointer for that, and we're modifying
    // the array order inside the loop.
    if ($scope === 'footer' && !empty($items['settings'])) {
        // Remove settings array from items.
        $settings_js['settings'] = $items['settings'];
        unset($items['settings']);
        // Move $settings_js to the bottom of the js that was added to the
        // header, but has now been moved to the footer via advagg_mod.
        $counter = 0;
        foreach ($items as $key => $item) {
            if ($item['group'] > 9000) {
                advagg_array_splice_assoc($items, $counter, 0, $settings_js);
                unset($settings_js);
                break;
            }
            ++$counter;
        }
        // Nothing in the footer, add settings to the bottom of the array.
        if (isset($settings_js)) {
            $items = array_merge($items, $settings_js);
        }
    }
    else {
        foreach (array_keys($items) as $key) {
            if ($items[$key]['type'] === 'setting') {
                $item = $items[$key];
                unset($items[$key]);
                $items[$key] = $item;
            }
        }
    }
    // Provide the page with information about the individual JavaScript files
    // used, information not otherwise available when aggregation is enabled.
    $page_state = array_merge($page_state, array_fill_keys(array_keys($items), 1));
    // If we're outputting the header scope, then this should be the final time
    // that drupal_get_js() is running, so add the setting to this output as well
    // as to the drupal_add_js() cache. If $items['settings'] doesn't exist, it's
    // because drupal_get_js() was intentionally passed a $javascript argument
    // stripped of settings, potentially in order to override how settings get
    // output, so in this case, do not add the setting to this output.
    // Also output the settings if we have pushed all javascript to the footer.
    if (isset($items['settings'])) {
        $items['settings']['data'][] = array(
            'ajaxPageState' => array(
                'js' => $page_state,
            ),
        );
    }
    // Do not include jQuery.extend(Drupal.settings) if the output is ajax.
    if ($ajax) {
        unset($items['settings']['data']);
    }
    // Semi support of the attributes array.
    foreach ($items as $key => $item) {
        if (!isset($item['attributes'])) {
            continue;
        }
        if (isset($item['attributes']['defer'])) {
            $items[$key]['defer'] = $item['attributes']['defer'];
        }
        if (isset($item['attributes']['async'])) {
            $items[$key]['async'] = $item['attributes']['async'];
        }
        if (isset($item['attributes']['onload'])) {
            $items[$key]['onload'] = $item['attributes']['onload'];
        }
        if (isset($item['attributes']['onerror'])) {
            $items[$key]['onerror'] = $item['attributes']['onerror'];
        }
    }
    // Render the HTML needed to load the JavaScript.
    $elements = array(
        '#type' => 'scripts',
        '#items' => $items,
    );
    // Aurora and Omega themes uses alter without checking previous value.
    if (variable_get('advagg_enforce_scripts_callback', TRUE)) {
        // Get the element_info for scripts.
        $scripts = element_info('scripts');
        if (empty($scripts) || $scripts['#aggregate_callback'] !== '_advagg_aggregate_js') {
            // Directly alter the static.
            $element_info =& drupal_static('element_info');
            advagg_element_info_alter($element_info);
            if (function_exists('advagg_mod_element_info_alter')) {
                advagg_mod_element_info_alter($element_info);
            }
        }
    }
    // Remove ajaxPageState CSS/JS from Drupal.settings if ajax.js is not used.
    if (function_exists('advagg_mod_js_no_ajaxpagestate')) {
        if (variable_get('advagg_mod_js_no_ajaxpagestate', ADVAGG_MOD_JS_NO_AJAXPAGESTATE)) {
            advagg_mod_js_no_ajaxpagestate($elements);
        }
    }
    return $elements;
}