Skip to content

Instantly share code, notes, and snippets.

@sdboyer
Last active December 17, 2015 18:09
Show Gist options
  • Save sdboyer/5651305 to your computer and use it in GitHub Desktop.
Save sdboyer/5651305 to your computer and use it in GitHub Desktop.
notes on what to do with page preprocessing
<?php
function template_preprocess_page(&$variables) {
$language_interface = language(LANGUAGE_TYPE_INTERFACE); // DC
$site_config = config('system.site'); // DC
// Move some variables to the top level for themer convenience and template cleanliness.
$variables['show_messages'] = $variables['page']['#show_messages']; // Bad
foreach (system_region_list($GLOBALS['theme']) as $region_key => $region_name) {
if (!isset($variables['page'][$region_key])) {
$variables['page'][$region_key] = array();
}
}
// Set up layout variable.
$variables['layout'] = 'none'; // BAD
if (!empty($variables['page']['sidebar_first'])) {
$variables['layout'] = 'first'; // BAD
}
if (!empty($variables['page']['sidebar_second'])) {
$variables['layout'] = ($variables['layout'] == 'first') ? 'both' : 'second'; // BAD
}
$variables['base_path'] = base_path(); // Shouldn't be a variable *IN* the template
$variables['front_page'] = url(); // Gives you a link to the frontpage; this should go away given blocks that encapsulates any such links
$variables['feed_icons'] = drupal_get_feeds(); // wtf? put it in a block.
$variables['language'] = $language_interface; // should go somewhere else, a product of the bizarre passing of data to the html template
$variables['language']->dir = $language_interface->direction ? 'rtl' : 'ltr'; // ibid
$variables['logo'] = theme_get_setting('logo'); // should be in the block
$variables['main_menu'] = theme_get_setting('toggle_main_menu') ? menu_main_menu() : array(); // move to block
$variables['secondary_menu'] = theme_get_setting('toggle_secondary_menu') ? menu_secondary_menu() : array(); // move to block
$variables['action_links'] = menu_local_actions(); // move to a block
$variables['site_name'] = (theme_get_setting('toggle_name') ? check_plain($site_config->get('name')) : ''); // GOOD
$variables['site_slogan'] = (theme_get_setting('toggle_slogan') ? filter_xss_admin($site_config->get('slogan')) : ''); // move to a block
$variables['tabs'] = menu_local_tabs(); // move to a block
if ($node = menu_get_object()) {
$variables['node'] = $node; // this is shitty global-grabbing, but technically unlikely to break cache
}
// Populate the page template suggestions.
if ($suggestions = theme_get_suggestions(arg(), 'page')) {
$variables['theme_hook_suggestions'] = $suggestions; // bad, duplicates the pattern that pluggable layouts is itself trying to do
}
}
/**
* Process variables for page.tpl.php
*
* Perform final addition of variables before passing them into the template.
* To customize these variables, simply set them in an earlier step.
*
* @see template_preprocess_page()
* @see page.tpl.php
*/
function template_process_page(&$variables) {
if (!isset($variables['breadcrumb'])) {
// Build the breadcrumb last, so as to increase the chance of being able to
// re-use the cache of an already rendered menu containing the active link
// for the current page.
// @see menu_tree_page_data()
$variables['breadcrumb'] = theme('breadcrumb', array('breadcrumb' => drupal_get_breadcrumb())); // move to a block
}
if (!isset($variables['title'])) {
$variables['title'] = drupal_get_title(); // move to a block
}
// Generate messages last in order to capture as many as possible for the
// current page.
if (!isset($variables['messages'])) {
$variables['messages'] = $variables['show_messages'] ? theme('status_messages') : ''; // necessary until moved to block
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment