Commit 99b9a6a5 authored by catch's avatar catch
Browse files

Issue #2026431 by YesCT, dawehner, Crell: Make ContentNegotiation a 'internal'...

Issue #2026431 by YesCT, dawehner, Crell: Make ContentNegotiation a 'internal' service, used only by the router, so that core or contrib can implement real negotiation.
parent 3e268109
...@@ -429,27 +429,21 @@ services: ...@@ -429,27 +429,21 @@ services:
tags: tags:
- { name: route_enhancer, priority: 1000 } - { name: route_enhancer, priority: 1000 }
arguments: ['@authentication'] arguments: ['@authentication']
route_enhancer.content_controller:
class: Drupal\Core\Routing\Enhancer\ContentControllerEnhancer
arguments: ['@content_negotiation']
tags:
- { name: route_enhancer, priority: 30 }
route_enhancer.ajax:
class: Drupal\Core\Routing\Enhancer\AjaxEnhancer
arguments: ['@content_negotiation']
tags:
- { name: route_enhancer, priority: 15 }
- { name: legacy_route_enhancer, priority: 15 }
route_enhancer.entity: route_enhancer.entity:
class: Drupal\Core\Entity\Enhancer\EntityRouteEnhancer class: Drupal\Core\Entity\Enhancer\EntityRouteEnhancer
arguments: ['@controller_resolver', '@entity.manager', '@form_builder'] arguments: ['@controller_resolver', '@entity.manager', '@form_builder']
tags: tags:
- { name: route_enhancer, priority: 20 } - { name: route_enhancer, priority: 20 }
route_enhancer.form: route_content_controller_subscriber:
class: Drupal\Core\Routing\Enhancer\FormEnhancer class: Drupal\Core\EventSubscriber\ContentControllerSubscriber
arguments: ['@content_negotiation']
tags:
- { name: event_subscriber }
route_content_form_controller_subscriber:
class: Drupal\Core\EventSubscriber\ContentFormControllerSubscriber
arguments: ['@service_container', '@controller_resolver', '@form_builder'] arguments: ['@service_container', '@controller_resolver', '@form_builder']
tags: tags:
- { name: route_enhancer, priority: 10 } - { name: event_subscriber }
route_special_attributes_subscriber: route_special_attributes_subscriber:
class: Drupal\Core\EventSubscriber\SpecialAttributesRouteSubscriber class: Drupal\Core\EventSubscriber\SpecialAttributesRouteSubscriber
tags: tags:
......
...@@ -2,19 +2,20 @@ ...@@ -2,19 +2,20 @@
/** /**
* @file * @file
* Contains \Drupal\Core\Routing\Enhancer\ContentControllerEnhancer. * Contains \Drupal\Core\EventSubscriber\ContentControllerSubscriber.
*/ */
namespace Drupal\Core\Routing\Enhancer; namespace Drupal\Core\EventSubscriber;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancerInterface;
use Drupal\Core\ContentNegotiation; use Drupal\Core\ContentNegotiation;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
/** /**
* Enhances a route to select a controller based on the mime type of the request. * Defines a subscriber for setting the format of the request.
*/ */
class ContentControllerEnhancer implements RouteEnhancerInterface { class ContentControllerSubscriber implements EventSubscriberInterface {
/** /**
* Content negotiation library. * Content negotiation library.
...@@ -23,6 +24,16 @@ class ContentControllerEnhancer implements RouteEnhancerInterface { ...@@ -23,6 +24,16 @@ class ContentControllerEnhancer implements RouteEnhancerInterface {
*/ */
protected $negotiation; protected $negotiation;
/**
* Constructs a new ContentControllerSubscriber object.
*
* @param \Drupal\Core\ContentNegotiation $negotiation
* The Content Negotiation service.
*/
public function __construct(ContentNegotiation $negotiation) {
$this->negotiation = $negotiation;
}
/** /**
* Associative array of supported mime types and their appropriate controller. * Associative array of supported mime types and their appropriate controller.
* *
...@@ -36,26 +47,47 @@ class ContentControllerEnhancer implements RouteEnhancerInterface { ...@@ -36,26 +47,47 @@ class ContentControllerEnhancer implements RouteEnhancerInterface {
); );
/** /**
* Constructs a new ContentControllerEnhancer object. * Sets the derived request format on the request.
* *
* @param \Drupal\Core\ContentNegotiation $negotiation * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
* The Content Negotiation service. * The event to process.
*/ */
public function __construct(ContentNegotiation $negotiation) { public function onRequestDeriveFormat(GetResponseEvent $event) {
$this->negotiation = $negotiation; $request = $event->getRequest();
if (!$request->attributes->get('_format')) {
$request->setRequestFormat($this->negotiation->getContentType($request));
}
} }
/** /**
* {@inheritdoc} * Sets the _controller on a request based on the request format.
*
* @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
* The event to process.
*/ */
public function enhance(array $defaults, Request $request) { public function onRequestDeriveContentWrapper(GetResponseEvent $event) {
// If no controller is set and either _content is set or the request is $request = $event->getRequest();
// for a dialog or modal, then enhance.
if (empty($defaults['_controller']) && ($type = $this->negotiation->getContentType($request))) { $controller = $request->attributes->get('_controller');
if (empty($controller) && ($type = $request->getRequestFormat())) {
if (isset($this->types[$type])) { if (isset($this->types[$type])) {
$defaults['_controller'] = $this->types[$type]; $request->attributes->set('_controller', $this->types[$type]);
} }
} }
return $defaults;
} }
/**
* Registers the methods in this class that should be listeners.
*
* @return array
* An array of event listener definitions.
*/
static function getSubscribedEvents() {
$events[KernelEvents::REQUEST][] = array('onRequestDeriveFormat', 31);
$events[KernelEvents::REQUEST][] = array('onRequestDeriveContentWrapper', 30);
return $events;
}
} }
...@@ -2,22 +2,23 @@ ...@@ -2,22 +2,23 @@
/** /**
* @file * @file
* Contains \Drupal\Core\Routing\Enhancer\FormEnhancer. * Definition of Drupal\Core\EventSubscriber\ContentFormControllerSubscriber.
*/ */
namespace Drupal\Core\Routing\Enhancer; namespace Drupal\Core\EventSubscriber;
use Drupal\Core\Controller\HtmlFormController; use Drupal\Core\Controller\HtmlFormController;
use Drupal\Core\Controller\ControllerResolverInterface; use Drupal\Core\Controller\ControllerResolverInterface;
use Drupal\Core\Form\FormBuilderInterface; use Drupal\Core\Form\FormBuilderInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancerInterface;
/** /**
* Enhances a form route with the appropriate controller. * Subscriber for setting wrapping form logic.
*/ */
class FormEnhancer implements RouteEnhancerInterface { class ContentFormControllerSubscriber implements EventSubscriberInterface {
/** /**
* The service container. * The service container.
...@@ -41,7 +42,7 @@ class FormEnhancer implements RouteEnhancerInterface { ...@@ -41,7 +42,7 @@ class FormEnhancer implements RouteEnhancerInterface {
protected $formBuilder; protected $formBuilder;
/** /**
* Constructs a new \Drupal\Core\Routing\Enhancer\FormEnhancer object. * Constructs a new ContentFormControllerSubscriber object.
* *
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
* The service container. * The service container.
...@@ -57,14 +58,29 @@ public function __construct(ContainerInterface $container, ControllerResolverInt ...@@ -57,14 +58,29 @@ public function __construct(ContainerInterface $container, ControllerResolverInt
} }
/** /**
* {@inheritdoc} * Sets the _controllere on a request based on the request format.
*
* @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
* The event to process.
*/ */
public function enhance(array $defaults, Request $request) { public function onRequestDeriveFormWrapper(GetResponseEvent $event) {
if (!empty($defaults['_form'])) { $request = $event->getRequest();
$wrapper = new HtmlFormController($this->resolver, $this->container, $defaults['_form'], $this->formBuilder);
$defaults['_content'] = array($wrapper, 'getContentResult'); if ($form = $request->attributes->get('_form')) {
$wrapper = new HtmlFormController($this->resolver, $this->container, $form, $this->formBuilder);
$request->attributes->set('_content', array($wrapper, 'getContentResult'));
} }
return $defaults;
} }
/**
* Registers the methods in this class that should be listeners.
*
* @return array
* An array of event listener definitions.
*/
static function getSubscribedEvents() {
$events[KernelEvents::REQUEST][] = array('onRequestDeriveFormWrapper', 29);
return $events;
}
} }
system.ajax: system.ajax:
path: '/system/ajax' path: '/system/ajax'
defaults: defaults:
_controller: '\Drupal\system\Controller\FormAjaxController::content' _content: '\Drupal\system\Controller\FormAjaxController::content'
options: options:
_theme: ajax_base_page _theme: ajax_base_page
requirements: requirements:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment