function advagg_pre_render_scripts

cis7 advagg.module advagg_pre_render_scripts($elements)
cle7 advagg.module advagg_pre_render_scripts($elements)
elmsmedia7 advagg.module advagg_pre_render_scripts($elements)
icor7 advagg.module advagg_pre_render_scripts($elements)
meedjum_blog7 advagg.module advagg_pre_render_scripts($elements)
mooc7 advagg.module advagg_pre_render_scripts($elements)

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.

Parameters

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.

See also

drupal_get_js()

1 string reference to 'advagg_pre_render_scripts'
advagg_element_info_alter in sites/all/modules/ulmus/advagg/advagg.module
Implements hook_element_info_alter().

File

sites/all/modules/ulmus/advagg/advagg.module, line 2031
Advanced CSS/JS aggregation module.

Code

function advagg_pre_render_scripts($elements) {
  // Group and aggregate the items.
  if (isset($elements['#group_callback'])) {
    $elements['#groups'] = $elements['#group_callback']($elements['#items']);
  }
  if (isset($elements['#aggregate_callback'])) {
    $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_tag',
    '#tag' => 'script',
    '#value' => '',
    '#attributes' => array(
      'type' => 'text/javascript',
    ),
  );

  // 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'] = file_create_url($group['data']);
      $element['#browsers'] = $group['browsers'];
      if (!empty($group['defer'])) {
        $element['#attributes']['defer'] = 'defer';
      }
      if (!empty($group['async'])) {
        $element['#attributes']['async'] = 'async';
      }
      if (!empty($group['onload'])) {
        $element['#attributes']['onload'] = $group['onload'];
      }
      $elements[] = $element;
    }
    // For non-file types, and non-aggregated files, add a script element per
    // item.
    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'])) {
          $element['#attributes']['onload'] = $item['onload'];
        }
        $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':
            // If in development mode and PHP >= 5.4.0 pretty print JSON.
            static $php540;
            if (!isset($php540)) {
              $php540 = version_compare(PHP_VERSION, '5.4.0', '>=');
            }
            $json_data = drupal_array_merge_deep_array($item['data']);
            if ($php540 && variable_get('advagg_cache_level', ADVAGG_CACHE_LEVEL) == -1) {
              $json_data = @json_encode($json_data, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_PRETTY_PRINT);
            }
            else {
              $json_data = @drupal_json_encode($json_data);
            }
            $element['#value_prefix'] = $embed_prefix;
            $element['#value'] = 'jQuery.extend(Drupal.settings, ' . $json_data . ");";
            $element['#value_suffix'] = $embed_suffix;
            break;

          case 'inline':
            $element['#value_prefix'] = $embed_prefix;
            $element['#value'] = $item['data'];
            $element['#value_suffix'] = $embed_suffix;
            break;

          case 'file':
            $query_string = empty($item['version']) ? $default_query_string : $js_version_string . $item['version'];
            $query_string_separator = (strpos($item['data'], '?') !== FALSE) ? '&' : '?';
            $element['#attributes']['src'] = file_create_url($item['data']) . $query_string_separator . ($item['cache'] ? $query_string : REQUEST_TIME);
            break;

          case 'external':
            $element['#attributes']['src'] = $item['data'];
            break;
        }

        $elements[] = $element;
      }
    }
  }

  return $elements;
}
Error | ELMSLN API

Error

×

Error message

  • Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/elmsln_community/api.elmsln.org/includes/common.inc:2791) in drupal_send_headers() (line 1499 of /var/www/html/elmsln_community/api.elmsln.org/includes/bootstrap.inc).
  • Error: Call to undefined function apc_delete() in DrupalAPCCache->clear() (line 289 of /var/www/html/elmsln_community/api.elmsln.org/sites/all/modules/apc/drupal_apc_cache.inc).
The website encountered an unexpected error. Please try again later.