diff --git a/core/lib/Drupal/Core/Ajax/AjaxResponse.php b/core/lib/Drupal/Core/Ajax/AjaxResponse.php index 12250b9..f0bc82c 100644 --- a/core/lib/Drupal/Core/Ajax/AjaxResponse.php +++ b/core/lib/Drupal/Core/Ajax/AjaxResponse.php @@ -27,20 +27,29 @@ class AjaxResponse extends JsonResponse { * * @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) { - $this->commands[] = $command->render(); + 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 A request object. + * @param Request $request + * A request object. * * @return * Response The current response. @@ -54,24 +63,25 @@ public function prepare(Request $request) { /** * Prepares the AJAX commands for sending back to the client. * - * @param Request + * @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) { + 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($request->parameters['ajax_page_state'][$type])) { + if (empty($ajax_page_state[$type])) { $items[$type] = array(); } else { @@ -90,7 +100,7 @@ protected function ajaxRender($request) { } } // Ensure that the page doesn't reload what it already has. - $items[$type] = array_diff_key($items[$type], $request->parameters['ajax_page_state'][$type]); + $items[$type] = array_diff_key($items[$type], $ajax_page_state[$type]); } } @@ -119,7 +129,7 @@ protected function ajaxRender($request) { $scripts = drupal_add_js(); if (!empty($scripts['settings'])) { $settings = $scripts['settings']; - $this->addCommand(new SettingsCommand(call_user_func_array('array_merge_recursive', $settings['data']), TRUE)); + $this->addCommand(new SettingsCommand(call_user_func_array('array_merge_recursive', $settings['data']), TRUE), TRUE); } $commands = $this->commands;