function PlaceholderGenerator::shouldAutomaticallyPlaceholder

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Render/PlaceholderGenerator.php \Drupal\Core\Render\PlaceholderGenerator::shouldAutomaticallyPlaceholder()
  2. 8.9.x core/lib/Drupal/Core/Render/PlaceholderGenerator.php \Drupal\Core\Render\PlaceholderGenerator::shouldAutomaticallyPlaceholder()
  3. 11.x core/lib/Drupal/Core/Render/PlaceholderGenerator.php \Drupal\Core\Render\PlaceholderGenerator::shouldAutomaticallyPlaceholder()

Whether the given render array should be automatically placeholdered.

The render array should be placeholdered if its cacheability either has a cache context with too high cardinality, a cache tag with a too high invalidation rate, or a max-age that is too low. Either of these would make caching ineffective, and thus we choose to placeholder instead.

Parameters

array $element: The render array whose cacheability to analyze.

Return value

bool Whether the given render array's cacheability meets the placeholdering conditions.

Overrides PlaceholderGeneratorInterface::shouldAutomaticallyPlaceholder

File

core/lib/Drupal/Core/Render/PlaceholderGenerator.php, line 57

Class

PlaceholderGenerator
Turns a render array into a placeholder.

Namespace

Drupal\Core\Render

Code

public function shouldAutomaticallyPlaceholder(array $element) {
  // Auto-placeholder if the max-age, cache context or cache tag is specified
  // in the auto-placeholder conditions in the 'renderer.config' container
  // parameter.
  $conditions = $this->rendererConfig['auto_placeholder_conditions'];
  $cacheability = CacheableMetadata::createFromRenderArray($element);
  if ($cacheability->getCacheMaxAge() !== Cache::PERMANENT && $cacheability->getCacheMaxAge() <= $conditions['max-age']) {
    return TRUE;
  }
  // Optimize the contexts and let them affect the cache tags to mimic what
  // happens to the cacheability in the variation cache (RenderCache backend).
  $cacheability->addCacheableDependency($this->cacheContextsManager
    ->convertTokensToKeys($cacheability->getCacheContexts()));
  $cacheability->setCacheContexts($this->cacheContextsManager
    ->optimizeTokens($cacheability->getCacheContexts()));
  if (array_intersect($cacheability->getCacheContexts(), $conditions['contexts'])) {
    return TRUE;
  }
  if (array_intersect($cacheability->getCacheTags(), $conditions['tags'])) {
    return TRUE;
  }
  return FALSE;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.