Updated html-reset comments.
[project/zen.git] / template.php
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * Contains theme override functions and preprocess functions for the Zen theme.
7 *
8 * IMPORTANT WARNING: DO NOT MODIFY THIS FILE.
9 *
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
14 */
15
16 // Auto-rebuild the theme registry during theme development.
17 if (theme_get_setting('zen_rebuild_registry')) {
18 drupal_rebuild_theme_registry();
19 }
20
21
22 /**
23 * Implements HOOK_theme().
24 */
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);
29 }
30
31 /**
32 * Return a themed breadcrumb trail.
33 *
34 * @param $breadcrumb
35 * An array containing the breadcrumb links.
36 * @return
37 * A string containing the breadcrumb output.
38 */
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') {
43
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);
48 }
49
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;
57 }
58 }
59 elseif (theme_get_setting('zen_breadcrumb_trailing')) {
60 $trailing_separator = $breadcrumb_separator;
61 }
62 return '<div class="breadcrumb">' . implode($breadcrumb_separator, $breadcrumb) . "$trailing_separator$title</div>";
63 }
64 }
65 // Otherwise, return an empty string.
66 return '';
67 }
68
69 /**
70 * Return a themed set of links.
71 *
72 * @param $links
73 * A keyed array of links to be themed.
74 * @param $attributes
75 * A keyed array of attributes
76 * @param $heading
77 * An optional keyed array or a string for a heading to precede the links.
78 * When using an array the following keys can be used:
79 * - text: the heading text
80 * - level: the heading level (e.g. 'h2', 'h3')
81 * - class: (optional) a string of the CSS classes for the heading
82 * When using a string it will be used as the text of the heading and the
83 * level will default to 'h2'.
84 * Headings should be used on navigation menus and any list of links that
85 * consistently appears on multiple pages. To make the heading invisible
86 * use the 'element-invisible' CSS class. Do not use 'display:none', which
87 * removes it from screen-readers and assistive technology. Headings allow
88 * screen-reader and keyboard only users to navigate to or skip the links.
89 * See http://juicystudio.com/article/screen-readers-display-none.php
90 * and http://www.w3.org/TR/WCAG-TECHS/H42.html for more information.
91 * @return
92 * A string containing an unordered list of links.
93 */
94 function zen_links($links, $attributes = array('class' => 'links'), $heading = '') {
95 global $language;
96 $output = '';
97
98 if (count($links) > 0) {
99 // Treat the heading first if it is present to prepend it to the
100 // list of links.
101 if (!empty($heading)) {
102 if (is_string($heading)) {
103 // Prepare the array that will be used when the passed heading
104 // is a string.
105 $heading = array(
106 'text' => $heading,
107 // Set the default level of the heading.
108 'level' => 'h2',
109 );
110 }
111 $output .= '<' . $heading['level'];
112 if (!empty($heading['class'])) {
113 $output .= drupal_attributes(array('class' => $heading['class']));
114 }
115 $output .= '>' . check_plain($heading['text']) . '</' . $heading['level'] . '>';
116 }
117
118 $output .= '<ul'. drupal_attributes($attributes) .'>';
119
120 $num_links = count($links);
121 $i = 1;
122
123 foreach ($links as $key => $link) {
124 $class = $key;
125
126 // Add first, last and active classes to the list of links to help out themers.
127 if ($i == 1) {
128 $class .= ' first';
129 }
130 if ($i == $num_links) {
131 $class .= ' last';
132 }
133 if (isset($link['href']) && ($link['href'] == $_GET['q'] || ($link['href'] == '<front>' && drupal_is_front_page()))
134 && (empty($link['language']) || $link['language']->language == $language->language)) {
135 $class .= ' active';
136 }
137 $output .= '<li'. drupal_attributes(array('class' => $class)) .'>';
138
139 if (isset($link['href'])) {
140 // Pass in $link as $options, they share the same keys.
141 $output .= l($link['title'], $link['href'], $link);
142 }
143 else if (!empty($link['title'])) {
144 // Some links are actually not links, but we wrap these in <span> for adding title and class attributes
145 if (empty($link['html'])) {
146 $link['title'] = check_plain($link['title']);
147 }
148 $span_attributes = '';
149 if (isset($link['attributes'])) {
150 $span_attributes = drupal_attributes($link['attributes']);
151 }
152 $output .= '<span'. $span_attributes .'>'. $link['title'] .'</span>';
153 }
154
155 $i++;
156 $output .= "</li>\n";
157 }
158
159 $output .= '</ul>';
160 }
161
162 return $output;
163 }
164
165 /**
166 * Implements theme_menu_item_link()
167 */
168 function zen_menu_item_link($link) {
169 if (empty($link['localized_options'])) {
170 $link['localized_options'] = array();
171 }
172
173 // If an item is a LOCAL TASK, render it as a tab
174 if ($link['type'] & MENU_IS_LOCAL_TASK) {
175 $link['title'] = '<span class="tab">' . check_plain($link['title']) . '</span>';
176 $link['localized_options']['html'] = TRUE;
177 }
178
179 return l($link['title'], $link['href'], $link['localized_options']);
180 }
181
182 /**
183 * Duplicate of theme_menu_local_tasks() but adds clearfix to tabs.
184 */
185 function zen_menu_local_tasks() {
186 $output = '';
187
188 // CTools requires a different set of local task functions.
189 if (module_exists('ctools')) {
190 $primary = ctools_menu_primary_local_tasks();
191 $secondary = ctools_menu_secondary_local_tasks();
192 }
193 else {
194 $primary = menu_primary_local_tasks();
195 $secondary = menu_secondary_local_tasks();
196 }
197
198 if ($primary) {
199 $output .= '<ul class="tabs primary clearfix">' . $primary . '</ul>';
200 }
201 if ($secondary) {
202 $output .= '<ul class="tabs secondary clearfix">' . $secondary . '</ul>';
203 }
204
205 return $output;
206 }
207
208 /**
209 * Return a set of blocks available for the current user.
210 *
211 * @param $region
212 * Which set of blocks to retrieve.
213 * @param $show_blocks
214 * A boolean to determine whether to render sidebar regions or not. Should be
215 * the same value as passed to theme_page.
216 * @return
217 * A string containing the themed blocks for this region.
218 *
219 * @see zen_show_blocks_discovery()
220 */
221 function zen_blocks($region, $show_blocks = NULL) {
222 // Since Drupal 6 doesn't pass $show_blocks to theme_blocks, we manually call
223 // theme('blocks', NULL, $show_blocks) so that this function can remember the
224 // value on later calls.
225 static $render_sidebars = TRUE;
226 if (!is_null($show_blocks)) {
227 $render_sidebars = $show_blocks;
228 }
229
230 // If zen_blocks was called with a NULL region, its likely we were just
231 // setting the $render_sidebars static variable.
232 if ($region) {
233 $output = '';
234
235 // If $renders_sidebars is FALSE, don't render any region whose name begins
236 // with "sidebar_".
237 if (($render_sidebars || (strpos($region, 'sidebar_') !== 0)) && ($list = block_list($region))) {
238 foreach ($list as $key => $block) {
239 // $key == module_delta
240 $output .= theme('block', $block);
241 }
242 }
243
244 // Add any content assigned to this region through drupal_set_content() calls.
245 $output .= drupal_get_content($region);
246
247 $elements['#children'] = $output;
248 $elements['#region'] = $region;
249
250 return $output ? theme('region', $elements) : '';
251 }
252 }
253
254 /**
255 * Examine the $show_blocks variable before template_preprocess_page() is called.
256 *
257 * @param $vars
258 * An array of variables to pass to the page template.
259 *
260 * @see zen_blocks()
261 */
262 function zen_show_blocks_discovery(&$vars) {
263 if ($vars['show_blocks'] == FALSE) {
264 // Allow zen_blocks() to statically cache the $show_blocks variable. A TRUE
265 // value is assumed, so we only need to override when $show_blocks is FALSE.
266 theme('blocks', NULL, FALSE);
267 }
268 }
269
270 /**
271 * Override or insert variables into templates before other preprocess functions have run.
272 *
273 * @param $vars
274 * An array of variables to pass to the theme template.
275 * @param $hook
276 * The name of the template being rendered.
277 */
278 function zen_preprocess(&$vars, $hook) {
279 // In D6, the page.tpl uses a different variable name to hold the classes.
280 $key = ($hook == 'page' || $hook == 'maintenance_page') ? 'body_classes' : 'classes';
281
282 // Create a D7-standard classes_array variable.
283 if (array_key_exists($key, $vars)) {
284 // Views (and possibly other modules) have templates with a $classes
285 // variable that isn't a string, so we leave those variables alone.
286 if (is_string($vars[$key])) {
287 $vars['classes_array'] = explode(' ', $vars[$key]);
288 unset($vars[$key]);
289 }
290 }
291 else {
292 $vars['classes_array'] = array($hook);
293 }
294 // Add support for Skinr
295 if (!empty($vars['skinr']) && array_key_exists('classes_array', $vars)) {
296 $vars['classes_array'][] = $vars['skinr'];
297 }
298 }
299
300 /**
301 * Override or insert variables into the page templates.
302 *
303 * @param $vars
304 * An array of variables to pass to the theme template.
305 * @param $hook
306 * The name of the template being rendered ("page" in this case.)
307 */
308 function zen_preprocess_page(&$vars, $hook) {
309 // If the user is silly and enables Zen as the theme, add some styles.
310 if ($GLOBALS['theme'] == 'zen') {
311 include_once './' . _zen_path() . '/zen-internals/template.zen.inc';
312 _zen_preprocess_page($vars, $hook);
313 }
314 // Add conditional stylesheets.
315 elseif (!module_exists('conditional_styles')) {
316 $vars['styles'] .= $vars['conditional_styles'] = variable_get('conditional_styles_' . $GLOBALS['theme'], '');
317 }
318
319 // Classes for body element. Allows advanced theming based on context
320 // (home page, node of certain type, etc.)
321 // Remove the mostly useless page-ARG0 class.
322 if ($index = array_search(preg_replace('![^abcdefghijklmnopqrstuvwxyz0-9-_]+!s', '', 'page-'. drupal_strtolower(arg(0))), $vars['classes_array'])) {
323 unset($vars['classes_array'][$index]);
324 }
325 if (!$vars['is_front']) {
326 // Add unique class for each page.
327 $path = drupal_get_path_alias($_GET['q']);
328 $vars['classes_array'][] = drupal_html_class('page-' . $path);
329 // Add unique class for each website section.
330 list($section, ) = explode('/', $path, 2);
331 if (arg(0) == 'node') {
332 if (arg(1) == 'add') {
333 $section = 'node-add';
334 }
335 elseif (is_numeric(arg(1)) && (arg(2) == 'edit' || arg(2) == 'delete')) {
336 $section = 'node-' . arg(2);
337 }
338 }
339 $vars['classes_array'][] = drupal_html_class('section-' . $section);
340 }
341 if (theme_get_setting('zen_wireframes')) {
342 $vars['classes_array'][] = 'with-wireframes'; // Optionally add the wireframes style.
343 }
344 // We need to re-do the $layout and body classes because
345 // template_preprocess_page() assumes sidebars are named 'left' and 'right'.
346 $vars['layout'] = 'none';
347 if (!empty($vars['sidebar_first'])) {
348 $vars['layout'] = 'first';
349 }
350 if (!empty($vars['sidebar_second'])) {
351 $vars['layout'] = ($vars['layout'] == 'first') ? 'both' : 'second';
352 }
353 // If the layout is 'none', then template_preprocess_page() will already have
354 // set a 'no-sidebars' class since it won't find a 'left' or 'right' sidebar.
355 if ($vars['layout'] != 'none') {
356 // Remove the incorrect 'no-sidebars' class.
357 if ($index = array_search('no-sidebars', $vars['classes_array'])) {
358 unset($vars['classes_array'][$index]);
359 }
360 // Set the proper layout body classes.
361 if ($vars['layout'] == 'both') {
362 $vars['classes_array'][] = 'two-sidebars';
363 }
364 else {
365 $vars['classes_array'][] = 'one-sidebar';
366 $vars['classes_array'][] = 'sidebar-' . $vars['layout'];
367 }
368 }
369 // Store the menu item since it has some useful information.
370 $vars['menu_item'] = menu_get_item();
371 switch ($vars['menu_item']['page_callback']) {
372 case 'views_page':
373 // Is this a Views page?
374 $vars['classes_array'][] = 'page-views';
375 break;
376 case 'page_manager_page_execute':
377 // Is this a Panels page?
378 $vars['classes_array'][] = 'page-panels';
379 break;
380 }
381 }
382
383 /**
384 * Override or insert variables into the maintenance page template.
385 *
386 * @param $vars
387 * An array of variables to pass to the theme template.
388 * @param $hook
389 * The name of the template being rendered ("maintenance_page" in this case.)
390 */
391 function zen_preprocess_maintenance_page(&$vars, $hook) {
392 // If Zen is the maintenance theme, add some styles.
393 if ($GLOBALS['theme'] == 'zen') {
394 include_once './' . _zen_path() . '/zen-internals/template.zen.inc';
395 _zen_preprocess_page($vars, $hook);
396 }
397 // Add conditional stylesheets.
398 elseif (!module_exists('conditional_styles')) {
399 $vars['styles'] .= $vars['conditional_styles'] = variable_get('conditional_styles_' . $GLOBALS['theme'], '');
400 }
401
402 // Classes for body element. Allows advanced theming based on context
403 // (home page, node of certain type, etc.)
404 $vars['body_classes_array'] = explode(' ', $vars['body_classes']);
405 }
406
407 /**
408 * Override or insert variables into the node templates.
409 *
410 * @param $vars
411 * An array of variables to pass to the theme template.
412 * @param $hook
413 * The name of the template being rendered ("node" in this case.)
414 */
415 function zen_preprocess_node(&$vars, $hook) {
416 // Create the build_mode variable.
417 switch ($vars['node']->build_mode) {
418 case NODE_BUILD_NORMAL:
419 $vars['build_mode'] = $vars['teaser'] ? 'teaser' : 'full';
420 break;
421 case NODE_BUILD_PREVIEW:
422 $vars['build_mode'] = 'preview';
423 break;
424 case NODE_BUILD_SEARCH_INDEX:
425 $vars['build_mode'] = 'search_index';
426 break;
427 case NODE_BUILD_SEARCH_RESULT:
428 $vars['build_mode'] = 'search_result';
429 break;
430 case NODE_BUILD_RSS:
431 $vars['build_mode'] = 'rss';
432 break;
433 case NODE_BUILD_PRINT:
434 $vars['build_mode'] = 'print';
435 break;
436 }
437
438 // Create the user_picture variable.
439 $vars['user_picture'] = $vars['picture'];
440
441 // Create the Drupal 7 $display_submitted variable.
442 $vars['display_submitted'] = theme_get_setting('toggle_node_info_' . $vars['node']->type);
443
444 // Special classes for nodes.
445 // Class for node type: "node-type-page", "node-type-story", "node-type-my-custom-type", etc.
446 $vars['classes_array'][] = drupal_html_class('node-type-' . $vars['type']);
447 if ($vars['promote']) {
448 $vars['classes_array'][] = 'node-promoted';
449 }
450 if ($vars['sticky']) {
451 $vars['classes_array'][] = 'node-sticky';
452 }
453 if (!$vars['status']) {
454 $vars['classes_array'][] = 'node-unpublished';
455 $vars['unpublished'] = TRUE;
456 }
457 else {
458 $vars['unpublished'] = FALSE;
459 }
460 if ($vars['uid'] && $vars['uid'] == $GLOBALS['user']->uid) {
461 $vars['classes_array'][] = 'node-by-viewer'; // Node is authored by current user.
462 }
463 if ($vars['teaser']) {
464 $vars['classes_array'][] = 'node-teaser'; // Node is displayed as teaser.
465 }
466 if (isset($vars['preview'])) {
467 $vars['classes_array'][] = 'node-preview';
468 }
469 }
470
471 /**
472 * Override or insert variables into the comment templates.
473 *
474 * @param $vars
475 * An array of variables to pass to the theme template.
476 * @param $hook
477 * The name of the template being rendered ("comment" in this case.)
478 */
479 function zen_preprocess_comment(&$vars, $hook) {
480 include_once './' . _zen_path() . '/zen-internals/template.comment.inc';
481 _zen_preprocess_comment($vars, $hook);
482 }
483
484 /**
485 * Preprocess variables for region.tpl.php
486 *
487 * Prepare the values passed to the theme_region function to be passed into a
488 * pluggable template engine. Uses the region name to generate a template file
489 * suggestions. If none are found, the default region.tpl.php is used.
490 *
491 * @see region.tpl.php
492 */
493 function zen_preprocess_region(&$vars, $hook) {
494 // Create the $content variable that templates expect.
495 $vars['content'] = $vars['elements']['#children'];
496 $vars['region'] = $vars['elements']['#region'];
497
498 // Setup the default classes.
499 $region = 'region-' . str_replace('_', '-', $vars['region']);
500 $vars['classes_array'] = array('region', $region);
501
502 // Sidebar regions get a common template suggestion a couple extra classes.
503 if (strpos($vars['region'], 'sidebar_') === 0) {
504 $vars['template_files'][] = 'region-sidebar';
505 $vars['classes_array'][] = 'column';
506 $vars['classes_array'][] = 'sidebar';
507 }
508
509 // Setup the default template suggestion.
510 $vars['template_files'][] = $region;
511 }
512
513 /**
514 * Override or insert variables into the block templates.
515 *
516 * @param $vars
517 * An array of variables to pass to the theme template.
518 * @param $hook
519 * The name of the template being rendered ("block" in this case.)
520 */
521 function zen_preprocess_block(&$vars, $hook) {
522 $block = $vars['block'];
523
524 // Drupal 7 uses a $content variable instead of $block->content.
525 $vars['content'] = $block->content;
526 // Drupal 7 should use a $title variable instead of $block->subject.
527 $vars['title'] = $block->subject;
528
529 // Special classes for blocks.
530 $vars['classes_array'][] = 'block-' . $block->module;
531 $vars['classes_array'][] = 'region-' . $vars['block_zebra'];
532 $vars['classes_array'][] = $vars['zebra'];
533 $vars['classes_array'][] = 'region-count-' . $vars['block_id'];
534 $vars['classes_array'][] = 'count-' . $vars['id'];
535
536 $vars['edit_links_array'] = array();
537 if (theme_get_setting('zen_block_editing') && user_access('administer blocks')) {
538 include_once './' . _zen_path() . '/zen-internals/template.block-editing.inc';
539 zen_preprocess_block_editing($vars, $hook);
540 $vars['classes_array'][] = 'with-block-editing';
541 }
542 }
543
544 /**
545 * Override or insert variables into the views-view templates.
546 *
547 * @param $vars
548 * An array of variables to pass to the theme template.
549 * @param $hook
550 * The name of the template being rendered ("views-view" in this case.)
551 */
552 function zen_preprocess_views_view(&$vars, $hook) {
553 // Add the default Views classes.
554 $vars['classes_array'][0] = 'view'; // Replace "views-view".
555 $vars['classes_array'][] = 'view-' . $vars['css_name'];
556 $vars['classes_array'][] = 'view-id-' . $vars['name'];
557 $vars['classes_array'][] = 'view-display-id-' . $vars['display_id'];
558 $vars['classes_array'][] = 'view-dom-id-' . $vars['dom_id'];
559 }
560
561 /**
562 * Override or insert variables into templates after preprocess functions have run.
563 *
564 * @param $vars
565 * An array of variables to pass to the theme template.
566 * @param $hook
567 * The name of the template being rendered.
568 */
569 function zen_process(&$vars, $hook) {
570 if (array_key_exists('classes_array', $vars)) {
571 $vars['classes'] = implode(' ', $vars['classes_array']);
572 }
573 }
574
575 /**
576 * Override or insert variables into the block templates after preprocess functions have run.
577 *
578 * @param $vars
579 * An array of variables to pass to the theme template.
580 * @param $hook
581 * The name of the template being rendered ("block" in this case.)
582 */
583 function zen_process_block(&$vars, $hook) {
584 $vars['edit_links'] = !empty($vars['edit_links_array']) ? '<div class="edit">' . implode(' ', $vars['edit_links_array']) . '</div>' : '';
585 }
586
587 if (!function_exists('drupal_html_class')) {
588 /**
589 * Prepare a string for use as a valid class name.
590 *
591 * Do not pass one string containing multiple classes as they will be
592 * incorrectly concatenated with dashes, i.e. "one two" will become "one-two".
593 *
594 * @param $class
595 * The class name to clean.
596 * @return
597 * The cleaned class name.
598 */
599 function drupal_html_class($class) {
600 // By default, we filter using Drupal's coding standards.
601 $class = strtr(drupal_strtolower($class), array(' ' => '-', '_' => '-', '/' => '-', '[' => '-', ']' => ''));
602
603 // http://www.w3.org/TR/CSS21/syndata.html#characters shows the syntax for valid
604 // CSS identifiers (including element names, classes, and IDs in selectors.)
605 //
606 // Valid characters in a CSS identifier are:
607 // - the hyphen (U+002D)
608 // - a-z (U+0030 - U+0039)
609 // - A-Z (U+0041 - U+005A)
610 // - the underscore (U+005F)
611 // - 0-9 (U+0061 - U+007A)
612 // - ISO 10646 characters U+00A1 and higher
613 // We strip out any character not in the above list.
614 $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);
615
616 return $class;
617 }
618 } /* End of drupal_html_class conditional definition. */
619
620 if (!function_exists('drupal_html_id')) {
621 /**
622 * Prepare a string for use as a valid HTML ID and guarantee uniqueness.
623 *
624 * @param $id
625 * The ID to clean.
626 * @return
627 * The cleaned ID.
628 */
629 function drupal_html_id($id) {
630 $id = strtr(drupal_strtolower($id), array(' ' => '-', '_' => '-', '[' => '-', ']' => ''));
631
632 // As defined in http://www.w3.org/TR/html4/types.html#type-name, HTML IDs can
633 // only contain letters, digits ([0-9]), hyphens ("-"), underscores ("_"),
634 // colons (":"), and periods ("."). We strip out any character not in that
635 // list. Note that the CSS spec doesn't allow colons or periods in identifiers
636 // (http://www.w3.org/TR/CSS21/syndata.html#characters), so we strip those two
637 // characters as well.
638 $id = preg_replace('/[^A-Za-z0-9\-_]/', '', $id);
639
640 return $id;
641 }
642 } /* End of drupal_html_id conditional definition. */
643
644 /**
645 * Returns the path to the Zen theme.
646 *
647 * drupal_get_filename() is broken; see #341140. When that is fixed in Drupal 6,
648 * replace _zen_path() with drupal_get_path('theme', 'zen').
649 */
650 function _zen_path() {
651 static $path = FALSE;
652 if (!$path) {
653 $matches = drupal_system_listing('zen\.info$', 'themes', 'name', 0);
654 if (!empty($matches['zen']->filename)) {
655 $path = dirname($matches['zen']->filename);
656 }
657 }
658 return $path;
659 }