3 namespace Drupal\Core\EventSubscriber;
5 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
6 use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
7 use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
8 use Symfony\Component\HttpKernel\KernelEvents;
11 * View subscriber rendering a 406 if we could not route or render a request.
13 * @todo fix or replace this in https://www.drupal.org/node/2364011
15 class AcceptNegotiation406 implements EventSubscriberInterface {
18 * Throws an HTTP 406 error if we get this far, which we normally shouldn't.
20 * @param \Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent $event
21 * The event to process.
23 public function onViewDetect406(GetResponseForControllerResultEvent $event) {
24 $request = $event->getRequest();
25 $result = $event->getControllerResult();
27 // If this is a render array then we assume that the router went with the
28 // generic controller and not one with a format. If the format requested is
29 // not HTML though we can also assume that the requested format is invalid
30 // so we provide a 406 response.
31 if (is_array($result) && $request->getRequestFormat() !== 'html') {
32 throw new NotAcceptableHttpException('Not acceptable format: ' . $request->getRequestFormat());
39 public static function getSubscribedEvents() {
40 $events[KernelEvents::VIEW][] = ['onViewDetect406', -10];