You are here

class HtmlResponsePlaceholderStrategySubscriber in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/EventSubscriber/HtmlResponsePlaceholderStrategySubscriber.php \Drupal\Core\EventSubscriber\HtmlResponsePlaceholderStrategySubscriber

HTML response subscriber to allow for different placeholder strategies.

This allows core and contrib to coordinate how to render placeholders; e.g. an EsiRenderStrategy could replace the placeholders with ESI tags, while e.g. a BigPipeRenderStrategy could store the placeholders in a BigPipe service and render them after the main content has been sent to the client.

Hierarchy

Expanded class hierarchy of HtmlResponsePlaceholderStrategySubscriber

1 string reference to 'HtmlResponsePlaceholderStrategySubscriber'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses HtmlResponsePlaceholderStrategySubscriber
html_response.placeholder_strategy_subscriber in core/core.services.yml
Drupal\Core\EventSubscriber\HtmlResponsePlaceholderStrategySubscriber

File

core/lib/Drupal/Core/EventSubscriber/HtmlResponsePlaceholderStrategySubscriber.php, line 20

Namespace

Drupal\Core\EventSubscriber
View source
class HtmlResponsePlaceholderStrategySubscriber implements EventSubscriberInterface {

  /**
   * The placeholder strategy to use.
   *
   * @var \Drupal\Core\Render\Placeholder\PlaceholderStrategyInterface
   */
  protected $placeholderStrategy;

  /**
   * Constructs a HtmlResponsePlaceholderStrategySubscriber object.
   *
   * @param \Drupal\Core\Render\Placeholder\PlaceholderStrategyInterface $placeholder_strategy
   *   The placeholder strategy to use.
   */
  public function __construct(PlaceholderStrategyInterface $placeholder_strategy) {
    $this->placeholderStrategy = $placeholder_strategy;
  }

  /**
   * Processes placeholders for HTML responses.
   *
   * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
   *   The event to process.
   */
  public function onRespond(FilterResponseEvent $event) {
    $response = $event
      ->getResponse();
    if (!$response instanceof HtmlResponse) {
      return;
    }
    $attachments = $response
      ->getAttachments();
    if (empty($attachments['placeholders'])) {
      return;
    }
    $attachments['placeholders'] = $this->placeholderStrategy
      ->processPlaceholders($attachments['placeholders']);
    $response
      ->setAttachments($attachments);
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {

    // Run shortly before HtmlResponseSubscriber.
    $events[KernelEvents::RESPONSE][] = [
      'onRespond',
      5,
    ];
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
HtmlResponsePlaceholderStrategySubscriber::$placeholderStrategy protected property The placeholder strategy to use.
HtmlResponsePlaceholderStrategySubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
HtmlResponsePlaceholderStrategySubscriber::onRespond public function Processes placeholders for HTML responses.
HtmlResponsePlaceholderStrategySubscriber::__construct public function Constructs a HtmlResponsePlaceholderStrategySubscriber object.