Commit 99b9a6a5 authored by catch's avatar catch

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:
tags:
- { name: route_enhancer, priority: 1000 }
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:
class: Drupal\Core\Entity\Enhancer\EntityRouteEnhancer
arguments: ['@controller_resolver', '@entity.manager', '@form_builder']
tags:
- { name: route_enhancer, priority: 20 }
route_enhancer.form:
class: Drupal\Core\Routing\Enhancer\FormEnhancer
route_content_controller_subscriber:
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']
tags:
- { name: route_enhancer, priority: 10 }
- { name: event_subscriber }
route_special_attributes_subscriber:
class: Drupal\Core\EventSubscriber\SpecialAttributesRouteSubscriber
tags:
......
......@@ -2,19 +2,20 @@
/**
* @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 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.
......@@ -23,6 +24,16 @@ class ContentControllerEnhancer implements RouteEnhancerInterface {
*/
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.
*
......@@ -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
* The Content Negotiation service.
* @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
* The event to process.
*/
public function __construct(ContentNegotiation $negotiation) {
$this->negotiation = $negotiation;
public function onRequestDeriveFormat(GetResponseEvent $event) {
$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) {
// If no controller is set and either _content is set or the request is
// for a dialog or modal, then enhance.
if (empty($defaults['_controller']) && ($type = $this->negotiation->getContentType($request))) {
public function onRequestDeriveContentWrapper(GetResponseEvent $event) {
$request = $event->getRequest();
$controller = $request->attributes->get('_controller');
if (empty($controller) && ($type = $request->getRequestFormat())) {
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 @@
/**
* @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\ControllerResolverInterface;
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\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.
......@@ -41,7 +42,7 @@ class FormEnhancer implements RouteEnhancerInterface {
protected $formBuilder;
/**
* Constructs a new \Drupal\Core\Routing\Enhancer\FormEnhancer object.
* Constructs a new ContentFormControllerSubscriber object.
*
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
* The service container.
......@@ -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) {
if (!empty($defaults['_form'])) {
$wrapper = new HtmlFormController($this->resolver, $this->container, $defaults['_form'], $this->formBuilder);
$defaults['_content'] = array($wrapper, 'getContentResult');
public function onRequestDeriveFormWrapper(GetResponseEvent $event) {
$request = $event->getRequest();
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:
path: '/system/ajax'
defaults:
_controller: '\Drupal\system\Controller\FormAjaxController::content'
_content: '\Drupal\system\Controller\FormAjaxController::content'
options:
_theme: ajax_base_page
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