You are here

public function RenderStack::collectAndRemoveAssets in Render cache 7.2

3 calls to RenderStack::collectAndRemoveAssets()
RenderStack::addRecursionStorage in src/Cache/RenderStack.php
Adds assets to the current stack frame and removes them from the render array.
RenderStack::getRecursionStorage in src/Cache/RenderStack.php
Returns the current recursion storage.
RenderStack::processPostRenderCache in src/Cache/RenderStack.php

File

src/Cache/RenderStack.php, line 237
Contains \Drupal\render_cache\Cache\RenderStack

Class

RenderStack
Defines the RenderStack service.

Namespace

Drupal\render_cache\Cache

Code

public function collectAndRemoveAssets(&$element, $recursive = FALSE) {
  $assets = $this
    ->collectAndRemoveD8Properties($element);
  $assets['#cache']['tags'] = isset($assets['#cache']['tags']) ? $assets['#cache']['tags'] : array();
  $assets['#cache']['max-age'] = isset($assets['#cache']['max-age']) ? $assets['#cache']['max-age'] : array();
  $assets['#cache']['downstream-ttl'] = isset($assets['#cache']['downstream-ttl']) ? $assets['#cache']['downstream-ttl'] : array();
  $assets['#post_render_cache'] = isset($assets['#post_render_cache']) ? $assets['#post_render_cache'] : array();
  if (!is_array($assets['#cache']['max-age'])) {
    $assets['#cache']['max-age'] = array(
      $assets['#cache']['max-age'],
    );
  }
  if (!is_array($assets['#cache']['downstream-ttl'])) {
    $assets['#cache']['downstream-ttl'] = array(
      $assets['#cache']['downstream-ttl'],
    );
  }

  // Get the children of the element, sorted by weight.
  $children = Element::children($element, TRUE);
  foreach ($children as $key) {
    $new_assets = $this
      ->collectAndRemoveAssets($element[$key], TRUE);
    $assets['#cache']['tags'] = Cache::mergeTags($assets['#cache']['tags'], $new_assets['#cache']['tags']);
    $assets['#cache']['max-age'] = NestedArray::mergeDeep($assets['#cache']['max-age'], $new_assets['#cache']['max-age']);
    $assets['#cache']['downstream-ttl'] = NestedArray::mergeDeep($assets['#cache']['downstream-ttl'], $new_assets['#cache']['downstream-ttl']);
    $assets['#post_render_cache'] = NestedArray::mergeDeep($assets['#post_render_cache'], $new_assets['#post_render_cache']);
  }
  if (!$recursive) {

    // Ensure that there are no empty properties.
    if (empty($assets['#cache']['tags'])) {
      unset($assets['#cache']['tags']);
    }
    if (empty($assets['#cache']['max-age'])) {
      unset($assets['#cache']['max-age']);
    }
    if (empty($assets['#cache']['downstream-ttl'])) {
      unset($assets['#cache']['downstream-ttl']);
    }

    // Ensure the cache property is empty.
    if (empty($assets['#cache'])) {
      unset($assets['#cache']);
    }
    if (empty($assets['#post_render_cache'])) {
      unset($assets['#post_render_cache']);
    }
  }
  return $assets;
}