BigPipeStrategy::doProcessPlaceholders

protected BigPipeStrategy::doProcessPlaceholders(array $placeholders)

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

Parameters

array $placeholders: The placeholders to process.

Return value

array The BigPipe placeholders.

File

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

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 ($placeholder[0] !== '<' || $placeholder !== Html::normalize($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;
}
doc_Drupal
2016-10-29 08:46:20
Comments
Leave a Comment

Please login to continue.