function drupal_render_collect_attached

Collects #attached for an element and its children into a single array.

When caching elements, it is necessary to collect all libraries, JavaScript and CSS into a single array, from both the element itself and all child elements. This allows drupal_render() to add these back to the page when the element is returned from cache.

Parameters

$elements: The element to collect #attached from.

$return: Whether to return the attached elements and reset the internal static.

Return value

The #attached array for this element and its descendants.

2 calls to drupal_render_collect_attached()
CachePluginBase::gatherHeaders in drupal/core/modules/views/lib/Drupal/views/Plugin/views/cache/CachePluginBase.php
Gather the JS/CSS from the render array, the html head from the band data.
drupal_render_cache_set in drupal/core/includes/common.inc
Caches the rendered output of a renderable element.

File

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

Code

function drupal_render_collect_attached($elements, $return = FALSE) {
  $attached =& drupal_static(__FUNCTION__, array());

  // Collect all #attached for this element.
  if (isset($elements['#attached'])) {
    foreach ($elements['#attached'] as $key => $value) {
      if (!isset($attached[$key])) {
        $attached[$key] = array();
      }
      $attached[$key] = array_merge($attached[$key], $value);
    }
  }
  if ($children = element_children($elements)) {
    foreach ($children as $child) {
      drupal_render_collect_attached($elements[$child]);
    }
  }

  // If this was the first call to the function, return all attached elements
  // and reset the static cache.
  if ($return) {
    $return = $attached;
    $attached = array();
    return $return;
  }
}