Renamed $closure_region region to $page_closure
[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 $trailing_separator = $breadcrumb_separator;
56 $title = drupal_get_title();
57 }
58 elseif (theme_get_setting('zen_breadcrumb_trailing')) {
59 $trailing_separator = $breadcrumb_separator;
60 }
61 return '<div class="breadcrumb">' . implode($breadcrumb_separator, $breadcrumb) . "$trailing_separator$title</div>";
62 }
63 }
64 // Otherwise, return an empty string.
65 return '';
66 }
67
68 /**
69 * Implements theme_menu_item_link()
70 */
71 function zen_menu_item_link($link) {
72 if (empty($link['localized_options'])) {
73 $link['localized_options'] = array();
74 }
75
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;
80 }
81
82 return l($link['title'], $link['href'], $link['localized_options']);
83 }
84
85 /**
86 * Duplicate of theme_menu_local_tasks() but adds clear-block to tabs.
87 */
88 function zen_menu_local_tasks() {
89 $output = '';
90
91 if ($primary = menu_primary_local_tasks()) {
92 $output .= '<ul class="tabs primary clear-block">' . $primary . '</ul>';
93 }
94 if ($secondary = menu_secondary_local_tasks()) {
95 $output .= '<ul class="tabs secondary clear-block">' . $secondary . '</ul>';
96 }
97
98 return $output;
99 }
100
101 /**
102 * Return a set of blocks available for the current user.
103 *
104 * @param $region
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.
109 * @return
110 * A string containing the themed blocks for this region.
111 *
112 * @see zen_show_blocks_discovery()
113 */
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;
121 }
122
123 // If zen_blocks was called with a NULL region, its likely we were just
124 // setting the $render_sidebars static variable.
125 if ($region) {
126 $output = '';
127
128 // If $renders_sidebars is FALSE, don't render any region whose name begins
129 // with "sidebar_".
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);
134 }
135 }
136
137 // Add any content assigned to this region through drupal_set_content() calls.
138 $output .= drupal_get_content($region);
139
140 $elements['#children'] = $output;
141 $elements['#region'] = $region;
142
143 return $output ? theme('region', $elements) : '';
144 }
145 }
146
147 /**
148 * Examine the $show_blocks variable before template_preprocess_page() is called.
149 *
150 * @param $vars
151 * An array of variables to pass to the page template.
152 *
153 * @see zen_blocks()
154 */
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);
160 }
161 }
162
163 /**
164 * Override or insert variables into templates before other preprocess functions have run.
165 *
166 * @param $vars
167 * An array of variables to pass to the theme template.
168 * @param $hook
169 * The name of the template being rendered.
170 */
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';
174
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]);
181 unset($vars[$key]);
182 }
183 }
184 else {
185 $vars['classes_array'] = array($hook);
186 }
187 }
188
189 /**
190 * Override or insert variables into the page templates.
191 *
192 * @param $vars
193 * An array of variables to pass to the theme template.
194 * @param $hook
195 * The name of the template being rendered ("page" in this case.)
196 */
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);
202 }
203 // Add conditional stylesheets.
204 elseif (!module_exists('conditional_styles')) {
205 $vars['styles'] .= $vars['conditional_styles'] = variable_get('conditional_styles_' . $GLOBALS['theme'], '');
206 }
207
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]);
213 }
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';
223 }
224 elseif (is_numeric(arg(1)) && (arg(2) == 'edit' || arg(2) == 'delete')) {
225 $section = 'node-' . arg(2);
226 }
227 }
228 $vars['classes_array'][] = zen_id_safe('section-' . $section);
229 }
230 if (theme_get_setting('zen_wireframes')) {
231 $vars['classes_array'][] = 'with-wireframes'; // Optionally add the wireframes style.
232 }
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';
238 }
239 if (!empty($vars['sidebar_second'])) {
240 $vars['layout'] = ($vars['layout'] == 'first') ? 'both' : 'second';
241 }
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]);
248 }
249 // Set the proper layout body classes.
250 if ($vars['layout'] == 'both') {
251 $vars['classes_array'][] = 'two-sidebars';
252 }
253 else {
254 $vars['classes_array'][] = 'one-sidebar';
255 $vars['classes_array'][] = 'sidebar-' . $vars['layout'];
256 }
257 }
258 }
259
260 /**
261 * Override or insert variables into the maintenance page template.
262 *
263 * @param $vars
264 * An array of variables to pass to the theme template.
265 * @param $hook
266 * The name of the template being rendered ("maintenance_page" in this case.)
267 */
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);
273 }
274 // Add conditional stylesheets.
275 elseif (!module_exists('conditional_styles')) {
276 $vars['styles'] .= $vars['conditional_styles'] = variable_get('conditional_styles_' . $GLOBALS['theme'], '');
277 }
278
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']);
282 }
283
284 /**
285 * Override or insert variables into the node templates.
286 *
287 * @param $vars
288 * An array of variables to pass to the theme template.
289 * @param $hook
290 * The name of the template being rendered ("node" in this case.)
291 */
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';
297 break;
298 case NODE_BUILD_PREVIEW:
299 $vars['build_mode'] = 'preview';
300 break;
301 case NODE_BUILD_SEARCH_INDEX:
302 $vars['build_mode'] = 'search_index';
303 break;
304 case NODE_BUILD_SEARCH_RESULT:
305 $vars['build_mode'] = 'search_result';
306 break;
307 case NODE_BUILD_RSS:
308 $vars['build_mode'] = 'rss';
309 break;
310 case NODE_BUILD_PRINT:
311 $vars['build_mode'] = 'print';
312 break;
313 }
314
315 // Create the user_picture variable.
316 $vars['user_picture'] = $vars['picture'];
317
318 // Create the Drupal 7 $display_submitted variable.
319 $vars['display_submitted'] = theme_get_setting('toggle_node_info_' . $vars['node']->type);
320
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';
326 }
327 if ($vars['sticky']) {
328 $vars['classes_array'][] = 'node-sticky';
329 }
330 if (!$vars['status']) {
331 $vars['classes_array'][] = 'node-unpublished';
332 $vars['unpublished'] = TRUE;
333 }
334 else {
335 $vars['unpublished'] = FALSE;
336 }
337 if ($vars['uid'] && $vars['uid'] == $GLOBALS['user']->uid) {
338 $vars['classes_array'][] = 'node-by-viewer'; // Node is authored by current user.
339 }
340 if ($vars['teaser']) {
341 $vars['classes_array'][] = 'node-teaser'; // Node is displayed as teaser.
342 }
343 if (isset($vars['preview'])) {
344 $vars['classes_array'][] = 'node-preview';
345 }
346 }
347
348 /**
349 * Override or insert variables into the comment templates.
350 *
351 * @param $vars
352 * An array of variables to pass to the theme template.
353 * @param $hook
354 * The name of the template being rendered ("comment" in this case.)
355 */
356 function zen_preprocess_comment(&$vars, $hook) {
357 include_once './' . _zen_path() . '/zen-internals/template.comment.inc';
358 _zen_preprocess_comment($vars, $hook);
359 }
360
361 /**
362 * Preprocess variables for region.tpl.php
363 *
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.
367 *
368 * @see region.tpl.php
369 */
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'];
374
375 // Setup the default classes.
376 $region = 'region-' . str_replace('_', '-', $vars['region']);
377 $vars['classes_array'] = array('region', $region);
378
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';
384 }
385
386 // Setup the default template suggestion.
387 $vars['template_files'][] = $region;
388 }
389
390 /**
391 * Override or insert variables into the block templates.
392 *
393 * @param $vars
394 * An array of variables to pass to the theme template.
395 * @param $hook
396 * The name of the template being rendered ("block" in this case.)
397 */
398 function zen_preprocess_block(&$vars, $hook) {
399 $block = $vars['block'];
400
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;
405
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'];
412
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';
418 }
419 }
420
421 /**
422 * Override or insert variables into templates after preprocess functions have run.
423 *
424 * @param $vars
425 * An array of variables to pass to the theme template.
426 * @param $hook
427 * The name of the template being rendered.
428 */
429 function zen_process(&$vars, $hook) {
430 if (array_key_exists('classes_array', $vars)) {
431 $vars['classes'] = implode(' ', $vars['classes_array']);
432 }
433 }
434
435 /**
436 * Override or insert variables into the block templates after preprocess functions have run.
437 *
438 * @param $vars
439 * An array of variables to pass to the theme template.
440 * @param $hook
441 * The name of the template being rendered ("block" in this case.)
442 */
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>' : '';
445 }
446
447 /**
448 * Converts a string to a suitable html ID attribute.
449 *
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:
452 *
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.
456 *
457 * @param $string
458 * The string
459 * @return
460 * The converted string
461 */
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;
468 }
469 return $string;
470 }
471
472 /**
473 * Returns the path to the Zen theme.
474 *
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').
477 */
478 function _zen_path() {
479 static $path = FALSE;
480 if (!$path) {
481 $matches = drupal_system_listing('zen\.info$', 'themes', 'name', 0);
482 if (!empty($matches['zen']->filename)) {
483 $path = dirname($matches['zen']->filename);
484 }
485 }
486 return $path;
487 }