function magic_experimental_js in Magic 7.2
Same name and namespace in other branches
- 7 includes/scripts-experimental.inc \magic_experimental_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 magic_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.
Parameters
$scope: (optional) The scope for which the JavaScript rules should be returned. Defaults to 'header'.
$javascript: (optional) An array with all JavaScript code. Defaults to the default JavaScript array for the given scope.
$skip_alter: (optional) If set to TRUE, this function skips calling drupal_alter() on $javascript, useful when the calling function passes a $javascript array that has already been altered.
Return value
All JavaScript code segments and includes for the scope as HTML tags.
See also
1 call to magic_experimental_js()
- magic_template_process_html_override in ./
magic.module - Overrides template_process_html().
2 string references to 'magic_experimental_js'
- magic_form_system_theme_settings in includes/
magic.settings.inc - Implements hook_form_alter()-ish.
- magic_template_process_html_override in ./
magic.module - Overrides template_process_html().
File
- includes/
scripts-experimental.inc, line 41 - A file to contain functions for the magic module to abuse.
Code
function magic_experimental_js($scope = 'header', $javascript = NULL, $skip_alter = FALSE) {
if (!isset($javascript)) {
$javascript = magic_add_js();
}
if (empty($javascript)) {
return '';
}
// Allow modules to alter the JavaScript.
if (!$skip_alter) {
drupal_alter('js', $javascript);
}
// Check to see if Force Header is available and set to true.
// If it is neither enabled nor set to true, change scope to footer,
// otherwise, keep it in the header.
// Done again here because scope may change on alter.
foreach ($javascript as $js_key => $js_value) {
if (!empty($js_value['force header'])) {
$javascript[$js_key]['scope'] = 'header';
}
else {
$javascript[$js_key]['scope'] = 'footer';
}
}
// Check to see if Force Header is available and set to true.
// If it is neither enabled nor set to true, change scope to footer, otherwise, keep it in the header.
// Done here because scope may change on alter. Screw Inversion of control.
// Get Magic Library Head variable for current theme.
$library_head = theme_get_setting('magic_library_head');
$footer_js = theme_get_setting('magic_footer_js');
if ($footer_js) {
foreach ($javascript as $js_key => $js_value) {
if (!empty($js_value['force header']) && $js_value['force header']) {
$javascript[$js_key]['scope'] = 'header';
}
else {
$javascript[$js_key]['scope'] = 'footer';
}
if ($js_value['group'] == JS_LIBRARY && $library_head) {
$javascript[$js_key]['scope'] = 'header';
}
if ($js_key === 'settings') {
$javascript[$js_key]['scope'] = 'header';
}
}
}
// Filter out elements of the given scope.
$items = array();
foreach ($javascript as $key => $item) {
if ($item['scope'] == $scope) {
$items[$key] = $item;
}
}
// Sort the JavaScript so that it appears in the correct order.
uasort($items, 'magic_sort_css_js');
// 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.
foreach (array_keys($items) as $key) {
if (!empty($items[$key]['type']) && $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.
$setting['ajaxPageState']['js'] = array_fill_keys(array_keys($items), 1);
unset($setting['ajaxPageState']['js']['settings']);
drupal_add_js($setting, 'setting');
// If we're outputting the header scope, then this might 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.
if ($scope == 'header' && isset($items['settings'])) {
$items['settings']['data'][] = $setting;
}
// Render the HTML needed to load the JavaScript.
$elements = array(
'#type' => 'scripts',
'#items' => $items,
);
return drupal_render($elements);
}