function PlaceholderGenerator::shouldAutomaticallyPlaceholder

Same name 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()

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.