summaryrefslogtreecommitdiff
path: root/includes
diff options
context:
space:
mode:
Diffstat (limited to 'includes')
-rw-r--r--includes/common.inc78
-rw-r--r--includes/theme.inc92
2 files changed, 110 insertions, 60 deletions
diff --git a/includes/common.inc b/includes/common.inc
index e33a5c187..dd7e58c35 100644
--- a/includes/common.inc
+++ b/includes/common.inc
@@ -301,8 +301,7 @@ function drupal_get_destination() {
* Drupal will ensure that messages set by drupal_set_message() and other
* session data are written to the database before the user is redirected.
*
- * This function ends the request; use it rather than a print theme('page')
- * statement in your menu callback.
+ * This function ends the request; use it instead of a return in your menu callback.
*
* @param $path
* A Drupal path or a full URL.
@@ -379,19 +378,23 @@ function drupal_not_found() {
$path = drupal_get_normal_path(variable_get('site_404', ''));
if ($path && $path != $_GET['q']) {
- // Set the active item in case there are tabs to display, or other
- // dependencies on the path.
+ // Custom 404 handler. Set the active item in case there are tabs to
+ // display, or other dependencies on the path.
menu_set_active_item($path);
$return = menu_execute_active_handler($path);
}
if (empty($return) || $return == MENU_NOT_FOUND || $return == MENU_ACCESS_DENIED) {
+ // Standard 404 handler.
drupal_set_title(t('Page not found'));
$return = t('The requested page could not be found.');
}
+ $page = drupal_get_page($return);
// To conserve CPU and bandwidth, omit the blocks.
- print theme('page', $return, FALSE);
+ $page['#show_blocks'] = FALSE;
+
+ print drupal_render_page($page);
}
/**
@@ -408,17 +411,19 @@ function drupal_access_denied() {
$path = drupal_get_normal_path(variable_get('site_403', ''));
if ($path && $path != $_GET['q']) {
- // Set the active item in case there are tabs to display or other
- // dependencies on the path.
+ // Custom 403 handler. Set the active item in case there are tabs to
+ // display or other dependencies on the path.
menu_set_active_item($path);
$return = menu_execute_active_handler($path);
}
if (empty($return) || $return == MENU_NOT_FOUND || $return == MENU_ACCESS_DENIED) {
+ // Standard 403 handler.
drupal_set_title(t('Access denied'));
$return = t('You are not authorized to access this page.');
}
- print theme('page', $return);
+
+ print drupal_render_page($return);
}
/**
@@ -816,7 +821,10 @@ function _drupal_log_error($error, $fatal = FALSE) {
drupal_set_header($_SERVER['SERVER_PROTOCOL'] . ' Service unavailable');
drupal_set_title(t('Error'));
if (!defined('MAINTENANCE_MODE') && drupal_get_bootstrap_phase() == DRUPAL_BOOTSTRAP_FULL) {
- print theme('page', t('The website encountered an unexpected error. Please try again later.'), FALSE);
+ // To conserve CPU and bandwidth, omit the blocks.
+ $page = drupal_get_page(t('The website encountered an unexpected error. Please try again later.'));
+ $page['#show_blocks'] = FALSE;
+ print drupal_render_page($page);
}
else {
print theme('maintenance_page', t('The website encountered an unexpected error. Please try again later.'), FALSE);
@@ -3195,6 +3203,53 @@ function drupal_alter($type, &$data) {
}
/**
+ * Retrieve a $page element that is ready for decorating.
+ *
+ * Used by menu callbacks in order to populate the page with content
+ * and behavior (e.g. #show_blocks).
+ *
+ * @param $content
+ * A string or renderable array representing the body of the page.
+ * @return
+ * A $page element that should be decorated and then passed to drupal_render_page().
+ *
+ * @see drupal_render_page().
+ */
+function drupal_get_page($content = NULL) {
+ // Initialize page array with defaults. @see hook_elements() - 'page' element.
+ $page = _element_info('page');
+ $page['content'] = is_array($content) ? $content : array('main' => array('#markup' => $content));
+
+ return $page;
+}
+
+/**
+ * Renders the page, including all theming.
+ *
+ * @param $page
+ * A string or array representing the content of a page. The array consists of
+ * the following keys:
+ * - #type: Value is always 'page'. This pushes the theming through page.tpl.php (required).
+ * - content: A renderable array as built by the menu callback (required).
+ * - #show_blocks: A marker which suppresses left/right regions if FALSE (optional).
+ * - #show_messages: Suppress drupal_get_message() items. Used by Batch API (optional).
+ *
+ * @see hook_page_alter()
+ * @see drupal_get_page()
+ */
+function drupal_render_page($page) {
+ // Allow menu callbacks to return strings.
+ if (is_string($page)) {
+ $page = drupal_get_page($page);
+ }
+ // Modules alter the $page as needed. Blocks are populated into regions like
+ // 'left', 'footer', etc.
+ drupal_alter('page', $page);
+
+ return drupal_render($page);
+}
+
+/**
* Renders HTML given a structured array tree.
*
* Recursively iterates over each of the array elements, generating HTML code.
@@ -3366,7 +3421,7 @@ function drupal_common_theme() {
'arguments' => array('text' => NULL)
),
'page' => array(
- 'arguments' => array('content' => NULL, 'show_blocks' => TRUE, 'show_messages' => TRUE),
+ 'arguments' => array('page' => NULL),
'template' => 'page',
),
'maintenance_page' => array(
@@ -3425,6 +3480,9 @@ function drupal_common_theme() {
'item_list' => array(
'arguments' => array('items' => array(), 'title' => NULL, 'type' => 'ul', 'attributes' => NULL),
),
+ 'list' => array(
+ 'arguments' => array('elements' => NULL),
+ ),
'more_help_link' => array(
'arguments' => array('url' => NULL),
),
diff --git a/includes/theme.inc b/includes/theme.inc
index b71da908b..b77e35854 100644
--- a/includes/theme.inc
+++ b/includes/theme.inc
@@ -1569,6 +1569,25 @@ function theme_item_list($items = array(), $title = NULL, $type = 'ul', $attribu
}
/**
+ * Return a themed list of items from a drupal_render() style array.
+ *
+ * @param $elements
+ * An array consisting of the following keys:
+ * - #items: an array of items as expected by theme('item_list').
+ * - #title: a title which prints above the list.
+ * - #list_type: the type of list to return. Defaults to "ul".
+ * - #attributes: an array of attributes as expected by theme('item_list').
+ * @return
+ * A string containing the list output.
+ */
+function theme_list($elements) {
+ // Populate any missing array elements with their defaults.
+ $elements += _element_info('list');
+
+ return theme('item_list', $elements['#items'], $elements['#title'], $elements['#list_type'], $elements['#attributes']);
+}
+
+/**
* Returns code that emits the 'more help'-link.
*/
function theme_more_help_link($url) {
@@ -1631,30 +1650,6 @@ function theme_closure($main = 0) {
}
/**
- * Return a set of blocks available for the current user.
- *
- * @param $region
- * Which set of blocks to retrieve.
- * @return
- * A string containing the themed blocks for this region.
- */
-function theme_blocks($region) {
- $output = '';
-
- if ($list = block_list($region)) {
- foreach ($list as $key => $block) {
- // $key == <i>module</i>_<i>delta</i>
- $output .= theme('block', $block);
- }
- }
-
- // Add any content assigned to this region through drupal_set_content() calls.
- $output .= drupal_get_content($region);
-
- return $output;
-}
-
-/**
* Format a username.
*
* @param $object
@@ -1816,32 +1811,26 @@ function template_preprocess(&$variables, $hook) {
* Any changes to variables in this preprocessor should also be changed inside
* template_preprocess_maintenance_page() to keep all of them consistent.
*
- * The $variables array contains the following arguments:
- * - $content
- * - $show_blocks
+ * The $variables array contains two keys:
+ * - 'page': the fully decorated page.
+ * - 'content': the content of the page, already rendered.
*
+ * @see drupal_render_page
* @see page.tpl.php
*/
function template_preprocess_page(&$variables) {
- // Add favicon
- if (theme_get_setting('toggle_favicon')) {
- drupal_set_html_head('<link rel="shortcut icon" href="' . check_url(theme_get_setting('favicon')) . '" type="image/x-icon" />');
+ // Move some variables to the top level for themer convenience and template cleanliness.
+ $variables['show_blocks'] = $variables['page']['#show_blocks'];
+ $variables['show_messages'] = $variables['page']['#show_messages'];
+
+ // Render each region into top level variables.
+ foreach (system_region_list($GLOBALS['theme']) as $region_key => $region_name) {
+ $variables[$region_key] = empty($variables['page'][$region_key]) ? '' : drupal_render($variables['page'][$region_key]);
}
- global $theme;
- // Populate all block regions.
- $regions = system_region_list($theme);
- // Load all region content assigned via blocks.
- foreach (array_keys($regions) as $region) {
- // Prevent left and right regions from rendering blocks when 'show_blocks' == FALSE.
- if ($variables['show_blocks'] || ($region != 'left' && $region != 'right')) {
- $blocks = theme('blocks', $region);
- }
- else {
- $blocks = '';
- }
- // Assign region to a region variable.
- isset($variables[$region]) ? $variables[$region] .= $blocks : $variables[$region] = $blocks;
+ // Add favicon.
+ if (theme_get_setting('toggle_favicon')) {
+ drupal_set_html_head('<link rel="shortcut icon" href="' . check_url(theme_get_setting('favicon')) . '" type="image/x-icon" />');
}
// Set up layout variable.
@@ -1881,8 +1870,8 @@ function template_preprocess_page(&$variables) {
$variables['logo'] = theme_get_setting('logo');
$variables['messages'] = $variables['show_messages'] ? theme('status_messages') : '';
$variables['mission'] = isset($mission) ? $mission : '';
- $variables['main_menu'] = theme_get_setting('toggle_main_menu') ? menu_main_menu() : array();
- $variables['secondary_menu'] = theme_get_setting('toggle_secondary_menu') ? menu_secondary_menu() : array();
+ $variables['main_menu'] = theme_get_setting('toggle_main_menu') ? menu_main_menu() : array();
+ $variables['secondary_menu'] = theme_get_setting('toggle_secondary_menu') ? menu_secondary_menu() : array();
$variables['search_box'] = (theme_get_setting('toggle_search') ? drupal_get_form('search_theme_form') : '');
$variables['site_name'] = (theme_get_setting('toggle_name') ? variable_get('site_name', 'Drupal') : '');
$variables['site_slogan'] = (theme_get_setting('toggle_slogan') ? variable_get('site_slogan', '') : '');
@@ -1975,6 +1964,8 @@ function template_preprocess_page(&$variables) {
* @see node.tpl.php
*/
function template_preprocess_node(&$variables) {
+ $variables['teaser'] = $variables['elements']['#teaser'];
+ $variables['node'] = $variables['elements']['#node'];
$node = $variables['node'];
$variables['date'] = format_date($node->created);
@@ -1982,17 +1973,17 @@ function template_preprocess_node(&$variables) {
$variables['node_url'] = url('node/' . $node->nid);
$variables['title'] = check_plain($node->title);
$variables['page'] = (bool)menu_get_object();
-
+
if ($node->build_mode == NODE_BUILD_PREVIEW) {
unset($node->content['links']);
}
-
+
// Render taxonomy links separately.
$variables['terms'] = !empty($node->content['links']['terms']) ? drupal_render($node->content['links']['terms']) : '';
-
+
// Render all remaining node links.
$variables['links'] = !empty($node->content['links']) ? drupal_render($node->content['links']) : '';
-
+
// Render any comments.
$variables['comments'] = !empty($node->content['comments']) ? drupal_render($node->content['comments']) : '';
@@ -2035,6 +2026,7 @@ function template_preprocess_node(&$variables) {
*/
function template_preprocess_block(&$variables) {
static $block_counter = array();
+ $variables['block'] = $variables['block']['#block'];
// All blocks get an independent counter for each region.
if (!isset($block_counter[$variables['block']->region])) {
$block_counter[$variables['block']->region] = 1;