drupal_deliver_page
- Versions
- 7
drupal_deliver_page($page_callback_result, $default_delivery_callback = NULL)
Deliver a page callback result to the browser in the format appropriate.
This function is most commonly called by menu_execute_active_handler(), but can also be called by error conditions such as drupal_not_found(), drupal_access_denied(), and drupal_site_offline().
When a user requests a page, index.php calls menu_execute_active_handler() which calls the 'page callback' function registered in hook_menu(). The page callback function can return 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
Returning a renderable array rather than a string of HTML is preferred, because that provides modules with more flexibility in customizing the final result.
When the page callback returns its constructed content to menu_execute_active_handler(), this functions gets called. The purpose of this function is to determine the most appropriate 'delivery callback' function to route the content to. The delivery callback function then sends the content to the browser in the needed format. The default delivery callback is drupal_deliver_html_page() which delivers the content as an HTML page, complete with blocks in addition to the content. This default can be overridden on a per menu item basis by setting 'delivery callback' in hook_menu(), hook_menu_alter(), or hook_menu_active_handler_alter(). Additionally, modules may use hook_page_delivery_callback_alter() to specify a different delivery callback to use for the page request.
For example, the same page callback function can be used for an HTML version of the page and an AJAX version of the page. The page callback function just needs to decide what content is to be returned and the delivery callback function will send it as an HTML page or an AJAX response, as appropriate.
In order for page callbacks to be reusable in different delivery formats, they should not issue any "print" or "echo" statements, but instead just return content.
See also
@see hook_menu()
See also
@see hook_menu_active_handler_alter()
See also
hook_page_delivery_callback_alter()
Parameters
$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.
$default_delivery_callback (Optional) If given, it is the name of a delivery function most likely to be appropriate for the page request as determined by the calling function (e.g., menu_execute_active_handler()). If not given, it is determined from the menu router information of the current page. In either case, modules have a final chance to alter which function is called.
Code
includes/common.inc, line 2803
<?php
function drupal_deliver_page($page_callback_result, $default_delivery_callback = NULL) {
if (!isset($default_delivery_callback) && ($router_item = menu_get_item())) {
drupal_alter('menu_active_handler', $router_item);
$default_delivery_callback = $router_item['delivery_callback'];
}
$delivery_callback = !empty($default_delivery_callback) ? $default_delivery_callback : 'drupal_deliver_html_page';
// Give modules a final chance to alter the delivery callback used. This is
// for modules that need to decide which delivery callback to use based on
// information made available during page callback execution and for pages
// without router items.
drupal_alter('page_delivery_callback', $delivery_callback);
if (function_exists($delivery_callback)) {
$delivery_callback($page_callback_result);
}
else {
// If a delivery callback is specified, but doesn't exist as a function,
// something is wrong, but don't print anything, since it's not known
// what format the response needs to be in.
watchdog('delivery callback not found', check_plain($delivery_callback) . ': ' . check_plain($_GET['q']), NULL, WATCHDOG_ERROR);
}
}
?>Login or register to post comments 