diff options
author | Dries Buytaert <dries@buytaert.net> | 2009-10-15 14:07:30 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2009-10-15 14:07:30 +0000 |
commit | f42bca3bd4a7b97e103ddba3fdb4e403f7215b2a (patch) | |
tree | 771d33dbec4ead4d7a3a20022dbbdd669bd32691 /includes/ajax.inc | |
parent | dbac31e066a95983c6a20d9cf9c69f048bb12ead (diff) | |
download | brdo-f42bca3bd4a7b97e103ddba3fdb4e403f7215b2a.tar.gz brdo-f42bca3bd4a7b97e103ddba3fdb4e403f7215b2a.tar.bz2 |
- Patch #599804 by effulgentsia, catch: unify page, AJAX 'path', and AJAX 'callback' callbacks. Oh my, this is the beginning of something big.
Diffstat (limited to 'includes/ajax.inc')
-rw-r--r-- | includes/ajax.inc | 72 |
1 files changed, 51 insertions, 21 deletions
diff --git a/includes/ajax.inc b/includes/ajax.inc index ca198d6fa..0cd6f63e6 100644 --- a/includes/ajax.inc +++ b/includes/ajax.inc @@ -291,30 +291,60 @@ function ajax_form_callback() { $callback = $triggering_element['#ajax']['callback']; } if (!empty($callback) && function_exists($callback)) { - $html = $callback($form, $form_state); - - // If the returned value is a string, assume it is HTML, add the status - // messages, and create a command object to return automatically. We want - // the status messages inside the new wrapper, so that they get replaced - // on subsequent AJAX calls for the same wrapper. - if (is_string($html)) { - $commands = array(); - $commands[] = ajax_command_replace(NULL, $html); - $commands[] = ajax_command_prepend(NULL, theme('status_messages')); - } - // Otherwise, $html is supposed to be an array of commands, suitable for - // Drupal.ajax, so we pass it on as is. In this situation, the callback is - // doing something fancy, so let it decide how to handle status messages - // without second guessing it. - else { - $commands = $html; - } + return $callback($form, $form_state); + } +} - ajax_render($commands); +/** + * Package and send the result of a page callback to the browser as an AJAX response. + * + * @param $page_callback_result + * The result of a page callback. Can be one of: + * - NULL: to indicate no content. + * - An integer menu status constant: to indicate an error condition. + * - A string of HTML content. + * - A renderable array of content. + */ +function ajax_deliver($page_callback_result) { + $commands = array(); + if (!isset($page_callback_result)) { + // Simply delivering an empty commands array is sufficient. This results + // in the AJAX request being completed, but nothing being done to the page. } + elseif (is_int($page_callback_result)) { + switch ($page_callback_result) { + case MENU_NOT_FOUND: + $commands[] = ajax_command_alert(t('The requested page could not be found.')); + break; + + case MENU_ACCESS_DENIED: + $commands[] = ajax_command_alert(t('You are not authorized to access this page.')); + break; - // Return a 'do nothing' command if there was no callback. - ajax_render(array()); + case MENU_SITE_OFFLINE: + $commands[] = ajax_command_alert(filter_xss_admin(variable_get('maintenance_mode_message', + t('@site is currently under maintenance. We should be back shortly. Thank you for your patience.', array('@site' => variable_get('site_name', 'Drupal')))))); + break; + } + } + elseif (is_array($page_callback_result) && isset($page_callback_result['#type']) && ($page_callback_result['#type'] == 'ajax_commands')) { + // Complex AJAX callbacks can return a result that contains a specific + // set of commands to send to the browser. + if (isset($page_callback_result['#ajax_commands'])) { + $commands = $page_callback_result['#ajax_commands']; + } + } + else { + // Like normal page callbacks, simple AJAX callbacks can return html + // content, as a string or renderable array, to replace what was previously + // there in the wrapper. In this case, in addition to the content, we want + // to add the status messages, but inside the new wrapper, so that they get + // replaced on subsequent AJAX calls for the same wrapper. + $html = is_string($page_callback_result) ? $page_callback_result : drupal_render($page_callback_result); + $commands[] = ajax_command_replace(NULL, $html); + $commands[] = ajax_command_prepend(NULL, theme('status_messages')); + } + ajax_render($commands); } /** |