getCacheableRenderArray example

$this->renderer->executeInRenderContext($render_contextfunction D) use (&$html) {
      // RendererInterface::render() renders the $html render array and updates       // it in place. We don't care about the return value (which is just       // $html['#markup']), but about the resulting render array.       // @todo Simplify this when https://www.drupal.org/node/2495001 lands.       $this->renderer->render($html);
    });
    // RendererInterface::render() always causes bubbleable metadata to be     // stored in the render context, no need to check it conditionally.     $bubbleable_metadata = $render_context->pop();
    $bubbleable_metadata->applyTo($html);
    $content = $this->renderCache->getCacheableRenderArray($html);

    // Also associate the required cache contexts.     // (Because we use ::render() above and not ::renderRoot(), we manually must     // ensure the HTML response varies by the required cache contexts.)     $content['#cache']['contexts'] = Cache::mergeContexts($content['#cache']['contexts']$this->rendererConfig['required_cache_contexts']);

    // Also associate the "rendered" cache tag. This allows us to invalidate the     // entire render cache, regardless of the cache bin.     $content['#cache']['tags'][] = 'rendered';

    $response = new HtmlResponse($content, 200, [
      

  public function set(array &$elements, array $pre_bubbling_elements) {
    // Form submissions rely on the form being built during the POST request,     // and render caching of forms prevents this from happening.     // @todo remove the isMethodCacheable() check when     // https://www.drupal.org/node/2367555 lands.     if (!$this->requestStack->getCurrentRequest()->isMethodCacheable() || !$cid = $this->createCacheID($elements)) {
      return FALSE;
    }

    $data = $this->getCacheableRenderArray($elements);

    $bin = $elements['#cache']['bin'] ?? 'render';
    $cache = $this->cacheFactory->get($bin);

    // Calculate the pre-bubbling CID.     $pre_bubbling_cid = $this->createCacheID($pre_bubbling_elements);

    // Two-tier caching: detect different CID post-bubbling, create redirect,     // update redirect if different set of cache contexts.     // @see \Drupal\Core\Render\RendererInterface::render()     // @see ::get()
$result = parent::set($elements$pre_bubbling_elements);

    // @todo remove this check when https://www.drupal.org/node/2367555 lands.     if (!$this->requestStack->getCurrentRequest()->isMethodCacheable()) {
      return FALSE;
    }

    if ($this->placeholderGenerator->canCreatePlaceholder($pre_bubbling_elements) && $this->placeholderGenerator->shouldAutomaticallyPlaceholder($elements)) {
      // Overwrite $elements with a placeholder. The Renderer (which called this       // method) will update the context with the bubbleable metadata of the       // overwritten $elements.       $elements = $this->createPlaceholderAndRemember($this->getCacheableRenderArray($elements)$pre_bubbling_elements);
    }

    return $result;
  }

  /** * Create a placeholder for a renderable array and remember in a static cache. * * @param array $rendered_elements * A fully rendered renderable array. * @param array $pre_bubbling_elements * A renderable array corresponding to the state (in particular, the * cacheability metadata) of $rendered_elements prior to the beginning of * its rendering process, and therefore before any bubbling of child * information has taken place. Only the #cache property is used by this * function, so the caller may omit all other properties and children from * this array. * * @return array * Renderable array with placeholder markup and the attached placeholder * replacement metadata. */
Home | Imprint | This part of the site doesn't use cookies.