You are here

function magic_pre_render_scripts in Magic 7.2

Same name and namespace in other branches
  1. 7 includes/scripts-experimental.inc \magic_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()

File

includes/scripts-experimental.inc, line 553
A file to contain functions for the magic module to abuse.

Code

function magic_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 (!empty($group['type']) && $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;
        if (!empty($item['defer'])) {
          $element['#attributes']['defer'] = 'defer';
        }
        if (!empty($item['async'])) {
          $element['#attributes']['async'] = 'async';
        }
        $element['#browsers'] = $item['browsers'];
        if (!empty($item['type'])) {

          // Element properties that depend on item type.
          switch ($item['type']) {
            case 'setting':
              $element['#value_prefix'] = $embed_prefix;
              $element['#value'] = 'jQuery.extend(Drupal.settings, ' . drupal_json_encode(drupal_array_merge_deep_array($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;
          }
        }
        $elements[] = $element;
      }
    }
  }
  return $elements;
}