Callback for pre_render to add elements needed for JavaScript to be rendered.
This function evaluates the aggregation enabled/disabled condition on a group by group basis by testing whether an aggregate file has been made for the group rather than by testing the site-wide aggregation setting. This allows this function to work correctly even if modules have implemented custom logic for grouping and aggregating files.
Paramètres
array $elements: A render array containing:
- #items: The JavaScript items as returned by drupal_add_js() and altered by drupal_get_js().
- #group_callback: A function to call to group #items. Following this function, #aggregate_callback is called to aggregate items within the same group into a single file.
- #aggregate_callback: A function to call to aggregate the items within the groups arranged by the #group_callback function.
Return value
array A render array that will render to a string of JavaScript tags.
Voir aussi
drupal_get_js()
2 string references to 'advagg_pre_render_scripts'
- advagg_element_info_alter dans ./
advagg.module - Implements hook_element_info_alter().
- advagg_mod_element_info_alter dans advagg_mod/
advagg_mod.module - Implements hook_element_info_alter().
Fichier
-
./
advagg.module, line 4079
Code
function advagg_pre_render_scripts(array $elements) {
// Don't run it twice.
if (!empty($elements['#groups'])) {
return $elements;
}
// Group and aggregate the items.
if (isset($elements['#group_callback'])) {
// Call advagg_group_js().
$elements['#groups'] = $elements['#group_callback']($elements['#items']);
}
if (isset($elements['#aggregate_callback'])) {
// Call _advagg_aggregate_js().
$elements['#aggregate_callback']($elements['#groups']);
}
// A dummy query-string is added to filenames, to gain control over
// browser-caching. The string changes on every update or full cache
// flush, forcing browsers to load a new copy of the files, as the
// URL changed. Files that should not be cached (see drupal_add_js())
// get REQUEST_TIME as query-string instead, to enforce reload on every
// page request.
$default_query_string = variable_get('css_js_query_string', '0');
// For inline JavaScript to validate as XHTML, all JavaScript containing
// XHTML needs to be wrapped in CDATA. To make that backwards compatible
// with HTML 4, we need to comment out the CDATA-tag.
$embed_prefix = "\n<!--//--><![CDATA[//><!--\n";
$embed_suffix = "\n//--><!]]>\n";
// Since JavaScript may look for arguments in the URL and act on them, some
// third-party code might require the use of a different query string.
$js_version_string = variable_get('drupal_js_version_query_string', 'v=');
// Defaults for each SCRIPT element.
$element_defaults = array(
'#type' => 'html_script_tag',
'#tag' => 'script',
'#value' => '',
'#attributes' => array(),
);
$hooks = theme_get_registry(FALSE);
if (empty($hooks['html_script_tag'])) {
$element_defaults['#type'] = 'html_tag';
}
// Loop through each group.
foreach ($elements['#groups'] as $group) {
// If a group of files has been aggregated into a single file,
// $group['data'] contains the URI of the aggregate file. Add a single
// script element for this file.
if (isset($group['type']) && $group['type'] === 'file' && isset($group['data'])) {
$element = $element_defaults;
$element['#attributes']['src'] = advagg_file_create_url($group['data']) . ($group['cache'] ? '' : '?' . REQUEST_TIME);
$element['#browsers'] = $group['browsers'];
if (!empty($group['defer'])) {
$element['#attributes']['defer'] = 'defer';
}
if (!empty($group['async'])) {
$element['#attributes']['async'] = 'async';
}
if (!empty($group['onload'])) {
if (!isset($element['#attributes']['onload'])) {
$element['#attributes']['onload'] = '';
}
$element['#attributes']['onload'] .= $group['onload'];
}
if (!empty($group['onerror'])) {
if (!isset($element['#attributes']['onerror'])) {
$element['#attributes']['onerror'] = '';
}
$element['#attributes']['onerror'] .= $group['onerror'];
}
if (!empty($group['attributes'])) {
$element['#attributes'] += $group['attributes'];
}
$elements[] = $element;
}
else {
foreach ($group['items'] as $item) {
// Skip if data is empty.
if (empty($item['data'])) {
continue;
}
// Element properties that do not depend on item type.
$element = $element_defaults;
if (!empty($item['defer'])) {
$element['#attributes']['defer'] = 'defer';
}
if (!empty($item['async'])) {
$element['#attributes']['async'] = 'async';
}
if (!empty($item['onload'])) {
if (!isset($element['#attributes']['onload'])) {
$element['#attributes']['onload'] = '';
}
$element['#attributes']['onload'] .= $item['onload'];
}
if (!empty($item['onerror'])) {
if (!isset($element['#attributes']['onerror'])) {
$element['#attributes']['onerror'] = '';
}
$element['#attributes']['onerror'] .= $item['onerror'];
}
if (!empty($group['attributes'])) {
$element['#attributes'] += $group['attributes'];
}
$element['#browsers'] = isset($item['browsers']) ? $item['browsers'] : array();
// Crude type detection if needed.
if (empty($item['type'])) {
if (is_array($item['data'])) {
$item['type'] = 'setting';
}
elseif (strpos($item['data'], 'http://') === 0 || strpos($item['data'], 'https://') === 0 || strpos($item['data'], '//') === 0) {
$item['type'] = 'external';
}
elseif (file_exists(trim($item['data']))) {
$item['type'] = 'file';
}
else {
$item['type'] = 'inline';
}
}
// Element properties that depend on item type.
switch ($item['type']) {
case 'setting':
$data = advagg_cleanup_settings_array(drupal_array_merge_deep_array(array_filter($item['data'], 'is_array')));
$json_data = advagg_json_encode($data);
$element['#value_prefix'] = $embed_prefix;
$element['#value'] = 'jQuery.extend(Drupal.settings, ' . $json_data . ");";
$element['#value_suffix'] = $embed_suffix;
break;
case 'inline':
// If a BOM is found, convert the string to UTF-8.
$encoding = advagg_get_encoding_from_bom($item['data']);
if (!empty($encoding)) {
$item['data'] = advagg_convert_to_utf8($item['data'], $encoding);
}
$element['#value_prefix'] = $embed_prefix;
$element['#value'] = $item['data'];
$element['#value_suffix'] = $embed_suffix;
break;
case 'file':
$query_string_separator = strpos($item['data'], '?') !== FALSE ? '&' : '?';
$cache_validator = REQUEST_TIME;
if (!empty($item['cache'])) {
$cache_validator = empty($item['version']) ? $default_query_string : $js_version_string . $item['version'];
}
$element['#attributes']['src'] = advagg_file_create_url($item['data']) . $query_string_separator . $cache_validator;
break;
case 'external':
// Convert to protocol relative path.
$file_uri = $item['data'];
if (variable_get('advagg_convert_absolute_to_protocol_relative_path', ADVAGG_CONVERT_ABSOLUTE_TO_PROTOCOL_RELATIVE_PATH)) {
$file_uri = advagg_convert_abs_to_protocol($item['data']);
}
$element['#attributes']['src'] = $file_uri;
break;
}
$elements[] = $element;
}
}
}
return $elements;
}