diff --git a/core/lib/Drupal/Core/Ajax/AjaxSubscriber.php b/core/lib/Drupal/Core/Ajax/AjaxSubscriber.php new file mode 100644 index 0000000..7d2ed7b --- /dev/null +++ b/core/lib/Drupal/Core/Ajax/AjaxSubscriber.php @@ -0,0 +1,41 @@ +getRequest(); + $request->setFormat('ajax', 'application/vnd.drupal-ajax'); + } + + /** + * 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('onKernelRequest', 50); + return $events; + } + +} \ No newline at end of file diff --git a/core/lib/Drupal/Core/AjaxController.php b/core/lib/Drupal/Core/AjaxController.php new file mode 100644 index 0000000..31259fb --- /dev/null +++ b/core/lib/Drupal/Core/AjaxController.php @@ -0,0 +1,79 @@ +attributes; + $controller = $_content; + + // We need to clean up the derived information and such so that the + // subrequest can be processed properly without leaking data through. + $attributes->remove('system_path'); + $attributes->remove('_content'); + $attributes->remove('_legacy'); + + // Remove the accept header so the subrequest does not end up back in this + // controller. + $request->headers->remove('accept'); + + $response = $this->container->get('http_kernel')->forward($controller, $attributes->all(), $request->query->all()); + // For successful (HTTP status 200) responses. + if ($response->isOk()) { + // If there is already an AjaxResponse, then return it without + // manipulation. + if (!($response instanceof AjaxResponse)) { + // Pull the content out of the response. + $content = $response->getContent(); + // A page callback could return a render array or a string. + $html = is_string($content) ? $content : drupal_render($content); + $response = new AjaxResponse(); + // The selector for the insert command is NULL as the new content will + // replace the element making the ajax call. The default 'replaceWith' + // behavior can be changed with #ajax['method']. + $response->addCommand(new InsertCommand(NULL, $html)); + $status_messages = theme('status_messages'); + if (!empty($status_messages)) { + $response->addCommand(new PrependCommand(NULL, $status_messages)); + } + } + } + return $response; + } +} + + diff --git a/core/lib/Drupal/Core/ContentNegotiation.php b/core/lib/Drupal/Core/ContentNegotiation.php index 1695de9..5dfefc2 100644 --- a/core/lib/Drupal/Core/ContentNegotiation.php +++ b/core/lib/Drupal/Core/ContentNegotiation.php @@ -36,16 +36,11 @@ public function getContentType(Request $request) { return 'iframeupload'; } - // AJAX calls need to be run through ajax rendering functions - elseif ($request->isXmlHttpRequest()) { - return 'ajax'; - } - // Check all formats, it HTML is found return it. $first_found_format = FALSE; foreach ($request->getAcceptableContentTypes() as $mime_type) { $format = $request->getFormat($mime_type); - if ($format === 'html') { + if ($format === 'html' || $format === 'ajax') { return $format; } if (!is_null($format) && !$first_found_format) { diff --git a/core/lib/Drupal/Core/CoreBundle.php b/core/lib/Drupal/Core/CoreBundle.php index 88b25bf..b59be66 100644 --- a/core/lib/Drupal/Core/CoreBundle.php +++ b/core/lib/Drupal/Core/CoreBundle.php @@ -304,7 +304,10 @@ public function build(ContainerBuilder $container) { $container->register('plugin.manager.condition', 'Drupal\Core\Condition\ConditionManager'); $container->register('kernel_destruct_subscriber', 'Drupal\Core\EventSubscriber\KernelDestructionSubscriber') - ->addMethodCall('setContainer', array(new Reference('service_container'))) + ->addMethodCall('setContainer', array(new Reference('service_container'))); + + // Register Ajax event subscriber. + $container->register('ajax.subscriber', 'Drupal\Core\Ajax\AjaxSubscriber') ->addTag('event_subscriber'); $container->addCompilerPass(new RegisterMatchersPass()); diff --git a/core/lib/Drupal/Core/EventSubscriber/RouteProcessorSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/RouteProcessorSubscriber.php index 0061530..58387b2 100644 --- a/core/lib/Drupal/Core/EventSubscriber/RouteProcessorSubscriber.php +++ b/core/lib/Drupal/Core/EventSubscriber/RouteProcessorSubscriber.php @@ -37,8 +37,11 @@ public function __construct(ContentNegotiation $negotiation) { */ public function onRequestSetController(GetResponseEvent $event) { $request = $event->getRequest(); - - if (!$request->attributes->has('_controller') && $this->negotiation->getContentType($request) === 'html') { + if (!$request->attributes->has('_content') && $this->negotiation->getContentType($request) == 'ajax') { + $request->attributes->set('_content', $request->attributes->get('_controller')); + $request->attributes->set('_controller', '\Drupal\Core\AjaxController::content'); + } + elseif (!$request->attributes->has('_controller') && $this->negotiation->getContentType($request) === 'html') { $request->attributes->set('_controller', '\Drupal\Core\HtmlPageController::content'); } } @@ -55,4 +58,5 @@ static function getSubscribedEvents() { return $events; } + } diff --git a/core/misc/ajax.js b/core/misc/ajax.js index 53351ad..7ca4463 100644 --- a/core/misc/ajax.js +++ b/core/misc/ajax.js @@ -208,6 +208,9 @@ Drupal.ajax = function (base, element, element_settings) { } }, dataType: 'json', + accepts: { + json: 'application/vnd.drupal-ajax' + }, type: 'POST' };