Commit 80fd0f97 authored by webchick's avatar webchick

Issue #1843220 by mkadin, dawehner, Crell, effulgentsia, Wim Leers: Convert...

Issue #1843220 by mkadin, dawehner, Crell, effulgentsia, Wim Leers: Convert form AJAX to new AJAX API.
parent d35e3cd8
<?php
/**
* @file
* Contains \Drupal\Core\Ajax\AjaxSubscriber.
*/
namespace Drupal\Core\Ajax;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Subscribes to the kernel request event to add the Ajax media type.
*
* @param \Symfont\Component\HttpKernel\Event\GetResponseEvent $event
* The event to process.
*/
class AjaxSubscriber implements EventSubscriberInterface {
/**
* Registers Ajax formats with the Request class.
*/
public function onKernelRequest(GetResponseEvent $event) {
$request = $event->getRequest();
// @todo Refactor 'drupal_ajax' to just 'ajax' once all Ajax is converted to
// Drupal 8's API.
$request->setFormat('drupal_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
<?php
/**
* @file
* Contains \Drupal\Core\AjaxController.
*/
namespace Drupal\Core;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\InsertCommand;
use Drupal\Core\Ajax\PrependCommand;
use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Default controller for ajax requests.
*/
class AjaxController extends ContainerAware {
/**
* Controller method for AJAX content.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The request object.
* @param callable $_content
* The callable that returns the content of the ajax response.
*
* @return \Symfony\Component\HttpFoundation\Response
* A response object.
*/
public function content(Request $request, $_content) {
// @todo When we have a Generator, we can replace the forward() call with
// a render() call, which would handle ESI and hInclude as well. That will
// require an _internal route. For examples, see:
// https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Resources/config/routing/internal.xml
// https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Controller/InternalController.php
$attributes = clone $request->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;
}
}
......@@ -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 === 'drupal_ajax') {
return $format;
}
if (!is_null($format) && !$first_found_format) {
......@@ -58,6 +53,10 @@ public function getContentType(Request $request) {
return $first_found_format;
}
if ($request->isXmlHttpRequest()) {
return 'ajax';
}
// Do HTML last so that it always wins.
return 'html';
}
......
......@@ -303,7 +303,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());
......
......@@ -39,7 +39,11 @@ public function onRequestSetController(GetResponseEvent $event) {
$request = $event->getRequest();
// @todo This should all get converted into one or more RouteEnhancers.
if (!$request->attributes->has('_controller') && $this->negotiation->getContentType($request) === 'html') {
if (!$request->attributes->has('_content') && $this->negotiation->getContentType($request) == 'drupal_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') {
if ($request->attributes->has('_form')) {
$request->attributes->set('_controller', '\Drupal\Core\HtmlFormController::content');
}
......@@ -49,7 +53,6 @@ public function onRequestSetController(GetResponseEvent $event) {
else {
throw new \InvalidArgumentException('No valid subcontroller key was found');
}
}
}
......@@ -65,4 +68,5 @@ static function getSubscribedEvents() {
return $events;
}
}
......@@ -208,6 +208,9 @@ Drupal.ajax = function (base, element, element_settings) {
}
},
dataType: 'json',
accepts: {
json: 'application/vnd.drupal-ajax'
},
type: 'POST'
};
......
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