Viewing file: template.php (15.25 KB) -rw-rw-rw- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php // $Id: template.php,v 1.22 2008/05/14 14:39:20 johnalbin Exp $
/** * @file * File which contains theme overrides for the Zen theme. * * ABOUT * * The template.php file is one of the most useful files when creating or * modifying Drupal themes. You can add new regions for block content, modify or * override Drupal's theme functions, intercept or make additional variables * available to your theme, and create custom PHP logic. For more information, * please visit the Theme Developer's Guide on Drupal.org: * http://drupal.org/theme-guide * * NOTE ABOUT ZEN'S TEMPLATE.PHP FILE * * The base Zen theme is designed to be easily extended by its sub-themes. You * shouldn't modify this or any of the CSS or PHP files in the root zen/ folder. * See the online documentation for more information: * http://drupal.org/node/193318 */
/* * To make this file easier to read, we split up the code into managable parts. * Theme developers are likely to only be interested in functions that are in * this main template.php file. */
// Tabs and menu functions include_once 'template-menus.php';
/** * Return a themed breadcrumb trail. * * @param $breadcrumb * An array containing the breadcrumb links. * @return * A string containing the breadcrumb output. */ function zen_breadcrumb($breadcrumb) { $show_breadcrumb = theme_get_setting('zen_breadcrumb'); $show_breadcrumb_home = theme_get_setting('zen_breadcrumb_home'); $breadcrumb_separator = theme_get_setting('zen_breadcrumb_separator'); $trailing_separator = (theme_get_setting('zen_breadcrumb_trailing') || theme_get_setting('zen_breadcrumb_title')) ? $breadcrumb_separator : '';
// Determine if we are to display the breadcrumb if ($show_breadcrumb == 'yes' || $show_breadcrumb == 'admin' && arg(0) == 'admin') { if (!$show_breadcrumb_home) { // Optionally get rid of the homepage link array_shift($breadcrumb); } if (!empty($breadcrumb)) { // Return the breadcrumb with separators return '<div class="breadcrumb">' . implode($breadcrumb_separator, $breadcrumb) . "$trailing_separator</div>"; } } // Otherwise, return an empty string return ''; }
/* * CREATE OR MODIFY VARIABLES FOR YOUR THEME * * The most powerful functions available to themers are the * THEME_preprocess_HOOK() functions. They allow you to pass newly created * variables to different template (tpl.php) files in your theme. Or even unset * ones you don't want to use. * * It works by switching on the hook, or name of the theme function, such as: * - page * - node * - comment * - block * * By switching on this hook you can send different variables to page.tpl.php * file, node.tpl.php (and any other derivative node template file, like * node-forum.tpl.php), comment.tpl.php, and block.tpl.php. */
/** * Override or insert PHPTemplate variables into the page templates. * * @param $vars * A sequential array of variables to pass to the theme template. * @param $hook * The name of the theme function being called ("page" in this case.) */ function zen_preprocess_page(&$vars, $hook) { global $theme;
// These next lines add additional CSS files and redefine // the $css and $styles variables available to your page template if ($theme == 'zen') { // If we're in the main theme // Load the stylesheet for a liquid layout if (theme_get_setting('zen_layout') == 'border-politics-liquid') { drupal_add_css($vars['directory'] . '/layout-liquid.css', 'theme', 'all'); } // Or load the stylesheet for a fixed width layout else { drupal_add_css($vars['directory'] . '/layout-fixed.css', 'theme', 'all'); } } // Optionally add the block editing styles. if (theme_get_setting('zen_block_editing')) { drupal_add_css(path_to_zentheme() . '/block-editing.css', 'theme', 'all'); } // Optionally add the wireframes style. if (theme_get_setting('zen_wireframes')) { drupal_add_css(path_to_zentheme() . '/wireframes.css', 'theme', 'all'); } $vars['css'] = drupal_add_css(); $vars['styles'] = drupal_get_css();
// Allow sub-themes to have an ie.css file $vars['zentheme_directory'] = path_to_zentheme();
// Add an optional title to the end of the breadcrumb. if (theme_get_setting('zen_breadcrumb_title') && $vars['breadcrumb']) { $vars['breadcrumb'] = substr($vars['breadcrumb'], 0, -6) . $vars['title'] . '</div>'; }
// Don't display empty help from node_help(). if ($vars['help'] == "<div class=\"help\"><p></p>\n</div>") { $vars['help'] = ''; }
// Classes for body element. Allows advanced theming based on context // (home page, node of certain type, etc.) $body_classes = array($vars['body_classes']); if (!$vars['is_front']) { // Add unique classes for each page and website section $path = drupal_get_path_alias($_GET['q']); list($section, ) = explode('/', $path, 2); $body_classes[] = zen_id_safe('page-' . $path); $body_classes[] = zen_id_safe('section-' . $section); if (arg(0) == 'node') { if (arg(1) == 'add') { if ($section == 'node') { array_pop($body_classes); // Remove 'section-node' } $body_classes[] = 'section-node-add'; // Add 'section-node-add' } elseif (is_numeric(arg(1)) && (arg(2) == 'edit' || arg(2) == 'delete')) { if ($section == 'node') { array_pop($body_classes); // Remove 'section-node' } $body_classes[] = 'section-node-' . arg(2); // Add 'section-node-edit' or 'section-node-delete' } } } $vars['body_classes'] = implode(' ', $body_classes); // Concatenate with spaces }
/** * Override or insert PHPTemplate variables into the node templates. * * @param $vars * A sequential array of variables to pass to the theme template. * @param $hook * The name of the theme function being called ("node" in this case.) */ function zen_preprocess_node(&$vars, $hook) { global $user;
// Special classes for nodes $node_classes = array(); if ($vars['sticky']) { $node_classes[] = 'sticky'; } if (!$vars['node']->status) { $node_classes[] = 'node-unpublished'; $vars['unpublished'] = TRUE; } else { $vars['unpublished'] = FALSE; } if ($vars['node']->uid && $vars['node']->uid == $user->uid) { // Node is authored by current user $node_classes[] = 'node-mine'; } if ($vars['teaser']) { // Node is displayed as teaser $node_classes[] = 'node-teaser'; } // Class for node type: "node-type-page", "node-type-story", "node-type-my-custom-type", etc. $node_classes[] = 'node-type-' . $vars['node']->type; $vars['node_classes'] = implode(' ', $node_classes); // Concatenate with spaces }
/** * Override or insert PHPTemplate variables into the comment templates. * * @param $vars * A sequential array of variables to pass to the theme template. * @param $hook * The name of the theme function being called ("comment" in this case.) */ function zen_preprocess_comment(&$vars, $hook) { global $user;
// We load the node object that the current comment is attached to $node = node_load($vars['comment']->nid); // If the author of this comment is equal to the author of the node, we // set a variable so we can theme this comment uniquely. $vars['author_comment'] = $vars['comment']->uid == $node->uid ? TRUE : FALSE;
$comment_classes = array();
// Odd/even handling static $comment_odd = TRUE; $comment_classes[] = $comment_odd ? 'odd' : 'even'; $comment_odd = !$comment_odd;
if ($vars['comment']->status == COMMENT_NOT_PUBLISHED) { $comment_classes[] = 'comment-unpublished'; $vars['unpublished'] = TRUE; } else { $vars['unpublished'] = FALSE; } if ($vars['author_comment']) { // Comment is by the node author $comment_classes[] = 'comment-by-author'; } if ($vars['comment']->uid == 0) { // Comment is by an anonymous user $comment_classes[] = 'comment-by-anon'; } if ($user->uid && $vars['comment']->uid == $user->uid) { // Comment was posted by current user $comment_classes[] = 'comment-mine'; } $vars['comment_classes'] = implode(' ', $comment_classes);
// If comment subjects are disabled, don't display 'em if (variable_get('comment_subject_field', 1) == 0) { $vars['title'] = ''; } }
/** * Override or insert PHPTemplate variables into the block templates. * * @param $vars * A sequential array of variables to pass to the theme template. * @param $hook * The name of the theme function being called ("block" in this case.) */ function zen_preprocess_block(&$vars, $hook) { $block = $vars['block'];
// Special classes for blocks $block_classes = array(); $block_classes[] = 'block-' . $block->module; $block_classes[] = 'region-' . $vars['block_zebra']; $block_classes[] = $vars['zebra']; $block_classes[] = 'region-count-' . $vars['block_id']; $block_classes[] = 'count-' . $vars['id']; $vars['block_classes'] = implode(' ', $block_classes);
$vars['edit_links'] = ''; if (theme_get_setting('zen_block_editing') && user_access('administer blocks')) { // Display 'edit block' for custom blocks if ($block->module == 'block') { $edit_links[] = l('<span>' . t('edit block') . '</span>', 'admin/build/block/configure/' . $block->module . '/' . $block->delta, array( 'attributes' => array( 'title' => t('edit the content of this block'), 'class' => 'block-edit', ), 'query' => drupal_get_destination(), 'html' => TRUE, ) ); } // Display 'configure' for other blocks else { $edit_links[] = l('<span>' . t('configure') . '</span>', 'admin/build/block/configure/' . $block->module . '/' . $block->delta, array( 'attributes' => array( 'title' => t('configure this block'), 'class' => 'block-config', ), 'query' => drupal_get_destination(), 'html' => TRUE, ) ); }
// Display 'administer views' for views blocks if ($block->module == 'views' && user_access('administer views')) { $edit_links[] = l('<span>' . t('edit view') . '</span>', 'admin/build/views/' . $block->delta . '/edit', array( 'attributes' => array( 'title' => t('edit the view that defines this block'), 'class' => 'block-edit-view', ), 'query' => drupal_get_destination(), 'fragment' => 'edit-block', 'html' => TRUE, ) ); } // Display 'edit menu' for menu blocks elseif (($block->module == 'menu' || ($block->module == 'user' && $block->delta == 1)) && user_access('administer menu')) { $menu_name = ($block->module == 'user') ? 'navigation' : $block->delta; $edit_links[] = l('<span>' . t('edit menu') . '</span>', 'admin/build/menu-customize/' . $menu_name, array( 'attributes' => array( 'title' => t('edit the menu that defines this block'), 'class' => 'block-edit-menu', ), 'query' => drupal_get_destination(), 'html' => TRUE, ) ); } $vars['edit_links_array'] = $edit_links; $vars['edit_links'] = '<div class="edit">' . implode(' ', $edit_links) . '</div>'; } }
/** * Converts a string to a suitable html ID attribute. * * http://www.w3.org/TR/html4/struct/global.html#h-7.5.2 specifies what makes a * valid ID attribute in HTML. This function: * * - Ensure an ID starts with an alpha character by optionally adding an 'n'. * - Replaces any character except A-Z, numbers, and underscores with dashes. * - Converts entire string to lowercase. * * @param $string * The string * @return * The converted string */ function zen_id_safe($string) { // Replace with dashes anything that isn't A-Z, numbers, dashes, or underscores. $string = strtolower(preg_replace('/[^a-zA-Z0-9_-]+/', '-', $string)); // If the first character is not a-z, add 'n' in front. if (!ctype_lower($string{0})) { // Don't use ctype_alpha since its locale aware. $string = 'id' . $string; } return $string; }
/** * Return the path to the main zen theme directory. */ function path_to_zentheme() { static $theme_path; if (!isset($theme_path)) { global $theme; if ($theme == 'zen') { $theme_path = path_to_theme(); } else { $theme_path = drupal_get_path('theme', 'zen'); } } return $theme_path; }
/** * Implementation of HOOK_theme(). * * The Zen base theme uses this function as a work-around for a bug in Drupal * 6.0-6.2: #252430 (Allow BASETHEME_ prefix in preprocessor function names). * * Sub-themes Also use this function by calling it from their HOOK_theme() in * order to get around a design limitation in Drupal 6: #249532 (Allow subthemes * to have preprocess hooks without tpl files.) * * @param $existing * An array of existing implementations that may be used for override purposes. * @param $type * What 'type' is being processed. * @param $theme * The actual name of theme that is being being checked. * @param $path * The directory path of the theme or module, so that it doesn't need to be looked up. */ function zen_theme(&$existing, $type, $theme, $path) { // Each theme has two possible preprocess functions that can act on a hook. // This function applies to every hook. $functions[0] = $theme . '_preprocess'; // Inspect the preprocess functions for every hook in the theme registry. foreach ($existing AS $hook => &$value) { // Each theme has two possible preprocess functions that can act on a hook. // This function only applies to this hook. $functions[1] = $theme . '_preprocess_' . $hook; foreach ($functions AS $key => $function) { // Add any functions that are not already in the registry. if (function_exists($function) && !in_array($function, $existing[$hook]['preprocess functions'])) { // We add the preprocess function to the end of the existing list. $existing[$hook]['preprocess functions'][] = $function; } } }
// Since we are rebuilding the theme registry and the theme settings' default // values may have changed, make sure they are saved in the database properly. zen_settings_init($theme);
// Since we modify the $existing cache directly, return nothing. return array(); }
/** * Read the theme settings' default values from the .info and save them into the database. * * @param $theme * The actual name of theme that is being being checked. */ function zen_settings_init($theme) { $themes = list_themes();
// Get the default values from the .info file. $defaults = $themes[$theme]->info['settings'];
// Get the theme settings saved in the database. $settings = theme_get_settings($theme); // Don't save the toggle_node_info_ variables. if (module_exists('node')) { foreach (node_get_types() as $type => $name) { unset($settings['toggle_node_info_' . $type]); } } // Save default theme settings. variable_set( str_replace('/', '_', 'theme_' . $theme . '_settings'), array_merge($defaults, $settings) ); // Force refresh of Drupal internals. theme_get_setting('', TRUE); }
/* * In addition to initializing the theme settings during HOOK_theme(), init them * when viewing/resetting the admin/build/themes/settings/THEME forms. */ if (arg(0) == 'admin' && arg(2) == 'themes' && arg(4)) { global $theme_key; zen_settings_init($theme_key); }
|