JSON response object for AJAX requests.
Expanded class hierarchy of AjaxResponse
class AjaxResponse extends JsonResponse {
/**
* The array of ajax commands.
*
* @var array
*/
protected $commands = array();
/**
* Add an AJAX command to the response.
*
* @param object $command
* An AJAX command object implementing CommandInterface.
* @param boolean $prepend
* A boolean which determines whether the new command should be executed
* before previously added commands. Defaults to FALSE.
*
* @return AjaxResponse
* The current AjaxResponse.
*/
public function addCommand($command, $prepend = FALSE) {
if ($prepend) {
array_unshift($this->commands, $command
->render());
}
else {
$this->commands[] = $command
->render();
}
return $this;
}
/**
* Sets the response's data to be the array of AJAX commands.
*
* @param Request $request
* A request object.
*
* @return Response
* The current response.
*/
public function prepare(Request $request) {
$this
->setData($this
->ajaxRender($request));
return parent::prepare($request);
}
/**
* Prepares the AJAX commands for sending back to the client.
*
* @param Request $request
* The request object that the AJAX is responding to.
*
* @return array
* An array of commands ready to be returned as JSON.
*/
protected function ajaxRender(Request $request) {
// Ajax responses aren't rendered with html.tpl.php, so we have to call
// drupal_get_css() and drupal_get_js() here, in order to have new files
// added during this request to be loaded by the page. We only want to send
// back files that the page hasn't already loaded, so we implement simple
// diffing logic using array_diff_key().
$ajax_page_state = $request->request
->get('ajax_page_state');
foreach (array(
'css',
'js',
) as $type) {
// It is highly suspicious if $_POST['ajax_page_state'][$type] is empty,
// since the base page ought to have at least one JS file and one CSS file
// loaded. It probably indicates an error, and rather than making the page
// reload all of the files, instead we return no new files.
if (empty($ajax_page_state[$type])) {
$items[$type] = array();
}
else {
$function = 'drupal_add_' . $type;
$items[$type] = $function();
drupal_alter($type, $items[$type]);
// @todo Inline CSS and JS items are indexed numerically. These can't be
// reliably diffed with array_diff_key(), since the number can change
// due to factors unrelated to the inline content, so for now, we
// strip the inline items from Ajax responses, and can add support for
// them when drupal_add_css() and drupal_add_js() are changed to use
// a hash of the inline content as the array key.
foreach ($items[$type] as $key => $item) {
if (is_numeric($key)) {
unset($items[$type][$key]);
}
}
// Ensure that the page doesn't reload what it already has.
$items[$type] = array_diff_key($items[$type], $ajax_page_state[$type]);
}
}
// Render the HTML to load these files, and add AJAX commands to insert this
// HTML in the page. We pass TRUE as the $skip_alter argument to prevent the
// data from being altered again, as we already altered it above. Settings
// are handled separately, afterwards.
if (isset($items['js']['settings'])) {
unset($items['js']['settings']);
}
$styles = drupal_get_css($items['css'], TRUE);
$scripts_footer = drupal_get_js('footer', $items['js'], TRUE);
$scripts_header = drupal_get_js('header', $items['js'], TRUE);
// Prepend commands to add the resources, preserving their relative order.
$resource_commands = array();
if (!empty($styles)) {
$resource_commands[] = new AddCssCommand($styles);
}
if (!empty($scripts_header)) {
$resource_commands[] = new PrependCommand('head', $scripts_header);
}
if (!empty($scripts_footer)) {
$resource_commands[] = new AppendCommand('body', $scripts_footer);
}
foreach (array_reverse($resource_commands) as $resource_command) {
$this
->addCommand($resource_command, TRUE);
}
// Prepend a command to merge changes and additions to Drupal.settings.
$scripts = drupal_add_js();
if (!empty($scripts['settings'])) {
$settings = drupal_merge_js_settings($scripts['settings']['data']);
$this
->addCommand(new SettingsCommand($settings, TRUE), TRUE);
}
$commands = $this->commands;
drupal_alter('ajax_render', $commands);
return $commands;
}
}
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AjaxResponse:: |
protected | property | The array of ajax commands. | |
AjaxResponse:: |
public | function | Add an AJAX command to the response. | |
AjaxResponse:: |
protected | function | Prepares the AJAX commands for sending back to the client. | |
AjaxResponse:: |
public | function |
Sets the response's data to be the array of AJAX commands. Overrides Response:: |
|
JsonResponse:: |
protected | property | ||
JsonResponse:: |
protected | property | ||
JsonResponse:: |
public static | function |
Factory method for chainability Overrides Response:: |
|
JsonResponse:: |
public | function | Sets the JSONP callback. | |
JsonResponse:: |
public | function | Sets the data to be sent as json. | |
JsonResponse:: |
protected | function | Updates the content and headers according to the json data and callback. | |
JsonResponse:: |
public | function |
Constructor. Overrides Response:: |
|
Response:: |
protected | property | ||
Response:: |
protected | property | ||
Response:: |
public | property | ||
Response:: |
protected | property | ||
Response:: |
protected | property | ||
Response:: |
public static | property | Status codes translation table. | |
Response:: |
protected | property | ||
Response:: |
protected | function | Check if we need to remove Cache-Control for ssl encrypted downloads when using IE < 9 | |
Response:: |
public | function | Marks the response stale by setting the Age header to be equal to the maximum age of the response. | |
Response:: |
public | function | Returns the age of the response. | |
Response:: |
public | function | Retrieves the response charset. | |
Response:: |
public | function | Gets the current response content. | 2 |
Response:: |
public | function | Returns the Date header as a DateTime instance. | |
Response:: |
public | function | Returns the literal value of the ETag HTTP header. | |
Response:: |
public | function | Returns the value of the Expires header as a DateTime instance. | |
Response:: |
public | function | Returns the Last-Modified HTTP header as a DateTime instance. | |
Response:: |
public | function | Returns the number of seconds after the time specified in the response's Date header when the response should no longer be considered fresh. | |
Response:: |
public | function | Gets the HTTP protocol version. | |
Response:: |
public | function | Retrieves the status code for the current web response. | |
Response:: |
public | function | Returns the response's time-to-live in seconds. | |
Response:: |
public | function | Returns an array of header names given in the Vary header. | |
Response:: |
public | function | Returns true if the response includes a Vary header. | |
Response:: |
public | function | Returns true if the response is worth caching under any circumstance. | |
Response:: |
public | function | Is there a client error? | |
Response:: |
public | function | Is the response empty? | |
Response:: |
public | function | Is the response forbidden? | |
Response:: |
public | function | Returns true if the response is "fresh". | |
Response:: |
public | function | Is response informative? | |
Response:: |
public | function | Is response invalid? | |
Response:: |
public | function | Is the response a not found error? | |
Response:: |
public | function | Determines if the Response validators (ETag, Last-Modified) match a conditional value specified in the Request. | |
Response:: |
public | function | Is the response OK? | |
Response:: |
public | function | Is the response a redirect of some form? | |
Response:: |
public | function | Is the response a redirect? | |
Response:: |
public | function | Was there a server side error? | |
Response:: |
public | function | Is response successful? | |
Response:: |
public | function | Returns true if the response includes headers that can be used to validate the response with the origin server using a conditional GET request. | |
Response:: |
public | function | Returns true if the response must be revalidated by caches. | |
Response:: |
public | function | Sends HTTP headers and content. | |
Response:: |
public | function | Sends content for the current web response. | 2 |
Response:: |
public | function | Sends HTTP headers. | |
Response:: |
public | function | Sets the response's cache headers (validation and/or expiration). | |
Response:: |
public | function | Sets the response charset. | |
Response:: |
public | function | Sets the response's time-to-live for private/client caches. | |
Response:: |
public | function | Sets the response content. | 2 |
Response:: |
public | function | Sets the Date header. | |
Response:: |
public | function | Sets the ETag value. | |
Response:: |
public | function | Sets the Expires HTTP header with a DateTime instance. | |
Response:: |
public | function | Sets the Last-Modified HTTP header with a DateTime instance. | |
Response:: |
public | function | Sets the number of seconds after which the response should no longer be considered fresh. | |
Response:: |
public | function | Modifies the response so that it conforms to the rules defined for a 304 status code. | |
Response:: |
public | function | Marks the response as "private". | |
Response:: |
public | function | Sets the HTTP protocol version (1.0 or 1.1). | |
Response:: |
public | function | Marks the response as "public". | |
Response:: |
public | function | Sets the number of seconds after which the response should no longer be considered fresh by shared caches. | |
Response:: |
public | function | Sets the response status code. | |
Response:: |
public | function | Sets the response's time-to-live for shared caches. | |
Response:: |
public | function | Sets the Vary header. | |
Response:: |
public | function | Clones the current Response instance. | |
Response:: |
public | function | Returns the Response as an HTTP string. |