You are here

protected function BigPipeStrategy::doProcessPlaceholders in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/big_pipe/src/Render/Placeholder/BigPipeStrategy.php \Drupal\big_pipe\Render\Placeholder\BigPipeStrategy::doProcessPlaceholders()

Transforms placeholders to BigPipe placeholders, either no-JS or JS.

Parameters

array $placeholders: The placeholders to process.

Return value

array The BigPipe placeholders.

1 call to BigPipeStrategy::doProcessPlaceholders()
BigPipeStrategy::processPlaceholders in core/modules/big_pipe/src/Render/Placeholder/BigPipeStrategy.php
Processes placeholders to render them with different strategies.

File

core/modules/big_pipe/src/Render/Placeholder/BigPipeStrategy.php, line 137

Class

BigPipeStrategy
Defines the BigPipe placeholder strategy, to send HTML in chunks.

Namespace

Drupal\big_pipe\Render\Placeholder

Code

protected function doProcessPlaceholders(array $placeholders) {
  $overridden_placeholders = [];
  foreach ($placeholders as $placeholder => $placeholder_elements) {

    // BigPipe uses JavaScript and the DOM to find the placeholder to replace.
    // This means finding the placeholder to replace must be efficient. Most
    // placeholders are HTML, which we can find efficiently thanks to the
    // querySelector API. But some placeholders are HTML attribute values or
    // parts thereof, and potentially even plain text in DOM text nodes. For
    // BigPipe's JavaScript to find those placeholders, it would need to
    // iterate over all DOM text nodes. This is highly inefficient. Therefore,
    // the BigPipe placeholder strategy only converts HTML placeholders into
    // BigPipe placeholders. The other placeholders need to be replaced on the
    // server, not via BigPipe.
    // @see \Drupal\Core\Access\RouteProcessorCsrf::renderPlaceholderCsrfToken()
    // @see \Drupal\Core\Form\FormBuilder::renderFormTokenPlaceholder()
    // @see \Drupal\Core\Form\FormBuilder::renderPlaceholderFormAction()
    if (static::placeholderIsAttributeSafe($placeholder)) {
      $overridden_placeholders[$placeholder] = static::createBigPipeNoJsPlaceholder($placeholder, $placeholder_elements, TRUE);
    }
    else {

      // If the current request/session doesn't have JavaScript, fall back to
      // no-JS BigPipe.
      if ($this->requestStack
        ->getCurrentRequest()->cookies
        ->has(static::NOJS_COOKIE)) {
        $overridden_placeholders[$placeholder] = static::createBigPipeNoJsPlaceholder($placeholder, $placeholder_elements, FALSE);
      }
      else {
        $overridden_placeholders[$placeholder] = static::createBigPipeJsPlaceholder($placeholder, $placeholder_elements);
      }
      $overridden_placeholders[$placeholder]['#cache']['contexts'][] = 'cookies:' . static::NOJS_COOKIE;
    }
  }
  return $overridden_placeholders;
}