function drupal_pre_render_scripts

#pre_render callback to add the elements needed for JavaScript tags 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

$element: 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

A render array that will render to a string of JavaScript tags.

See also

drupal_get_js()

1 string reference to 'drupal_pre_render_scripts'
system_element_info in drupal/core/modules/system/system.module
Implements hook_element_info().

File

drupal/core/includes/common.inc, line 3189
Common functions that many Drupal modules will need to reference.

Code

function drupal_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' => '',
  );

  // 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 ($group['type'] == 'file' && isset($group['data'])) {
      $element = $element_defaults;
      $element['#attributes']['src'] = file_create_url($group['data']);
      $element['#browsers'] = $group['browsers'];
      $elements[] = $element;
    }
    else {
      foreach ($group['items'] as $item) {

        // Element properties that do not depend on item type.
        $element = $element_defaults;
        $element['#browsers'] = $item['browsers'];

        // Element properties that depend on item type.
        switch ($item['type']) {
          case 'setting':
            $element['#value_prefix'] = $embed_prefix;
            $element['#value'] = 'var drupalSettings = ' . drupal_json_encode(drupal_merge_js_settings($item['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;
        }

        // Attributes may only be set if this script is output independently.
        if (!empty($element['#attributes']['src']) && !empty($item['attributes'])) {
          $element['#attributes'] += $item['attributes'];
        }
        $elements[] = $element;
      }
    }
  }
  return $elements;
}