function PlaceholderGenerator::createPlaceholder

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

Turns the given element into a placeholder.

Placeholdering allows us to avoid "poor cacheability contamination": this maps the current render array to one that only has #markup and #attached, and #attached contains a placeholder with this element's prior cacheability metadata. In other words: this placeholder is perfectly cacheable, the placeholder replacement logic effectively cordons off poor cacheability.

Parameters

array $element: The render array to create a placeholder for.

Return value

array Render array with placeholder markup and the attached placeholder replacement metadata.

Overrides PlaceholderGeneratorInterface::createPlaceholder

File

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

Class

PlaceholderGenerator
Turns a render array into a placeholder.

Namespace

Drupal\Core\Render

Code

public function createPlaceholder(array $element) {
  $placeholder_render_array = array_intersect_key($element, \array_flip([
    // Placeholders are replaced with markup by executing the associated
    // #lazy_builder callback, which generates a render array, and which the
    // Renderer will render and replace the placeholder with.
'#lazy_builder',
    // The cacheability metadata for the placeholder. The rendered result of
    // the placeholder may itself be cached, if [#cache][keys] are specified.
'#cache',
    // The placeholder strategy denylist for this placeholder which can be
    // used to skip placeholder strategies for specific render arrays.
'#placeholder_strategy_denylist',
  ]));
  if (isset($element['#lazy_builder_preview'])) {
    $placeholder_render_array['#preview'] = $element['#lazy_builder_preview'];
  }
  // Be sure cache contexts and tags are sorted before serializing them and
  // making hash. Issue #3225328 removes sort from contexts and tags arrays
  // for performances reasons.
  if (isset($placeholder_render_array['#cache']['contexts'])) {
    sort($placeholder_render_array['#cache']['contexts']);
  }
  if (isset($placeholder_render_array['#cache']['tags'])) {
    sort($placeholder_render_array['#cache']['tags']);
  }
  // Generate placeholder markup. Note that the only requirement is that this
  // is unique markup that isn't easily guessable. The #lazy_builder callback
  // and its arguments are put in the placeholder markup solely to simplify
  // debugging.
  $placeholder_markup = static::createPlaceholderTag('drupal-render-placeholder', [
    'callback' => $placeholder_render_array['#lazy_builder'][0],
    'arguments' => UrlHelper::buildQuery($placeholder_render_array['#lazy_builder'][1]),
    'token' => Crypt::hashBase64(serialize($placeholder_render_array)),
  ]);
  // Build the placeholder element to return.
  $placeholder_element = [];
  $placeholder_element['#markup'] = Markup::create($placeholder_markup);
  $placeholder_element['#attached']['placeholders'][$placeholder_markup] = $placeholder_render_array;
  return $placeholder_element;
}

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