You are here

public function RenderStack::processPostRenderCache in Render cache 7.2

File

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

Class

RenderStack
Defines the RenderStack service.

Namespace

Drupal\render_cache\Cache

Code

public function processPostRenderCache(&$render, $cache_info) {
  $strategy = $cache_info['render_cache_cache_strategy'];

  // Only when we have rendered to #markup we can post process.
  // @todo Use a #post_render function with a closure instead.
  if ($strategy != RenderCache::RENDER_CACHE_STRATEGY_DIRECT_RENDER) {

    // @todo Log an error.
    return;
  }
  $storage = $this
    ->collectAndRemoveAssets($render);
  while (!empty($storage['#post_render_cache'])) {

    // Save the value and unset from the storage.
    $post_render_cache = $storage['#post_render_cache'];
    unset($storage['#post_render_cache']);
    $this
      ->increaseRecursion();

    // Add the storage back first, so order is preserved.
    $this
      ->addRecursionStorage($storage);

    // Add todo use a helper function.
    foreach (array_keys($post_render_cache) as $callback) {
      foreach ($post_render_cache[$callback] as $context) {
        $render = call_user_func_array($callback, array(
          $render,
          $context,
        ));
      }
    }

    // Get and remove any new storage from the render array.
    $storage = $this
      ->collectAndRemoveAssets($render);

    // ... and push to the stack.
    $this
      ->addRecursionStorage($storage);

    // Now everything is in here.
    $storage = $this
      ->decreaseRecursion();

    // If there is attached on the stack, then merge it to the #attached data we already have.
    if (!empty($storage['#attached'])) {
      $render += array(
        '#attached' => array(),
      );

      // @codeCoverageIgnore
      $render['#attached'] = NestedArray::mergeDeep($render['#attached'], $storage['#attached']);
      unset($storage['#attached']);
    }
  }

  // Put the storage back, so it can be pushed to the stack.
  $render = NestedArray::mergeDeep($render, $storage);
}