You are here

public function ChainedPlaceholderStrategy::processPlaceholders in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Render/Placeholder/ChainedPlaceholderStrategy.php \Drupal\Core\Render\Placeholder\ChainedPlaceholderStrategy::processPlaceholders()

Processes placeholders to render them with different strategies.

Parameters

array $placeholders: The placeholders to process, with the keys being the markup for the placeholders and the values the corresponding render array describing the data to be rendered.

Return value

array The resulting placeholders, with a subset of the keys of $placeholders (and those being the markup for the placeholders) but with the corresponding render array being potentially modified to render e.g. an ESI or BigPipe placeholder.

Overrides PlaceholderStrategyInterface::processPlaceholders

File

core/lib/Drupal/Core/Render/Placeholder/ChainedPlaceholderStrategy.php, line 32

Class

ChainedPlaceholderStrategy
Renders placeholders using a chain of placeholder strategies.

Namespace

Drupal\Core\Render\Placeholder

Code

public function processPlaceholders(array $placeholders) {
  if (empty($placeholders)) {
    return [];
  }

  // Assert that there is at least one strategy.
  assert(!empty($this->placeholderStrategies), 'At least one placeholder strategy must be present; by default the fallback strategy \\Drupal\\Core\\Render\\Placeholder\\SingleFlushStrategy is always present.');
  $new_placeholders = [];

  // Give each placeholder strategy a chance to replace all not-yet replaced
  // placeholders. The order of placeholder strategies is well defined
  // and this uses a variation of the "chain of responsibility" design pattern.
  foreach ($this->placeholderStrategies as $strategy) {
    $processed_placeholders = $strategy
      ->processPlaceholders($placeholders);
    assert(array_intersect_key($processed_placeholders, $placeholders) === $processed_placeholders, 'Processed placeholders must be a subset of all placeholders.');
    $placeholders = array_diff_key($placeholders, $processed_placeholders);
    $new_placeholders += $processed_placeholders;
    if (empty($placeholders)) {
      break;
    }
  }
  return $new_placeholders;
}