Issue #1046090 by daniels and JohnAlbin: $styles is destroyed by rare bug with APC
[project/zen.git] / zen / template.php
1 <?php
2 /**
3 * @file
4 * Contains theme override functions and preprocess functions for the Zen theme.
5 *
6 * IMPORTANT WARNING: DO NOT MODIFY THIS FILE.
7 *
8 * The base Zen theme is designed to be easily extended by its sub-themes. You
9 * shouldn't modify this or any of the CSS or PHP files in the root zen/ folder.
10 * See the online documentation for more information:
11 * http://drupal.org/node/193318
12 */
13
14 // Auto-rebuild the theme registry during theme development.
15 if (theme_get_setting('zen_rebuild_registry')) {
16 drupal_rebuild_theme_registry();
17 }
18
19 /*
20 * Add stylesheets only needed when Zen is the active theme. Don't do something
21 * this dumb in your sub-theme; see how wireframes.css is handled instead.
22 */
23 if ($GLOBALS['theme'] == 'zen') { // If we're in the main theme
24 if (theme_get_setting('zen_layout') == 'border-politics-fixed') {
25 drupal_add_css(_zen_path() . '/layout-fixed.css', 'theme', 'all');
26 }
27 else {
28 drupal_add_css(_zen_path() . '/layout-liquid.css', 'theme', 'all');
29 }
30 }
31
32 /**
33 * Implements HOOK_theme().
34 */
35 function zen_theme(&$existing, $type, $theme, $path) {
36 include_once './' . _zen_path() . '/template.theme-registry.inc';
37 return _zen_theme($existing, $type, $theme, $path);
38 }
39
40 /**
41 * Return a themed breadcrumb trail.
42 *
43 * @param $breadcrumb
44 * An array containing the breadcrumb links.
45 * @return
46 * A string containing the breadcrumb output.
47 */
48 function zen_breadcrumb($breadcrumb) {
49 // Determine if we are to display the breadcrumb.
50 $show_breadcrumb = theme_get_setting('zen_breadcrumb');
51 if ($show_breadcrumb == 'yes' || $show_breadcrumb == 'admin' && arg(0) == 'admin') {
52
53 // Optionally get rid of the homepage link.
54 $show_breadcrumb_home = theme_get_setting('zen_breadcrumb_home');
55 if (!$show_breadcrumb_home) {
56 array_shift($breadcrumb);
57 }
58
59 // Return the breadcrumb with separators.
60 if (!empty($breadcrumb)) {
61 $breadcrumb_separator = theme_get_setting('zen_breadcrumb_separator');
62 $trailing_separator = $title = '';
63 if (theme_get_setting('zen_breadcrumb_title')) {
64 if ($title = drupal_get_title()) {
65 $trailing_separator = $breadcrumb_separator;
66 }
67 }
68 elseif (theme_get_setting('zen_breadcrumb_trailing')) {
69 $trailing_separator = $breadcrumb_separator;
70 }
71 return '<div class="breadcrumb">' . implode($breadcrumb_separator, $breadcrumb) . "$trailing_separator$title</div>";
72 }
73 }
74 // Otherwise, return an empty string.
75 return '';
76 }
77
78 /**
79 * Implements theme_menu_item_link()
80 */
81 function zen_menu_item_link($link) {
82 if (empty($link['localized_options'])) {
83 $link['localized_options'] = array();
84 }
85
86 // If an item is a LOCAL TASK, render it as a tab
87 if ($link['type'] & MENU_IS_LOCAL_TASK) {
88 $link['title'] = '<span class="tab">' . check_plain($link['title']) . '</span>';
89 $link['localized_options']['html'] = TRUE;
90 }
91
92 return l($link['title'], $link['href'], $link['localized_options']);
93 }
94
95 /**
96 * Duplicate of theme_menu_local_tasks() but adds clear-block to tabs.
97 */
98 function zen_menu_local_tasks() {
99 $output = '';
100
101 // CTools requires a different set of local task functions.
102 if (module_exists('ctools')) {
103 $primary = ctools_menu_primary_local_tasks();
104 $secondary = ctools_menu_secondary_local_tasks();
105 }
106 else {
107 $primary = menu_primary_local_tasks();
108 $secondary = menu_secondary_local_tasks();
109 }
110
111 if ($primary) {
112 $output .= '<ul class="tabs primary clear-block">' . $primary . '</ul>';
113 }
114 if ($secondary) {
115 $output .= '<ul class="tabs secondary clear-block">' . $secondary . '</ul>';
116 }
117
118 return $output;
119 }
120
121
122 /**
123 * Override or insert variables into the page templates.
124 *
125 * @param $vars
126 * An array of variables to pass to the theme template.
127 * @param $hook
128 * The name of the template being rendered ("page" in this case.)
129 */
130 function zen_preprocess_page(&$vars, $hook) {
131 // Add conditional stylesheets.
132 if (!module_exists('conditional_styles')) {
133 $vars['conditional_styles'] = variable_get('conditional_styles_' . $GLOBALS['theme'], '');
134 $vars['styles'] .= $vars['conditional_styles'];
135 }
136
137 // Classes for body element. Allows advanced theming based on context
138 // (home page, node of certain type, etc.)
139 $classes = explode(' ', $vars['body_classes']);
140 // Remove the mostly useless page-ARG0 class.
141 if ($index = array_search(preg_replace('![^abcdefghijklmnopqrstuvwxyz0-9-_]+!s', '', 'page-'. drupal_strtolower(arg(0))), $classes)) {
142 unset($classes[$index]);
143 }
144 if (!$vars['is_front']) {
145 // Add unique class for each page.
146 $path = drupal_get_path_alias($_GET['q']);
147 $classes[] = zen_id_safe('page-' . $path);
148 // Add unique class for each website section.
149 list($section, ) = explode('/', $path, 2);
150 if (arg(0) == 'node') {
151 if (arg(1) == 'add') {
152 $section = 'node-add';
153 }
154 elseif (is_numeric(arg(1)) && (arg(2) == 'edit' || arg(2) == 'delete')) {
155 $section = 'node-' . arg(2);
156 }
157 }
158 $classes[] = zen_id_safe('section-' . $section);
159 }
160 if (theme_get_setting('zen_wireframes')) {
161 $classes[] = 'with-wireframes'; // Optionally add the wireframes style.
162 }
163 $vars['body_classes_array'] = $classes;
164 $vars['body_classes'] = implode(' ', $classes); // Concatenate with spaces.
165 }
166
167 /**
168 * Override or insert variables into the maintenance page template.
169 *
170 * @param $vars
171 * An array of variables to pass to the theme template.
172 * @param $hook
173 * The name of the template being rendered ("maintenance_page" in this case.)
174 */
175 function zen_preprocess_maintenance_page(&$vars, $hook) {
176 // Add conditional stylesheets.
177 if (!module_exists('conditional_styles')) {
178 $vars['conditional_styles'] = variable_get('conditional_styles_' . $GLOBALS['theme'], '');
179 $vars['styles'] .= $vars['conditional_styles'];
180 }
181
182 // Classes for body element. Allows advanced theming based on context
183 // (home page, node of certain type, etc.)
184 $vars['body_classes_array'] = explode(' ', $vars['body_classes']);
185 }
186
187 /**
188 * Override or insert variables into the node templates.
189 *
190 * @param $vars
191 * An array of variables to pass to the theme template.
192 * @param $hook
193 * The name of the template being rendered ("node" in this case.)
194 */
195 function zen_preprocess_node(&$vars, $hook) {
196 // Special classes for nodes
197 $classes = array('node');
198 if ($vars['sticky']) {
199 $classes[] = 'sticky';
200 }
201 if (!$vars['status']) {
202 $classes[] = 'node-unpublished';
203 $vars['unpublished'] = TRUE;
204 }
205 else {
206 $vars['unpublished'] = FALSE;
207 }
208 if ($vars['uid'] && $vars['uid'] == $GLOBALS['user']->uid) {
209 $classes[] = 'node-mine'; // Node is authored by current user.
210 }
211 if ($vars['teaser']) {
212 $classes[] = 'node-teaser'; // Node is displayed as teaser.
213 }
214 // Class for node type: "node-type-page", "node-type-story", "node-type-my-custom-type", etc.
215 $classes[] = zen_id_safe('node-type-' . $vars['type']);
216 $vars['classes'] = implode(' ', $classes); // Concatenate with spaces
217 }
218
219 /**
220 * Override or insert variables into the comment templates.
221 *
222 * @param $vars
223 * An array of variables to pass to the theme template.
224 * @param $hook
225 * The name of the template being rendered ("comment" in this case.)
226 */
227 function zen_preprocess_comment(&$vars, $hook) {
228 include_once './' . _zen_path() . '/template.comment.inc';
229 _zen_preprocess_comment($vars, $hook);
230 }
231
232 /**
233 * Override or insert variables into the block templates.
234 *
235 * @param $vars
236 * An array of variables to pass to the theme template.
237 * @param $hook
238 * The name of the template being rendered ("block" in this case.)
239 */
240 function zen_preprocess_block(&$vars, $hook) {
241 $block = $vars['block'];
242
243 // Special classes for blocks.
244 $classes = array('block');
245 $classes[] = 'block-' . $block->module;
246 $classes[] = 'region-' . $vars['block_zebra'];
247 $classes[] = $vars['zebra'];
248 $classes[] = 'region-count-' . $vars['block_id'];
249 $classes[] = 'count-' . $vars['id'];
250
251 $vars['edit_links_array'] = array();
252 $vars['edit_links'] = '';
253 if (theme_get_setting('zen_block_editing') && user_access('administer blocks')) {
254 include_once './' . _zen_path() . '/template.block-editing.inc';
255 zen_preprocess_block_editing($vars, $hook);
256 $classes[] = 'with-block-editing';
257 }
258
259 // Render block classes.
260 $vars['classes'] = implode(' ', $classes);
261 }
262
263 /**
264 * Converts a string to a suitable html ID attribute.
265 *
266 * http://www.w3.org/TR/html4/struct/global.html#h-7.5.2 specifies what makes a
267 * valid ID attribute in HTML. This function:
268 *
269 * - Ensure an ID starts with an alpha character by optionally adding an 'id'.
270 * - Replaces any character except alphanumeric characters with dashes.
271 * - Converts entire string to lowercase.
272 *
273 * @param $string
274 * The string
275 * @return
276 * The converted string
277 */
278 function zen_id_safe($string) {
279 // Replace with dashes anything that isn't A-Z, numbers, dashes, or underscores.
280 return strtolower(preg_replace('/[^a-zA-Z0-9-]+/', '-', $string));
281 }
282
283 /**
284 * Returns the path to the Zen theme.
285 *
286 * drupal_get_filename() is broken; see #341140. When that is fixed in Drupal 6,
287 * replace _zen_path() with drupal_get_path('theme', 'zen').
288 */
289 function _zen_path() {
290 static $path = FALSE;
291 if (!$path) {
292 $matches = drupal_system_listing('zen\.info$', 'themes', 'name', 0);
293 if (!empty($matches['zen']->filename)) {
294 $path = dirname($matches['zen']->filename);
295 }
296 }
297 return $path;
298 }