You are here

public static function RenderCachePlaceholder::postRenderCacheMultiCallback in Render cache 7.2

Generic #post_render_cache callback for getPlaceholder() with multi=TRUE.

This is useful to group several related elements together.

Parameters

array $element: The renderable array that contains the to be replaced placeholders.

array $contexts: An array keyed by function with the contexts as values.

Return value

array A renderable array with the placeholders replaced.

Overrides RenderCachePlaceholderInterface::postRenderCacheMultiCallback

File

src/Cache/RenderCachePlaceholder.php, line 51
Contains \Drupal\render_cache\Cache\RenderCachePlaceholder

Class

RenderCachePlaceholder
Provides placeholder utility functions.

Namespace

Drupal\render_cache\Cache

Code

public static function postRenderCacheMultiCallback(array $element, array $contexts) {

  // Check this is really a multi placeholder.
  if (isset($contexts['function']) || !isset($contexts[0]['function'])) {
    return $element;
  }
  $function = $contexts[0]['function'];
  $args = array();
  foreach ($contexts as $context) {
    $placeholder = static::generatePlaceholder($context['function'], $context);

    // Check if the placeholder is present at all.
    if (strpos($element['#markup'], $placeholder) === FALSE) {
      continue;
    }
    $args[$placeholder] = static::loadPlaceholderFunctionArgs($context);
  }

  // This expects an array keyed by placeholder with the build as value.
  $placeholders = call_user_func($function, $args);
  foreach ($placeholders as $placeholder => $new_element) {
    $markup = static::drupalRender($new_element);
    $element['#markup'] = str_replace($placeholder, $markup, $element['#markup']);
  }
  return $element;
}