Subscriber that wraps controllers, to handle early rendering.
When controllers call drupal_render() (RendererInterface::render()) outside of a render context, we call that "early rendering". Controllers should return only render arrays, but we cannot prevent controllers from doing early rendering. The problem with early rendering is that the bubbleable metadata (cacheability & attachments) are lost.
This can lead to broken pages (missing assets), stale pages (missing cache tags causing a page not to be invalidated) or even security problems (missing cache contexts causing a cached page not to be varied sufficiently).
This event subscriber wraps all controller executions in a closure that sets up a render context. Consequently, any early rendering will have their bubbleable metadata (assets & cacheability) stored on that render context.
If the render context is empty, then the controller either did not do any rendering at all, or used the RendererInterface::renderRoot() or ::renderPlain() methods. In that case, no bubbleable metadata is lost.
If the render context is not empty, then the controller did use drupal_render(), and bubbleable metadata was collected. This bubbleable metadata is then merged onto the render array.
In other words: this just exists to ease the transition to Drupal 8: it allows controllers that return render arrays (the majority) and \Drupal\Core\Ajax\AjaxResponse\AjaxResponse objects (a sizable minority that often involve a fair amount of rendering) to still do early rendering. But controllers that return any other kind of response are already expected to do the right thing, so if early rendering is detected in such a case, an exception is thrown.
@todo Remove in Drupal 9.0.0, by disallowing early rendering.
Hierarchy
- class \Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber implements EventSubscriberInterface
See also
\Drupal\Core\Render\RendererInterface
File
- core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php, line 55
Namespace
Drupal\Core\EventSubscriber
Members
Name | Modifiers | Type | Description |
---|---|---|---|
EarlyRenderingControllerWrapperSubscriber::$controllerResolver | protected | property | The controller resolver. |
EarlyRenderingControllerWrapperSubscriber::$renderer | protected | property | The renderer. |
EarlyRenderingControllerWrapperSubscriber::getSubscribedEvents | public static | function | Returns an array of event names this subscriber wants to listen to. Overrides EventSubscriberInterface::getSubscribedEvents |
EarlyRenderingControllerWrapperSubscriber::onController | public | function | Ensures bubbleable metadata from early rendering is not lost. |
EarlyRenderingControllerWrapperSubscriber::wrapControllerExecutionInRenderContext | protected | function | Wraps a controller execution in a render context. |
EarlyRenderingControllerWrapperSubscriber::__construct | public | function | Constructs a new EarlyRenderingControllerWrapperSubscriber instance. |
Please login to continue.