class CachedStrategy

Looks up placeholders in the render cache and returns those we could find.

Hierarchy

Expanded class hierarchy of CachedStrategy

1 string reference to 'CachedStrategy'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses CachedStrategy
placeholder_strategy.cached in core/core.services.yml
Drupal\Core\Render\Placeholder\CachedStrategy

File

core/lib/Drupal/Core/Render/Placeholder/CachedStrategy.php, line 12

Namespace

Drupal\Core\Render\Placeholder
View source
class CachedStrategy implements PlaceholderStrategyInterface {
  public function __construct(protected readonly PlaceholderStrategyInterface $placeholderStrategy, protected readonly RenderCacheInterface $renderCache) {
  }
  
  /**
   * {@inheritdoc}
   */
  public function processPlaceholders(array $placeholders) {
    $return = $this->renderCache
      ->getMultiple($placeholders);
    if ($return) {
      $return = $this->processNestedPlaceholders($return);
    }
    return $return;
  }
  
  /**
   * Fetch any nested placeholders from cache.
   *
   * Placeholders returned from cache may have placeholders in #attached, which
   * can themselves be fetched from the cache. By recursively processing the
   * placeholders here, we're able to use multiple cache get to fetch the cache
   * items at each level of recursion.
   */
  private function processNestedPlaceholders(array $placeholders) : array {
    $sets = [];
    foreach ($placeholders as $key => $placeholder) {
      if (!empty($placeholder['#attached']['placeholders'])) {
        $sets[] = $placeholder['#attached']['placeholders'];
      }
    }
    if ($sets) {
      $cached = $this->renderCache
        ->getMultiple(...array_merge($sets));
      if ($cached) {
        $cached = $this->processNestedPlaceholders($cached);
        foreach ($placeholders as $key => $placeholder) {
          if (!empty($placeholder['#attached']['placeholders'])) {
            $placeholders[$key]['#attached']['placeholders'] = array_replace($placeholder['#attached']['placeholders'], $cached);
          }
        }
      }
    }
    return $placeholders;
  }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
CachedStrategy::processNestedPlaceholders private function Fetch any nested placeholders from cache.
CachedStrategy::processPlaceholders public function Processes placeholders to render them with different strategies. Overrides PlaceholderStrategyInterface::processPlaceholders
CachedStrategy::__construct public function

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