diff --git a/core/core.services.yml b/core/core.services.yml index b6decf3..a1648c9 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -339,7 +339,7 @@ services: - { name: event_subscriber } router.route_preloader: class: Drupal\Core\Routing\RoutePreloader - arguments: ['@router.route_provider', '@state', '@content_negotiation'] + arguments: ['@router.route_provider', '@state'] tags: - { name: 'event_subscriber' } router.matcher.final_matcher: @@ -515,12 +515,12 @@ services: - { name: event_subscriber } arguments: ['@router', '@router.request_context', NULL, '@request_stack'] content_negotiation: - class: Drupal\Core\ContentNegotiation + class: Negotiation\FormatNegotiator view_subscriber: class: Drupal\Core\EventSubscriber\ViewSubscriber tags: - { name: event_subscriber } - arguments: ['@content_negotiation', '@title_resolver', '@ajax_response_renderer'] + arguments: ['@title_resolver', '@ajax_response_renderer'] html_view_subscriber: class: Drupal\Core\EventSubscriber\HtmlViewSubscriber tags: diff --git a/core/lib/Drupal/Core/EventSubscriber/ContentControllerSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/ContentControllerSubscriber.php index 377ae93..9fae03e 100644 --- a/core/lib/Drupal/Core/EventSubscriber/ContentControllerSubscriber.php +++ b/core/lib/Drupal/Core/EventSubscriber/ContentControllerSubscriber.php @@ -7,7 +7,7 @@ namespace Drupal\Core\EventSubscriber; -use Drupal\Core\ContentNegotiation; +use Negotiation\FormatNegotiatorInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpKernel\Event\GetResponseEvent; use Symfony\Component\HttpKernel\KernelEvents; @@ -20,17 +20,17 @@ class ContentControllerSubscriber implements EventSubscriberInterface { /** * Content negotiation library. * - * @var \Drupal\Core\ContentNegotiation + * @var \Negotiation\FormatNegotiatorInterface */ protected $negotiation; /** * Constructs a new ContentControllerSubscriber object. * - * @param \Drupal\Core\ContentNegotiation $negotiation + * @param \Negotiation\FormatNegotiatorInterface $negotiation * The Content Negotiation service. */ - public function __construct(ContentNegotiation $negotiation) { + public function __construct(FormatNegotiatorInterface $negotiation) { $this->negotiation = $negotiation; } @@ -56,7 +56,16 @@ public function onRequestDeriveFormat(GetResponseEvent $event) { $request = $event->getRequest(); if (!$request->attributes->get('_format')) { - $request->setRequestFormat($this->negotiation->getContentType($request)); + // The following formats have special high-priority according to Drupal. + // Any other formats should negotiate in order as appropriate. + $priorities = array('text/html', 'application/vnd.drupal-ajax', 'application/vnd.drupal-modal', 'application/vnd.drupal-dialog'); + + // Because we're registering new formats on the Request object, we will + // retrieve the raw mime type and then let the request map that to + // the format machine name. That way we only need to register new formats + // on the one object. + $format = $this->negotiation->getBest($accept_header = $request->headers->get('Accept'), $priorities); + $request->setRequestFormat($request->getFormat($format->getValue())); } } @@ -84,7 +93,7 @@ public function onRequestDeriveContentWrapper(GetResponseEvent $event) { * An array of event listener definitions. */ static function getSubscribedEvents() { - $events[KernelEvents::REQUEST][] = array('onRequestDeriveFormat', 31); + $events[KernelEvents::REQUEST][] = array('onRequestDeriveFormat', 33); $events[KernelEvents::REQUEST][] = array('onRequestDeriveContentWrapper', 30); return $events; diff --git a/core/lib/Drupal/Core/EventSubscriber/ViewSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/ViewSubscriber.php index 84b94c9..91eba40 100644 --- a/core/lib/Drupal/Core/EventSubscriber/ViewSubscriber.php +++ b/core/lib/Drupal/Core/EventSubscriber/ViewSubscriber.php @@ -18,8 +18,6 @@ use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent; use Symfony\Component\EventDispatcher\EventSubscriberInterface; -use Drupal\Core\ContentNegotiation; - /** * Main subscriber for VIEW HTTP responses. * @@ -53,15 +51,12 @@ class ViewSubscriber implements EventSubscriberInterface { /** * Constructs a new ViewSubscriber. * - * @param \Drupal\Core\ContentNegotiation $negotiation - * The content negotiation. * @param \Drupal\Core\Controller\TitleResolverInterface $title_resolver * The title resolver. * @param \Drupal\Core\Ajax\AjaxResponseRenderer $ajax_renderer * The ajax response renderer. */ - public function __construct(ContentNegotiation $negotiation, TitleResolverInterface $title_resolver, AjaxResponseRenderer $ajax_renderer) { - $this->negotiation = $negotiation; + public function __construct(TitleResolverInterface $title_resolver, AjaxResponseRenderer $ajax_renderer) { $this->titleResolver = $title_resolver; $this->ajaxRenderer = $ajax_renderer; } @@ -88,7 +83,7 @@ public function onView(GetResponseForControllerResultEvent $event) { // object. The subrequest's response will get dissected and placed into // the larger page as needed. if ($event->getRequestType() == HttpKernelInterface::MASTER_REQUEST) { - $method = 'on' . $this->negotiation->getContentType($request); + $method = 'on' . $request->getRequestFormat(); if (method_exists($this, $method)) { $event->setResponse($this->$method($event)); diff --git a/core/lib/Drupal/Core/Routing/AcceptHeaderMatcher.php b/core/lib/Drupal/Core/Routing/AcceptHeaderMatcher.php index 34f0ae5..d964cc0 100644 --- a/core/lib/Drupal/Core/Routing/AcceptHeaderMatcher.php +++ b/core/lib/Drupal/Core/Routing/AcceptHeaderMatcher.php @@ -8,7 +8,7 @@ namespace Drupal\Core\Routing; use Drupal\Component\Utility\String; -use Drupal\Core\ContentNegotiation; +use Negotiation\FormatNegotiatorInterface; use Symfony\Cmf\Component\Routing\NestedMatcher\RouteFilterInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException; @@ -22,18 +22,18 @@ class AcceptHeaderMatcher implements RouteFilterInterface { /** * The content negotiation library. * - * @var \Drupal\Core\ContentNegotiation + * @var \Negotiation\FormatNegotiatorInterface */ - protected $contentNegotiation; + protected $negotiation; /** * Constructs a new AcceptHeaderMatcher. * - * @param \Drupal\Core\ContentNegotiation $cotent_negotiation + * @param \Negotiation\FormatNegotiatorInterface; $negotiation * The content negotiation library. */ - public function __construct(ContentNegotiation $content_negotiation) { - $this->contentNegotiation = $content_negotiation; + public function __construct(FormatNegotiatorInterface $negotiation) { + $this->negotiation = $negotiation; } /** @@ -44,7 +44,7 @@ public function filter(RouteCollection $collection, Request $request) { // @todo replace by proper content negotiation library. $acceptable_mime_types = $request->getAcceptableContentTypes(); $acceptable_formats = array_filter(array_map(array($request, 'getFormat'), $acceptable_mime_types)); - $primary_format = $this->contentNegotiation->getContentType($request); + $primary_format = $request->getRequestFormat(); foreach ($collection as $name => $route) { // _format could be a |-delimited list of supported formats. diff --git a/core/lib/Drupal/Core/Routing/RoutePreloader.php b/core/lib/Drupal/Core/Routing/RoutePreloader.php index 7952d17..8e8d230 100644 --- a/core/lib/Drupal/Core/Routing/RoutePreloader.php +++ b/core/lib/Drupal/Core/Routing/RoutePreloader.php @@ -38,13 +38,6 @@ class RoutePreloader implements EventSubscriberInterface { protected $state; /** - * The content negotiation. - * - * @var \Drupal\Core\ContentNegotiation - */ - protected $negotiation; - - /** * Contains the non-admin routes while rebuilding the routes. * * @var array @@ -58,13 +51,10 @@ class RoutePreloader implements EventSubscriberInterface { * The route provider. * @param \Drupal\Core\State\StateInterface $state * The state key value store. - * @param \Drupal\Core\ContentNegotiation $negotiation - * The content negotiation. */ - public function __construct(RouteProviderInterface $route_provider, StateInterface $state, ContentNegotiation $negotiation) { + public function __construct(RouteProviderInterface $route_provider, StateInterface $state) { $this->routeProvider = $route_provider; $this->state = $state; - $this->negotiation = $negotiation; } /** @@ -75,7 +65,7 @@ public function __construct(RouteProviderInterface $route_provider, StateInterfa */ public function onRequest(KernelEvent $event) { // Just preload on normal HTML pages, as they will display menu links. - if ($this->negotiation->getContentType($event->getRequest()) == 'html') { + if ($event->getRequest()->getRequestFormat() == 'html') { $this->loadNonAdminRoutes(); } }