3 namespace Drupal\Core\EventSubscriber;
5 use Drupal\Core\Render\HtmlResponse;
6 use Drupal\Core\Render\Placeholder\PlaceholderStrategyInterface;
7 use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
8 use Symfony\Component\HttpKernel\KernelEvents;
9 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
12 * HTML response subscriber to allow for different placeholder strategies.
14 * This allows core and contrib to coordinate how to render placeholders;
15 * e.g. an EsiRenderStrategy could replace the placeholders with ESI tags,
16 * while e.g. a BigPipeRenderStrategy could store the placeholders in a
17 * BigPipe service and render them after the main content has been sent to
20 class HtmlResponsePlaceholderStrategySubscriber implements EventSubscriberInterface {
23 * The placeholder strategy to use.
25 * @var \Drupal\Core\Render\Placeholder\PlaceholderStrategyInterface
27 protected $placeholderStrategy;
30 * Constructs a HtmlResponsePlaceholderStrategySubscriber object.
32 * @param \Drupal\Core\Render\Placeholder\PlaceholderStrategyInterface $placeholder_strategy
33 * The placeholder strategy to use.
35 public function __construct(PlaceholderStrategyInterface $placeholder_strategy) {
36 $this->placeholderStrategy = $placeholder_strategy;
40 * Processes placeholders for HTML responses.
42 * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
43 * The event to process.
45 public function onRespond(FilterResponseEvent $event) {
46 $response = $event->getResponse();
47 if (!$response instanceof HtmlResponse) {
51 $attachments = $response->getAttachments();
52 if (empty($attachments['placeholders'])) {
56 $attachments['placeholders'] = $this->placeholderStrategy->processPlaceholders($attachments['placeholders']);
58 $response->setAttachments($attachments);
64 public static function getSubscribedEvents() {
65 // Run shortly before HtmlResponseSubscriber.
66 $events[KernelEvents::RESPONSE][] = ['onRespond', 5];