function Renderer::executeInRenderContext
Same name and namespace in other branches
- 11.x core/lib/Drupal/Core/Render/Renderer.php \Drupal\Core\Render\Renderer::executeInRenderContext()
- 10 core/lib/Drupal/Core/Render/Renderer.php \Drupal\Core\Render\Renderer::executeInRenderContext()
- 9 core/lib/Drupal/Core/Render/Renderer.php \Drupal\Core\Render\Renderer::executeInRenderContext()
Executes a callable within a render context.
Only for very advanced use cases. Prefer using ::renderRoot() and ::renderPlain() instead.
All rendering must happen within a render context. Within a render context, all bubbleable metadata is bubbled and hence tracked. Outside of a render context, it would be lost. This could lead to missing assets, incorrect cache variations (and thus security issues), insufficient cache invalidations, and so on.
Any and all rendering must therefore happen within a render context, and it is this method that provides that.
Parameters
\Drupal\Core\Render\RenderContext $context: The render context to execute the callable within.
callable $callable: The callable to execute.
Return value
mixed The callable's return value.
Overrides RendererInterface::executeInRenderContext
2 calls to Renderer::executeInRenderContext()
- Renderer::renderPlain in core/
lib/ Drupal/ Core/ Render/ Renderer.php - Renderer::renderRoot in core/
lib/ Drupal/ Core/ Render/ Renderer.php
File
-
core/
lib/ Drupal/ Core/ Render/ Renderer.php, line 567
Class
- Renderer
- Turns a render array into a HTML string.
Namespace
Drupal\Core\RenderCode
public function executeInRenderContext(RenderContext $context, callable $callable) {
// Store the current render context.
$previous_context = $this->getCurrentRenderContext();
// Set the provided context and call the callable, it will use that context.
$this->setCurrentRenderContext($context);
$result = $callable();
// @todo Convert to an assertion in https://www.drupal.org/node/2408013
if ($context->count() > 1) {
throw new \LogicException('Bubbling failed.');
}
// Restore the original render context.
$this->setCurrentRenderContext($previous_context);
return $result;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.