#593290 by sfyn and JohnAlbin: Chaos Tools/Panels edit tab not working
[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 * Implements theme_menu_item_link()
71 */
72 function zen_menu_item_link($link) {
73 if (empty($link['localized_options'])) {
74 $link['localized_options'] = array();
75 }
76
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;
81 }
82
83 return l($link['title'], $link['href'], $link['localized_options']);
84 }
85
86 /**
87 * Duplicate of theme_menu_local_tasks() but adds clearfix to tabs.
88 */
89 function zen_menu_local_tasks() {
90 $output = '';
91
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();
96 }
97 else {
98 $primary = menu_primary_local_tasks();
99 $secondary = menu_secondary_local_tasks();
100 }
101
102 if ($primary) {
103 $output .= '<ul class="tabs primary clearfix">' . $primary . '</ul>';
104 }
105 if ($secondary) {
106 $output .= '<ul class="tabs secondary clearfix">' . $secondary . '</ul>';
107 }
108
109 return $output;
110 }
111
112 /**
113 * Return a set of blocks available for the current user.
114 *
115 * @param $region
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.
120 * @return
121 * A string containing the themed blocks for this region.
122 *
123 * @see zen_show_blocks_discovery()
124 */
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;
132 }
133
134 // If zen_blocks was called with a NULL region, its likely we were just
135 // setting the $render_sidebars static variable.
136 if ($region) {
137 $output = '';
138
139 // If $renders_sidebars is FALSE, don't render any region whose name begins
140 // with "sidebar_".
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);
145 }
146 }
147
148 // Add any content assigned to this region through drupal_set_content() calls.
149 $output .= drupal_get_content($region);
150
151 $elements['#children'] = $output;
152 $elements['#region'] = $region;
153
154 return $output ? theme('region', $elements) : '';
155 }
156 }
157
158 /**
159 * Examine the $show_blocks variable before template_preprocess_page() is called.
160 *
161 * @param $vars
162 * An array of variables to pass to the page template.
163 *
164 * @see zen_blocks()
165 */
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);
171 }
172 }
173
174 /**
175 * Override or insert variables into templates before other preprocess functions have run.
176 *
177 * @param $vars
178 * An array of variables to pass to the theme template.
179 * @param $hook
180 * The name of the template being rendered.
181 */
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';
185
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]);
192 unset($vars[$key]);
193 }
194 }
195 else {
196 $vars['classes_array'] = array($hook);
197 }
198 // Add support for Skinr
199 if (!empty($vars['skinr']) && array_key_exists('classes_array', $vars)) {
200 $vars['classes_array'][] = $vars['skinr'];
201 }
202 }
203
204 /**
205 * Override or insert variables into the page templates.
206 *
207 * @param $vars
208 * An array of variables to pass to the theme template.
209 * @param $hook
210 * The name of the template being rendered ("page" in this case.)
211 */
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);
217 }
218 // Add conditional stylesheets.
219 elseif (!module_exists('conditional_styles')) {
220 $vars['styles'] .= $vars['conditional_styles'] = variable_get('conditional_styles_' . $GLOBALS['theme'], '');
221 }
222
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]);
228 }
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';
238 }
239 elseif (is_numeric(arg(1)) && (arg(2) == 'edit' || arg(2) == 'delete')) {
240 $section = 'node-' . arg(2);
241 }
242 }
243 $vars['classes_array'][] = drupal_html_class('section-' . $section);
244 }
245 if (theme_get_setting('zen_wireframes')) {
246 $vars['classes_array'][] = 'with-wireframes'; // Optionally add the wireframes style.
247 }
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';
253 }
254 if (!empty($vars['sidebar_second'])) {
255 $vars['layout'] = ($vars['layout'] == 'first') ? 'both' : 'second';
256 }
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]);
263 }
264 // Set the proper layout body classes.
265 if ($vars['layout'] == 'both') {
266 $vars['classes_array'][] = 'two-sidebars';
267 }
268 else {
269 $vars['classes_array'][] = 'one-sidebar';
270 $vars['classes_array'][] = 'sidebar-' . $vars['layout'];
271 }
272 }
273 }
274
275 /**
276 * Override or insert variables into the maintenance page template.
277 *
278 * @param $vars
279 * An array of variables to pass to the theme template.
280 * @param $hook
281 * The name of the template being rendered ("maintenance_page" in this case.)
282 */
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);
288 }
289 // Add conditional stylesheets.
290 elseif (!module_exists('conditional_styles')) {
291 $vars['styles'] .= $vars['conditional_styles'] = variable_get('conditional_styles_' . $GLOBALS['theme'], '');
292 }
293
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']);
297 }
298
299 /**
300 * Override or insert variables into the node templates.
301 *
302 * @param $vars
303 * An array of variables to pass to the theme template.
304 * @param $hook
305 * The name of the template being rendered ("node" in this case.)
306 */
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';
312 break;
313 case NODE_BUILD_PREVIEW:
314 $vars['build_mode'] = 'preview';
315 break;
316 case NODE_BUILD_SEARCH_INDEX:
317 $vars['build_mode'] = 'search_index';
318 break;
319 case NODE_BUILD_SEARCH_RESULT:
320 $vars['build_mode'] = 'search_result';
321 break;
322 case NODE_BUILD_RSS:
323 $vars['build_mode'] = 'rss';
324 break;
325 case NODE_BUILD_PRINT:
326 $vars['build_mode'] = 'print';
327 break;
328 }
329
330 // Create the user_picture variable.
331 $vars['user_picture'] = $vars['picture'];
332
333 // Create the Drupal 7 $display_submitted variable.
334 $vars['display_submitted'] = theme_get_setting('toggle_node_info_' . $vars['node']->type);
335
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';
341 }
342 if ($vars['sticky']) {
343 $vars['classes_array'][] = 'node-sticky';
344 }
345 if (!$vars['status']) {
346 $vars['classes_array'][] = 'node-unpublished';
347 $vars['unpublished'] = TRUE;
348 }
349 else {
350 $vars['unpublished'] = FALSE;
351 }
352 if ($vars['uid'] && $vars['uid'] == $GLOBALS['user']->uid) {
353 $vars['classes_array'][] = 'node-by-viewer'; // Node is authored by current user.
354 }
355 if ($vars['teaser']) {
356 $vars['classes_array'][] = 'node-teaser'; // Node is displayed as teaser.
357 }
358 if (isset($vars['preview'])) {
359 $vars['classes_array'][] = 'node-preview';
360 }
361 }
362
363 /**
364 * Override or insert variables into the comment templates.
365 *
366 * @param $vars
367 * An array of variables to pass to the theme template.
368 * @param $hook
369 * The name of the template being rendered ("comment" in this case.)
370 */
371 function zen_preprocess_comment(&$vars, $hook) {
372 include_once './' . _zen_path() . '/zen-internals/template.comment.inc';
373 _zen_preprocess_comment($vars, $hook);
374 }
375
376 /**
377 * Preprocess variables for region.tpl.php
378 *
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.
382 *
383 * @see region.tpl.php
384 */
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'];
389
390 // Setup the default classes.
391 $region = 'region-' . str_replace('_', '-', $vars['region']);
392 $vars['classes_array'] = array('region', $region);
393
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';
399 }
400
401 // Setup the default template suggestion.
402 $vars['template_files'][] = $region;
403 }
404
405 /**
406 * Override or insert variables into the block templates.
407 *
408 * @param $vars
409 * An array of variables to pass to the theme template.
410 * @param $hook
411 * The name of the template being rendered ("block" in this case.)
412 */
413 function zen_preprocess_block(&$vars, $hook) {
414 $block = $vars['block'];
415
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;
420
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'];
427
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';
433 }
434 }
435
436 /**
437 * Override or insert variables into the views-view templates.
438 *
439 * @param $vars
440 * An array of variables to pass to the theme template.
441 * @param $hook
442 * The name of the template being rendered ("views-view" in this case.)
443 */
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'];
451 }
452
453 /**
454 * Override or insert variables into templates after preprocess functions have run.
455 *
456 * @param $vars
457 * An array of variables to pass to the theme template.
458 * @param $hook
459 * The name of the template being rendered.
460 */
461 function zen_process(&$vars, $hook) {
462 if (array_key_exists('classes_array', $vars)) {
463 $vars['classes'] = implode(' ', $vars['classes_array']);
464 }
465 }
466
467 /**
468 * Override or insert variables into the block templates after preprocess functions have run.
469 *
470 * @param $vars
471 * An array of variables to pass to the theme template.
472 * @param $hook
473 * The name of the template being rendered ("block" in this case.)
474 */
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>' : '';
477 }
478
479 if (!function_exists('drupal_html_class')) {
480 /**
481 * Prepare a string for use as a valid class name.
482 *
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".
485 *
486 * @param $class
487 * The class name to clean.
488 * @return
489 * The cleaned class name.
490 */
491 function drupal_html_class($class) {
492 // By default, we filter using Drupal's coding standards.
493 $class = strtr(drupal_strtolower($class), array(' ' => '-', '_' => '-', '/' => '-', '[' => '-', ']' => ''));
494
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.)
497 //
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);
507
508 return $class;
509 }
510 } /* End of drupal_html_class conditional definition. */
511
512 if (!function_exists('drupal_html_id')) {
513 /**
514 * Prepare a string for use as a valid HTML ID and guarantee uniqueness.
515 *
516 * @param $id
517 * The ID to clean.
518 * @return
519 * The cleaned ID.
520 */
521 function drupal_html_id($id) {
522 $id = strtr(drupal_strtolower($id), array(' ' => '-', '_' => '-', '[' => '-', ']' => ''));
523
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);
531
532 return $id;
533 }
534 } /* End of drupal_html_id conditional definition. */
535
536 /**
537 * Returns the path to the Zen theme.
538 *
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').
541 */
542 function _zen_path() {
543 static $path = FALSE;
544 if (!$path) {
545 $matches = drupal_system_listing('zen\.info$', 'themes', 'name', 0);
546 if (!empty($matches['zen']->filename)) {
547 $path = dirname($matches['zen']->filename);
548 }
549 }
550 return $path;
551 }