6 * Contains theme override functions and preprocess functions for the Zen theme.
8 * IMPORTANT WARNING: DO NOT MODIFY THIS FILE.
10 * The base Zen theme is designed to be easily extended by its sub-themes. You
11 * shouldn't modify this or any of the CSS or PHP files in the root zen/ folder.
12 * See the online documentation for more information:
13 * http://drupal.org/node/193318
16 // Auto-rebuild the theme registry during theme development.
17 if (theme_get_setting('zen_rebuild_registry')) {
18 drupal_rebuild_theme_registry();
23 * Implements HOOK_theme().
25 function zen_theme(&$existing, $type, $theme, $path) {
26 // When #341140 is fixed, replace _zen_path() with drupal_get_path().
27 include_once
'./' .
_zen_path() .
'/zen-internals/template.theme-registry.inc';
28 return _zen_theme($existing, $type, $theme, $path);
32 * Return a themed breadcrumb trail.
35 * An array containing the breadcrumb links.
37 * A string containing the breadcrumb output.
39 function zen_breadcrumb($breadcrumb) {
40 // Determine if we are to display the breadcrumb.
41 $show_breadcrumb = theme_get_setting('zen_breadcrumb');
42 if ($show_breadcrumb == 'yes' || $show_breadcrumb == 'admin' && arg(0) == 'admin') {
44 // Optionally get rid of the homepage link.
45 $show_breadcrumb_home = theme_get_setting('zen_breadcrumb_home');
46 if (!$show_breadcrumb_home) {
47 array_shift($breadcrumb);
50 // Return the breadcrumb with separators.
51 if (!empty($breadcrumb)) {
52 $breadcrumb_separator = theme_get_setting('zen_breadcrumb_separator');
53 $trailing_separator = $title = '';
54 if (theme_get_setting('zen_breadcrumb_title')) {
55 $trailing_separator = $breadcrumb_separator;
56 $title = drupal_get_title();
58 elseif (theme_get_setting('zen_breadcrumb_trailing')) {
59 $trailing_separator = $breadcrumb_separator;
61 return '<div class="breadcrumb">' .
implode($breadcrumb_separator, $breadcrumb) .
"$trailing_separator$title</div>";
64 // Otherwise, return an empty string.
69 * Implements theme_menu_item_link()
71 function zen_menu_item_link($link) {
72 if (empty($link['localized_options'])) {
73 $link['localized_options'] = array();
76 // If an item is a LOCAL TASK, render it as a tab
77 if ($link['type'] & MENU_IS_LOCAL_TASK
) {
78 $link['title'] = '<span class="tab">' .
check_plain($link['title']) .
'</span>';
79 $link['localized_options']['html'] = TRUE
;
82 return l($link['title'], $link['href'], $link['localized_options']);
86 * Duplicate of theme_menu_local_tasks() but adds clear-block to tabs.
88 function zen_menu_local_tasks() {
91 if ($primary = menu_primary_local_tasks()) {
92 $output .
= '<ul class="tabs primary clear-block">' .
$primary .
'</ul>';
94 if ($secondary = menu_secondary_local_tasks()) {
95 $output .
= '<ul class="tabs secondary clear-block">' .
$secondary .
'</ul>';
102 * Return a set of blocks available for the current user.
105 * Which set of blocks to retrieve.
106 * @param $show_blocks
107 * A boolean to determine whether to render sidebar regions or not. Should be
108 * the same value as passed to theme_page.
110 * A string containing the themed blocks for this region.
112 * @see zen_show_blocks_discovery()
114 function zen_blocks($region, $show_blocks = NULL
) {
115 // Since Drupal 6 doesn't pass $show_blocks to theme_blocks, we manually call
116 // theme('blocks', NULL, $show_blocks) so that this function can remember the
117 // value on later calls.
118 static
$render_sidebars = TRUE
;
119 if (!is_null($show_blocks)) {
120 $render_sidebars = $show_blocks;
123 // If zen_blocks was called with a NULL region, its likely we were just
124 // setting the $render_sidebars static variable.
128 // If $renders_sidebars is FALSE, don't render any region whose name begins
130 if (($render_sidebars || (strpos($region, 'sidebar_') !== 0)) && ($list = block_list($region))) {
131 foreach ($list as
$key => $block) {
132 // $key == module_delta
133 $output .
= theme('block', $block);
137 // Add any content assigned to this region through drupal_set_content() calls.
138 $output .
= drupal_get_content($region);
140 $elements['#children'] = $output;
141 $elements['#region'] = $region;
143 return $output ?
theme('region', $elements) : '';
148 * Examine the $show_blocks variable before template_preprocess_page() is called.
151 * An array of variables to pass to the page template.
155 function zen_show_blocks_discovery(&$vars) {
156 if ($vars['show_blocks'] == FALSE
) {
157 // Allow zen_blocks() to statically cache the $show_blocks variable. A TRUE
158 // value is assumed, so we only need to override when $show_blocks is FALSE.
159 theme('blocks', NULL
, FALSE
);
164 * Override or insert variables into templates before other preprocess functions have run.
167 * An array of variables to pass to the theme template.
169 * The name of the template being rendered.
171 function zen_preprocess(&$vars, $hook) {
172 // In D6, the page.tpl uses a different variable name to hold the classes.
173 $key = ($hook == 'page' || $hook == 'maintenance_page') ?
'body_classes' : 'classes';
175 // Create a D7-standard classes_array variable.
176 if (array_key_exists($key, $vars)) {
177 // Views (and possibly other modules) have templates with a $classes
178 // variable that isn't a string, so we leave those variables alone.
179 if (is_string($vars[$key])) {
180 $vars['classes_array'] = explode(' ', $vars[$key]);
185 $vars['classes_array'] = array($hook);
190 * Override or insert variables into the page templates.
193 * An array of variables to pass to the theme template.
195 * The name of the template being rendered ("page" in this case.)
197 function zen_preprocess_page(&$vars, $hook) {
198 // If the user is silly and enables Zen as the theme, add some styles.
199 if ($GLOBALS['theme'] == 'zen') {
200 include_once
'./' .
_zen_path() .
'/zen-internals/template.zen.inc';
201 _zen_preprocess_page($vars, $hook);
203 // Add conditional stylesheets.
204 elseif (!module_exists('conditional_styles')) {
205 $vars['styles'] .
= $vars['conditional_styles'] = variable_get('conditional_styles_' .
$GLOBALS['theme'], '');
208 // Classes for body element. Allows advanced theming based on context
209 // (home page, node of certain type, etc.)
210 // Remove the mostly useless page-ARG0 class.
211 if ($index = array_search(preg_replace('![^abcdefghijklmnopqrstuvwxyz0-9-_]+!s', '', 'page-'.
drupal_strtolower(arg(0))), $vars['classes_array'])) {
212 unset($vars['classes_array'][$index]);
214 if (!$vars['is_front']) {
215 // Add unique class for each page.
216 $path = drupal_get_path_alias($_GET['q']);
217 $vars['classes_array'][] = zen_id_safe('page-' .
$path);
218 // Add unique class for each website section.
219 list($section, ) = explode('/', $path, 2);
220 if (arg(0) == 'node') {
221 if (arg(1) == 'add') {
222 $section = 'node-add';
224 elseif (is_numeric(arg(1)) && (arg(2) == 'edit' || arg(2) == 'delete')) {
225 $section = 'node-' .
arg(2);
228 $vars['classes_array'][] = zen_id_safe('section-' .
$section);
230 if (theme_get_setting('zen_wireframes')) {
231 $vars['classes_array'][] = 'with-wireframes'; // Optionally add the wireframes style.
233 // We need to re-do the $layout and body classes because
234 // template_preprocess_page() assumes sidebars are named 'left' and 'right'.
235 $vars['layout'] = 'none';
236 if (!empty($vars['sidebar_first'])) {
237 $vars['layout'] = 'first';
239 if (!empty($vars['sidebar_second'])) {
240 $vars['layout'] = ($vars['layout'] == 'first') ?
'both' : 'second';
242 // If the layout is 'none', then template_preprocess_page() will already have
243 // set a 'no-sidebars' class since it won't find a 'left' or 'right' sidebar.
244 if ($vars['layout'] != 'none') {
245 // Remove the incorrect 'no-sidebars' class.
246 if ($index = array_search('no-sidebars', $vars['classes_array'])) {
247 unset($vars['classes_array'][$index]);
249 // Set the proper layout body classes.
250 if ($vars['layout'] == 'both') {
251 $vars['classes_array'][] = 'two-sidebars';
254 $vars['classes_array'][] = 'one-sidebar';
255 $vars['classes_array'][] = 'sidebar-' .
$vars['layout'];
261 * Override or insert variables into the maintenance page template.
264 * An array of variables to pass to the theme template.
266 * The name of the template being rendered ("maintenance_page" in this case.)
268 function zen_preprocess_maintenance_page(&$vars, $hook) {
269 // If Zen is the maintenance theme, add some styles.
270 if ($GLOBALS['theme'] == 'zen') {
271 include_once
'./' .
_zen_path() .
'/zen-internals/template.zen.inc';
272 _zen_preprocess_page($vars, $hook);
274 // Add conditional stylesheets.
275 elseif (!module_exists('conditional_styles')) {
276 $vars['styles'] .
= $vars['conditional_styles'] = variable_get('conditional_styles_' .
$GLOBALS['theme'], '');
279 // Classes for body element. Allows advanced theming based on context
280 // (home page, node of certain type, etc.)
281 $vars['body_classes_array'] = explode(' ', $vars['body_classes']);
285 * Override or insert variables into the node templates.
288 * An array of variables to pass to the theme template.
290 * The name of the template being rendered ("node" in this case.)
292 function zen_preprocess_node(&$vars, $hook) {
293 // Create the build_mode variable.
294 switch ($vars['node']->build_mode
) {
295 case NODE_BUILD_NORMAL
:
296 $vars['build_mode'] = $vars['teaser'] ?
'teaser' : 'full';
298 case NODE_BUILD_PREVIEW
:
299 $vars['build_mode'] = 'preview';
301 case NODE_BUILD_SEARCH_INDEX
:
302 $vars['build_mode'] = 'search_index';
304 case NODE_BUILD_SEARCH_RESULT
:
305 $vars['build_mode'] = 'search_result';
308 $vars['build_mode'] = 'rss';
310 case NODE_BUILD_PRINT
:
311 $vars['build_mode'] = 'print';
315 // Create the user_picture variable.
316 $vars['user_picture'] = $vars['picture'];
318 // Create the Drupal 7 $display_submitted variable.
319 $vars['display_submitted'] = theme_get_setting('toggle_node_info_' .
$vars['node']->type
);
321 // Special classes for nodes.
322 // Class for node type: "node-type-page", "node-type-story", "node-type-my-custom-type", etc.
323 $vars['classes_array'][] = zen_id_safe('node-type-' .
$vars['type']);
324 if ($vars['promote']) {
325 $vars['classes_array'][] = 'node-promoted';
327 if ($vars['sticky']) {
328 $vars['classes_array'][] = 'node-sticky';
330 if (!$vars['status']) {
331 $vars['classes_array'][] = 'node-unpublished';
332 $vars['unpublished'] = TRUE
;
335 $vars['unpublished'] = FALSE
;
337 if ($vars['uid'] && $vars['uid'] == $GLOBALS['user']->uid
) {
338 $vars['classes_array'][] = 'node-by-viewer'; // Node is authored by current user.
340 if ($vars['teaser']) {
341 $vars['classes_array'][] = 'node-teaser'; // Node is displayed as teaser.
343 if (isset($vars['preview'])) {
344 $vars['classes_array'][] = 'node-preview';
349 * Override or insert variables into the comment templates.
352 * An array of variables to pass to the theme template.
354 * The name of the template being rendered ("comment" in this case.)
356 function zen_preprocess_comment(&$vars, $hook) {
357 include_once
'./' .
_zen_path() .
'/zen-internals/template.comment.inc';
358 _zen_preprocess_comment($vars, $hook);
362 * Preprocess variables for region.tpl.php
364 * Prepare the values passed to the theme_region function to be passed into a
365 * pluggable template engine. Uses the region name to generate a template file
366 * suggestions. If none are found, the default region.tpl.php is used.
368 * @see region.tpl.php
370 function zen_preprocess_region(&$vars, $hook) {
371 // Create the $content variable that templates expect.
372 $vars['content'] = $vars['elements']['#children'];
373 $vars['region'] = $vars['elements']['#region'];
375 // Setup the default classes.
376 $region = 'region-' .
str_replace('_', '-', $vars['region']);
377 $vars['classes_array'] = array('region', $region);
379 // Sidebar regions get a common template suggestion a couple extra classes.
380 if (strpos($vars['region'], 'sidebar_') === 0) {
381 $vars['template_files'][] = 'region-sidebar';
382 $vars['classes_array'][] = 'column';
383 $vars['classes_array'][] = 'sidebar';
386 // Setup the default template suggestion.
387 $vars['template_files'][] = $region;
391 * Override or insert variables into the block templates.
394 * An array of variables to pass to the theme template.
396 * The name of the template being rendered ("block" in this case.)
398 function zen_preprocess_block(&$vars, $hook) {
399 $block = $vars['block'];
401 // Drupal 7 uses a $content variable instead of $block->content.
402 $vars['content'] = $block->content
;
403 // Drupal 7 should use a $title variable instead of $block->subject.
404 $vars['title'] = $block->subject
;
406 // Special classes for blocks.
407 $vars['classes_array'][] = 'block-' .
$block->module
;
408 $vars['classes_array'][] = 'region-' .
$vars['block_zebra'];
409 $vars['classes_array'][] = $vars['zebra'];
410 $vars['classes_array'][] = 'region-count-' .
$vars['block_id'];
411 $vars['classes_array'][] = 'count-' .
$vars['id'];
413 $vars['edit_links_array'] = array();
414 if (theme_get_setting('zen_block_editing') && user_access('administer blocks')) {
415 include_once
'./' .
_zen_path() .
'/zen-internals/template.block-editing.inc';
416 zen_preprocess_block_editing($vars, $hook);
417 $vars['classes_array'][] = 'with-block-editing';
422 * Override or insert variables into templates after preprocess functions have run.
425 * An array of variables to pass to the theme template.
427 * The name of the template being rendered.
429 function zen_process(&$vars, $hook) {
430 if (array_key_exists('classes_array', $vars)) {
431 $vars['classes'] = implode(' ', $vars['classes_array']);
436 * Override or insert variables into the block templates after preprocess functions have run.
439 * An array of variables to pass to the theme template.
441 * The name of the template being rendered ("block" in this case.)
443 function zen_process_block(&$vars, $hook) {
444 $vars['edit_links'] = !empty($vars['edit_links_array']) ?
'<div class="edit">' .
implode(' ', $vars['edit_links_array']) .
'</div>' : '';
448 * Converts a string to a suitable html ID attribute.
450 * http://www.w3.org/TR/html4/struct/global.html#h-7.5.2 specifies what makes a
451 * valid ID attribute in HTML. This function:
453 * - Ensure an ID starts with an alpha character by optionally adding an 'id'.
454 * - Replaces any character except alphanumeric characters with dashes.
455 * - Converts entire string to lowercase.
460 * The converted string
462 function zen_id_safe($string) {
463 // Replace with dashes anything that isn't A-Z, numbers, dashes, or underscores.
464 $string = strtolower(preg_replace('/[^a-zA-Z0-9-]+/', '-', $string));
465 // If the first character is not a-z, add 'id' in front.
466 if (!ctype_lower($string{0})) { // Don't use ctype_alpha since its locale aware.
467 $string = 'id' .
$string;
473 * Returns the path to the Zen theme.
475 * drupal_get_filename() is broken; see #341140. When that is fixed in Drupal 6,
476 * replace _zen_path() with drupal_get_path('theme', 'zen').
478 function _zen_path() {
479 static
$path = FALSE
;
481 $matches = drupal_system_listing('zen\.info$', 'themes', 'name', 0);
482 if (!empty($matches['zen']->filename
)) {
483 $path = dirname($matches['zen']->filename
);