Commit 08c2cdde authored by pdrake's avatar pdrake
Browse files

Add onAjax and onIframeUpload handlers to ViewSubscriber. Add...

Add onAjax and onIframeUpload handlers to ViewSubscriber.  Add x-requested-with header to AJAX tests.
parent 2beb7964
......@@ -12,10 +12,18 @@ class ContentNegotiation {
public function getContentType(Request $request) {
$acceptable_content_types = $request->getAcceptableContentTypes();
if (in_array('application/json', $request->getAcceptableContentTypes())) {
if ($request->isXmlHttpRequest()) {
if ($request->get('ajax_iframe_upload', FALSE)) {
return 'iframeupload';
}
else {
return 'ajax';
}
}
elseif (in_array('application/json', $request->getAcceptableContentTypes())) {
return 'json';
}
if(in_array('text/html', $acceptable_content_types) || in_array('*/*', $acceptable_content_types)) {
elseif(in_array('text/html', $acceptable_content_types) || in_array('*/*', $acceptable_content_types)) {
return 'html';
}
}
......
......@@ -37,6 +37,27 @@ protected function createJsonResponse() {
return $response;
}
protected function createAjaxResponse(GetResponseEvent $event) {
$response = new Response();
$response->headers->set('Content-Type', 'application/json; charset=utf-8');
return $response;
}
protected function createIframeUploadResponse(GetResponseEvent $event) {
$response = new Response();
// Browsers do not allow JavaScript to read the contents of a user's local
// files. To work around that, the jQuery Form plugin submits forms containing
// a file input element to an IFRAME, instead of using XHR. Browsers do not
// normally expect JSON strings as content within an IFRAME, so the response
// must be customized accordingly.
// @see http://malsup.com/jquery/form/#file-upload
// @see Drupal.ajax.prototype.beforeSend()
$response->headers->set('Content-Type', 'text/html; charset=utf-8');
return $response;
}
/**
* Processes a successful controller into an HTTP 200 response.
......@@ -75,6 +96,42 @@ public function onJson(GetResponseEvent $event) {
return $response;
}
public function onAjax(GetResponseEvent $event) {
$page_callback_result = $event->getControllerResult();
// Construct the response content from the page callback result.
$commands = ajax_prepare_response($page_callback_result);
$json = ajax_render($commands);
// Build the actual response object.
$response = $this->createAjaxResponse($event);
$response->setContent($json);
return $response;
}
public function onIframeUpload(GetResponseEvent $event) {
$page_callback_result = $event->getControllerResult();
// Construct the response content from the page callback result.
$commands = ajax_prepare_response($page_callback_result);
$json = ajax_render($commands);
// Browser IFRAMEs expect HTML. Browser extensions, such as Linkification
// and Skype's Browser Highlighter, convert URLs, phone numbers, etc. into
// links. This corrupts the JSON response. Protect the integrity of the
// JSON data by making it the value of a textarea.
// @see http://malsup.com/jquery/form/#file-upload
// @see http://drupal.org/node/1009382
$json = '<textarea>' . $json . '</textarea>';
// Build the actual response object.
$response = $this->createIframeUploadResponse($event);
$response->setContent($json);
return $response;
}
/**
* Processes a successful controller into an HTTP 200 response.
*
......
......@@ -1834,6 +1834,7 @@ protected function drupalGet($path, array $options = array(), array $headers = a
* Retrieve a Drupal path or an absolute path and JSON decode the result.
*/
protected function drupalGetAJAX($path, array $options = array(), array $headers = array()) {
$headers[] = 'X-Requested-With: XMLHttpRequest';
return drupal_json_decode($this->drupalGet($path, $options, $headers));
}
......@@ -2041,6 +2042,7 @@ protected function drupalPostAJAX($path, $edit, $triggering_element, $ajax_path
}
$content = $this->content;
$drupal_settings = $this->drupalSettings;
$headers[] = 'X-Requested-With: XMLHttpRequest';
// Get the Ajax settings bound to the triggering element.
if (!isset($ajax_settings)) {
......
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