RenderArrayNonHtmlSubscriber.php in Drupal 10
File
core/lib/Drupal/Core/EventSubscriber/RenderArrayNonHtmlSubscriber.php
View source
<?php
namespace Drupal\Core\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
use Symfony\Component\HttpKernel\KernelEvents;
class RenderArrayNonHtmlSubscriber implements EventSubscriberInterface {
public function onRespond(ViewEvent $event) {
$request = $event
->getRequest();
$result = $event
->getControllerResult();
if (is_array($result) && $request
->getRequestFormat() !== 'html') {
throw new NotAcceptableHttpException('Not acceptable format: ' . $request
->getRequestFormat());
}
}
public static function getSubscribedEvents() : array {
$events[KernelEvents::VIEW][] = [
'onRespond',
-10,
];
return $events;
}
}