1 common.inc backdrop_pre_render_scripts($elements)

#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 backdrop_add_js() and altered by backdrop_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

backdrop_get_js()

File

core/includes/common.inc, line 4924
Common functions that many Backdrop modules will need to reference.

Code

function backdrop_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 backdrop_add_js())
  // get REQUEST_TIME as query-string instead, to enforce reload on every
  // page request.
  $default_query_string = !defined('MAINTENANCE_MODE') ? state_get('css_js_query_string', '0') : '';

  // Defaults for each SCRIPT element.
  $element_defaults = array(
    '#type' => 'head_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;
    }
    // For non-file types, and non-aggregated files, add a script element per
    // item.
    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';
        }
        $element['#browsers'] = $item['browsers'];

        // Element properties that depend on item type.
        switch ($item['type']) {
          case 'setting':
            $element['#value'] = 'window.Backdrop = {settings: ' . backdrop_json_encode(backdrop_array_merge_deep_array($item['data'])) . "};";
            break;

          case 'inline':
            $element['#value'] = $item['data'];
            break;

          case 'file':
            $query_string = empty($item['version']) ? $default_query_string : 'v=' . $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;
}