Skip to content
Snippets Groups Projects
Commit fd02c846 authored by Larry Garfield's avatar Larry Garfield
Browse files

Move the HTML request type handling to a subscriber class so that it's nicely encapsulated.

parent 501f6d5a
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
......@@ -14,6 +14,8 @@
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Drupal\Core\EventSubscriber\HtmlSubscriber;
use Exception;
/**
......@@ -32,21 +34,8 @@ function execute(Request $request) {
$dispatcher = new EventDispatcher();
// Quick and dirty attempt at wrapping our rendering logic as is.
$dispatcher->addListener(KernelEvents::VIEW, function(Event $event) {
$page_callback_result = $event->getControllerResult();
$event->setResponse(new Response(drupal_render_page($page_callback_result)));
});
$dispatcher->addListener(KernelEvents::EXCEPTION, function(Event $event) use ($request) {
debug($request->getAcceptableContentTypes());
if (in_array('text/html', $request->getAcceptableContentTypes())) {
if ($event->getException() instanceof ResourceNotFoundException) {
$event->setResponse(new Response('Not Found', 404));
}
}
});
// @todo Make this extensible rather than just hard coding some.
$dispatcher->addSubscriber(new HtmlSubscriber());
// Resolve a routing context(path, etc) using the routes object to a
// Set a routing context to translate.
......@@ -65,7 +54,7 @@ function execute(Request $request) {
$response = $kernel->handle($request);
}
catch (Exception $e) {
$error_event = new GetResponseForExceptionEvent($this, $request, $this->type, $e);
$error_event = new GetResponseForExceptionEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST, $e);
$dispatcher->dispatch(KernelEvents::EXCEPTION, $error_event);
if ($error_event->hasResponse()) {
$response = $error_event->getResponse();
......
<?php
namespace Drupal\Core\EventSubscriber;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
/**
* @file
*
* Description goes here.
*/
/**
* Description of HtmlSubscriber
*/
class HtmlSubscriber implements EventSubscriberInterface {
protected function isHtmlRequestEvent(GetResponseEvent $event) {
return in_array('text/html', $event->getRequest()->getAcceptableContentTypes());
}
public function onResourceNotFoundException(GetResponseEvent $event) {
if ($this->isHtmlRequestEvent($event) && $event->getException() instanceof ResourceNotFoundException) {
$event->setResponse(new Response('Not Found', 404));
}
}
public function onView(GetResponseEvent $event) {
if ($this->isHtmlRequestEvent($event)) {
$page_callback_result = $event->getControllerResult();
$event->setResponse(new Response(drupal_render_page($page_callback_result)));
}
}
public function onAccessDeniedException(Event $event) {
}
static function getSubscribedEvents() {
$events[KernelEvents::EXCEPTION][] = array('onResourceNotFoundException');
$events[KernelEvents::EXCEPTION][] = array('onAccessDeniedException');
$events[KernelEvents::VIEW][] = array('onView');
return $events;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment