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 if ($title = drupal_get_title()) {
56 $trailing_separator = $breadcrumb_separator;
59 elseif (theme_get_setting('zen_breadcrumb_trailing')) {
60 $trailing_separator = $breadcrumb_separator;
62 return '<div class="breadcrumb">' .
implode($breadcrumb_separator, $breadcrumb) .
"$trailing_separator$title</div>";
65 // Otherwise, return an empty string.
70 * Implements theme_menu_item_link()
72 function zen_menu_item_link($link) {
73 if (empty($link['localized_options'])) {
74 $link['localized_options'] = array();
77 // If an item is a LOCAL TASK, render it as a tab
78 if ($link['type'] & MENU_IS_LOCAL_TASK
) {
79 $link['title'] = '<span class="tab">' .
check_plain($link['title']) .
'</span>';
80 $link['localized_options']['html'] = TRUE
;
83 return l($link['title'], $link['href'], $link['localized_options']);
87 * Duplicate of theme_menu_local_tasks() but adds clearfix to tabs.
89 function zen_menu_local_tasks() {
92 // CTools requires a different set of local task functions.
93 if (module_exists('ctools')) {
94 $primary = ctools_menu_primary_local_tasks();
95 $secondary = ctools_menu_secondary_local_tasks();
98 $primary = menu_primary_local_tasks();
99 $secondary = menu_secondary_local_tasks();
103 $output .
= '<ul class="tabs primary clearfix">' .
$primary .
'</ul>';
106 $output .
= '<ul class="tabs secondary clearfix">' .
$secondary .
'</ul>';
113 * Return a set of blocks available for the current user.
116 * Which set of blocks to retrieve.
117 * @param $show_blocks
118 * A boolean to determine whether to render sidebar regions or not. Should be
119 * the same value as passed to theme_page.
121 * A string containing the themed blocks for this region.
123 * @see zen_show_blocks_discovery()
125 function zen_blocks($region, $show_blocks = NULL
) {
126 // Since Drupal 6 doesn't pass $show_blocks to theme_blocks, we manually call
127 // theme('blocks', NULL, $show_blocks) so that this function can remember the
128 // value on later calls.
129 static
$render_sidebars = TRUE
;
130 if (!is_null($show_blocks)) {
131 $render_sidebars = $show_blocks;
134 // If zen_blocks was called with a NULL region, its likely we were just
135 // setting the $render_sidebars static variable.
139 // If $renders_sidebars is FALSE, don't render any region whose name begins
141 if (($render_sidebars || (strpos($region, 'sidebar_') !== 0)) && ($list = block_list($region))) {
142 foreach ($list as
$key => $block) {
143 // $key == module_delta
144 $output .
= theme('block', $block);
148 // Add any content assigned to this region through drupal_set_content() calls.
149 $output .
= drupal_get_content($region);
151 $elements['#children'] = $output;
152 $elements['#region'] = $region;
154 return $output ?
theme('region', $elements) : '';
159 * Examine the $show_blocks variable before template_preprocess_page() is called.
162 * An array of variables to pass to the page template.
166 function zen_show_blocks_discovery(&$vars) {
167 if ($vars['show_blocks'] == FALSE
) {
168 // Allow zen_blocks() to statically cache the $show_blocks variable. A TRUE
169 // value is assumed, so we only need to override when $show_blocks is FALSE.
170 theme('blocks', NULL
, FALSE
);
175 * Override or insert variables into templates before other preprocess functions have run.
178 * An array of variables to pass to the theme template.
180 * The name of the template being rendered.
182 function zen_preprocess(&$vars, $hook) {
183 // In D6, the page.tpl uses a different variable name to hold the classes.
184 $key = ($hook == 'page' || $hook == 'maintenance_page') ?
'body_classes' : 'classes';
186 // Create a D7-standard classes_array variable.
187 if (array_key_exists($key, $vars)) {
188 // Views (and possibly other modules) have templates with a $classes
189 // variable that isn't a string, so we leave those variables alone.
190 if (is_string($vars[$key])) {
191 $vars['classes_array'] = explode(' ', $vars[$key]);
196 $vars['classes_array'] = array($hook);
198 // Add support for Skinr
199 if (!empty($vars['skinr']) && array_key_exists('classes_array', $vars)) {
200 $vars['classes_array'][] = $vars['skinr'];
205 * Override or insert variables into the page templates.
208 * An array of variables to pass to the theme template.
210 * The name of the template being rendered ("page" in this case.)
212 function zen_preprocess_page(&$vars, $hook) {
213 // If the user is silly and enables Zen as the theme, add some styles.
214 if ($GLOBALS['theme'] == 'zen') {
215 include_once
'./' .
_zen_path() .
'/zen-internals/template.zen.inc';
216 _zen_preprocess_page($vars, $hook);
218 // Add conditional stylesheets.
219 elseif (!module_exists('conditional_styles')) {
220 $vars['styles'] .
= $vars['conditional_styles'] = variable_get('conditional_styles_' .
$GLOBALS['theme'], '');
223 // Classes for body element. Allows advanced theming based on context
224 // (home page, node of certain type, etc.)
225 // Remove the mostly useless page-ARG0 class.
226 if ($index = array_search(preg_replace('![^abcdefghijklmnopqrstuvwxyz0-9-_]+!s', '', 'page-'.
drupal_strtolower(arg(0))), $vars['classes_array'])) {
227 unset($vars['classes_array'][$index]);
229 if (!$vars['is_front']) {
230 // Add unique class for each page.
231 $path = drupal_get_path_alias($_GET['q']);
232 $vars['classes_array'][] = drupal_html_class('page-' .
$path);
233 // Add unique class for each website section.
234 list($section, ) = explode('/', $path, 2);
235 if (arg(0) == 'node') {
236 if (arg(1) == 'add') {
237 $section = 'node-add';
239 elseif (is_numeric(arg(1)) && (arg(2) == 'edit' || arg(2) == 'delete')) {
240 $section = 'node-' .
arg(2);
243 $vars['classes_array'][] = drupal_html_class('section-' .
$section);
245 if (theme_get_setting('zen_wireframes')) {
246 $vars['classes_array'][] = 'with-wireframes'; // Optionally add the wireframes style.
248 // We need to re-do the $layout and body classes because
249 // template_preprocess_page() assumes sidebars are named 'left' and 'right'.
250 $vars['layout'] = 'none';
251 if (!empty($vars['sidebar_first'])) {
252 $vars['layout'] = 'first';
254 if (!empty($vars['sidebar_second'])) {
255 $vars['layout'] = ($vars['layout'] == 'first') ?
'both' : 'second';
257 // If the layout is 'none', then template_preprocess_page() will already have
258 // set a 'no-sidebars' class since it won't find a 'left' or 'right' sidebar.
259 if ($vars['layout'] != 'none') {
260 // Remove the incorrect 'no-sidebars' class.
261 if ($index = array_search('no-sidebars', $vars['classes_array'])) {
262 unset($vars['classes_array'][$index]);
264 // Set the proper layout body classes.
265 if ($vars['layout'] == 'both') {
266 $vars['classes_array'][] = 'two-sidebars';
269 $vars['classes_array'][] = 'one-sidebar';
270 $vars['classes_array'][] = 'sidebar-' .
$vars['layout'];
276 * Override or insert variables into the maintenance page template.
279 * An array of variables to pass to the theme template.
281 * The name of the template being rendered ("maintenance_page" in this case.)
283 function zen_preprocess_maintenance_page(&$vars, $hook) {
284 // If Zen is the maintenance theme, add some styles.
285 if ($GLOBALS['theme'] == 'zen') {
286 include_once
'./' .
_zen_path() .
'/zen-internals/template.zen.inc';
287 _zen_preprocess_page($vars, $hook);
289 // Add conditional stylesheets.
290 elseif (!module_exists('conditional_styles')) {
291 $vars['styles'] .
= $vars['conditional_styles'] = variable_get('conditional_styles_' .
$GLOBALS['theme'], '');
294 // Classes for body element. Allows advanced theming based on context
295 // (home page, node of certain type, etc.)
296 $vars['body_classes_array'] = explode(' ', $vars['body_classes']);
300 * Override or insert variables into the node templates.
303 * An array of variables to pass to the theme template.
305 * The name of the template being rendered ("node" in this case.)
307 function zen_preprocess_node(&$vars, $hook) {
308 // Create the build_mode variable.
309 switch ($vars['node']->build_mode
) {
310 case NODE_BUILD_NORMAL
:
311 $vars['build_mode'] = $vars['teaser'] ?
'teaser' : 'full';
313 case NODE_BUILD_PREVIEW
:
314 $vars['build_mode'] = 'preview';
316 case NODE_BUILD_SEARCH_INDEX
:
317 $vars['build_mode'] = 'search_index';
319 case NODE_BUILD_SEARCH_RESULT
:
320 $vars['build_mode'] = 'search_result';
323 $vars['build_mode'] = 'rss';
325 case NODE_BUILD_PRINT
:
326 $vars['build_mode'] = 'print';
330 // Create the user_picture variable.
331 $vars['user_picture'] = $vars['picture'];
333 // Create the Drupal 7 $display_submitted variable.
334 $vars['display_submitted'] = theme_get_setting('toggle_node_info_' .
$vars['node']->type
);
336 // Special classes for nodes.
337 // Class for node type: "node-type-page", "node-type-story", "node-type-my-custom-type", etc.
338 $vars['classes_array'][] = drupal_html_class('node-type-' .
$vars['type']);
339 if ($vars['promote']) {
340 $vars['classes_array'][] = 'node-promoted';
342 if ($vars['sticky']) {
343 $vars['classes_array'][] = 'node-sticky';
345 if (!$vars['status']) {
346 $vars['classes_array'][] = 'node-unpublished';
347 $vars['unpublished'] = TRUE
;
350 $vars['unpublished'] = FALSE
;
352 if ($vars['uid'] && $vars['uid'] == $GLOBALS['user']->uid
) {
353 $vars['classes_array'][] = 'node-by-viewer'; // Node is authored by current user.
355 if ($vars['teaser']) {
356 $vars['classes_array'][] = 'node-teaser'; // Node is displayed as teaser.
358 if (isset($vars['preview'])) {
359 $vars['classes_array'][] = 'node-preview';
364 * Override or insert variables into the comment templates.
367 * An array of variables to pass to the theme template.
369 * The name of the template being rendered ("comment" in this case.)
371 function zen_preprocess_comment(&$vars, $hook) {
372 include_once
'./' .
_zen_path() .
'/zen-internals/template.comment.inc';
373 _zen_preprocess_comment($vars, $hook);
377 * Preprocess variables for region.tpl.php
379 * Prepare the values passed to the theme_region function to be passed into a
380 * pluggable template engine. Uses the region name to generate a template file
381 * suggestions. If none are found, the default region.tpl.php is used.
383 * @see region.tpl.php
385 function zen_preprocess_region(&$vars, $hook) {
386 // Create the $content variable that templates expect.
387 $vars['content'] = $vars['elements']['#children'];
388 $vars['region'] = $vars['elements']['#region'];
390 // Setup the default classes.
391 $region = 'region-' .
str_replace('_', '-', $vars['region']);
392 $vars['classes_array'] = array('region', $region);
394 // Sidebar regions get a common template suggestion a couple extra classes.
395 if (strpos($vars['region'], 'sidebar_') === 0) {
396 $vars['template_files'][] = 'region-sidebar';
397 $vars['classes_array'][] = 'column';
398 $vars['classes_array'][] = 'sidebar';
401 // Setup the default template suggestion.
402 $vars['template_files'][] = $region;
406 * Override or insert variables into the block templates.
409 * An array of variables to pass to the theme template.
411 * The name of the template being rendered ("block" in this case.)
413 function zen_preprocess_block(&$vars, $hook) {
414 $block = $vars['block'];
416 // Drupal 7 uses a $content variable instead of $block->content.
417 $vars['content'] = $block->content
;
418 // Drupal 7 should use a $title variable instead of $block->subject.
419 $vars['title'] = $block->subject
;
421 // Special classes for blocks.
422 $vars['classes_array'][] = 'block-' .
$block->module
;
423 $vars['classes_array'][] = 'region-' .
$vars['block_zebra'];
424 $vars['classes_array'][] = $vars['zebra'];
425 $vars['classes_array'][] = 'region-count-' .
$vars['block_id'];
426 $vars['classes_array'][] = 'count-' .
$vars['id'];
428 $vars['edit_links_array'] = array();
429 if (theme_get_setting('zen_block_editing') && user_access('administer blocks')) {
430 include_once
'./' .
_zen_path() .
'/zen-internals/template.block-editing.inc';
431 zen_preprocess_block_editing($vars, $hook);
432 $vars['classes_array'][] = 'with-block-editing';
437 * Override or insert variables into the views-view templates.
440 * An array of variables to pass to the theme template.
442 * The name of the template being rendered ("views-view" in this case.)
444 function zen_preprocess_views_view(&$vars, $hook) {
445 // Add the default Views classes.
446 $vars['classes_array'][0] = 'view'; // Replace "views-view".
447 $vars['classes_array'][] = 'view-' .
$vars['css_name'];
448 $vars['classes_array'][] = 'view-id-' .
$vars['name'];
449 $vars['classes_array'][] = 'view-display-id-' .
$vars['display_id'];
450 $vars['classes_array'][] = 'view-dom-id-' .
$vars['dom_id'];
454 * Override or insert variables into templates after preprocess functions have run.
457 * An array of variables to pass to the theme template.
459 * The name of the template being rendered.
461 function zen_process(&$vars, $hook) {
462 if (array_key_exists('classes_array', $vars)) {
463 $vars['classes'] = implode(' ', $vars['classes_array']);
468 * Override or insert variables into the block templates after preprocess functions have run.
471 * An array of variables to pass to the theme template.
473 * The name of the template being rendered ("block" in this case.)
475 function zen_process_block(&$vars, $hook) {
476 $vars['edit_links'] = !empty($vars['edit_links_array']) ?
'<div class="edit">' .
implode(' ', $vars['edit_links_array']) .
'</div>' : '';
479 if (!function_exists('drupal_html_class')) {
481 * Prepare a string for use as a valid class name.
483 * Do not pass one string containing multiple classes as they will be
484 * incorrectly concatenated with dashes, i.e. "one two" will become "one-two".
487 * The class name to clean.
489 * The cleaned class name.
491 function drupal_html_class($class) {
492 // By default, we filter using Drupal's coding standards.
493 $class = strtr(drupal_strtolower($class), array(' ' => '-', '_' => '-', '/' => '-', '[' => '-', ']' => ''));
495 // http://www.w3.org/TR/CSS21/syndata.html#characters shows the syntax for valid
496 // CSS identifiers (including element names, classes, and IDs in selectors.)
498 // Valid characters in a CSS identifier are:
499 // - the hyphen (U+002D)
500 // - a-z (U+0030 - U+0039)
501 // - A-Z (U+0041 - U+005A)
502 // - the underscore (U+005F)
503 // - 0-9 (U+0061 - U+007A)
504 // - ISO 10646 characters U+00A1 and higher
505 // We strip out any character not in the above list.
506 $class = preg_replace('/[^\x{002D}\x{0030}-\x{0039}\x{0041}-\x{005A}\x{005F}\x{0061}-\x{007A}\x{00A1}-\x{FFFF}]/u', '', $class);
510 } /* End of drupal_html_class conditional definition. */
512 if (!function_exists('drupal_html_id')) {
514 * Prepare a string for use as a valid HTML ID and guarantee uniqueness.
521 function drupal_html_id($id) {
522 $id = strtr(drupal_strtolower($id), array(' ' => '-', '_' => '-', '[' => '-', ']' => ''));
524 // As defined in http://www.w3.org/TR/html4/types.html#type-name, HTML IDs can
525 // only contain letters, digits ([0-9]), hyphens ("-"), underscores ("_"),
526 // colons (":"), and periods ("."). We strip out any character not in that
527 // list. Note that the CSS spec doesn't allow colons or periods in identifiers
528 // (http://www.w3.org/TR/CSS21/syndata.html#characters), so we strip those two
529 // characters as well.
530 $id = preg_replace('/[^A-Za-z0-9\-_]/', '', $id);
534 } /* End of drupal_html_id conditional definition. */
537 * Returns the path to the Zen theme.
539 * drupal_get_filename() is broken; see #341140. When that is fixed in Drupal 6,
540 * replace _zen_path() with drupal_get_path('theme', 'zen').
542 function _zen_path() {
543 static
$path = FALSE
;
545 $matches = drupal_system_listing('zen\.info$', 'themes', 'name', 0);
546 if (!empty($matches['zen']->filename
)) {
547 $path = dirname($matches['zen']->filename
);