class HtmxContentViewSubscriber
View subscriber rendering main content from `_htmx_route` option routes.
Uses HtmxRenderer to create an HTML response for any route with the `_htmx_route` option set to TRUE. This subscriber runs before the MainContentViewSubscriber.
Hierarchy
- class \Drupal\Core\EventSubscriber\HtmxContentViewSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of HtmxContentViewSubscriber
See also
\Drupal\Core\Render\MainContent\MainContentRendererInterface
\Drupal\Core\Render\MainContent\HtmxRenderer
1 string reference to 'HtmxContentViewSubscriber'
- core.services.yml in core/
core.services.yml - core/core.services.yml
1 service uses HtmxContentViewSubscriber
File
-
core/
lib/ Drupal/ Core/ EventSubscriber/ HtmxContentViewSubscriber.php, line 24
Namespace
Drupal\Core\EventSubscriberView source
class HtmxContentViewSubscriber implements EventSubscriberInterface {
public function __construct(#[AutowireServiceClosure('main_content_renderer.htmx')] protected \Closure $htmxRenderer, protected RouteMatchInterface $routeMatch) {
}
/**
* Sets a minimal response render array on an `_htmx_route` route.
*
* @param \Symfony\Component\HttpKernel\Event\ViewEvent $event
* The event to process.
*/
public function renderHtmxResponse(ViewEvent $event) : void {
$htmxRoute = $this->routeMatch
->getRouteObject()
->getOption('_htmx_route') ?? FALSE;
$request = $event->getRequest();
$result = $event->getControllerResult();
if ($htmxRoute && is_array($result) && $request->getRequestFormat() === 'html') {
$response = $this->getHtmxRenderer()
->renderResponse($result, $request, $this->routeMatch);
$event->setResponse($response);
}
}
/**
* Gets the HtmxRenderer service.
*
* @return \Drupal\Core\Render\MainContent\HtmxRenderer
* The service instantiated by the autowire closure.
*/
protected function getHtmxRenderer() : MainContentRendererInterface {
return ($this->htmxRenderer)();
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() : array {
// MainContentViewSubscriber has a default priority of 0.
// Set a higher priority to ensure that this subscriber runs before it.
$events[KernelEvents::VIEW][] = [
'renderHtmxResponse',
100,
];
return $events;
}
}
Members
| Title Sort descending | Modifiers | Object type | Summary |
|---|---|---|---|
| HtmxContentViewSubscriber::getHtmxRenderer | protected | function | Gets the HtmxRenderer service. |
| HtmxContentViewSubscriber::getSubscribedEvents | public static | function | |
| HtmxContentViewSubscriber::renderHtmxResponse | public | function | Sets a minimal response render array on an `_htmx_route` route. |
| HtmxContentViewSubscriber::__construct | public | function |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.