by JohnAlbin: code comment
[project/zen.git] / 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 // Rebuild .info data.
17 system_rebuild_theme_data();
18 // Rebuild theme registry.
19 drupal_theme_rebuild();
20 }
21
22
23 /**
24 * Implements HOOK_theme().
25 */
26 function zen_theme(&$existing, $type, $theme, $path) {
27 include_once './' . drupal_get_path('theme', 'zen') . '/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($variables) {
40 $breadcrumb = $variables['breadcrumb'];
41 // Determine if we are to display the breadcrumb.
42 $show_breadcrumb = theme_get_setting('zen_breadcrumb');
43 if ($show_breadcrumb == 'yes' || $show_breadcrumb == 'admin' && arg(0) == 'admin') {
44
45 // Optionally get rid of the homepage link.
46 $show_breadcrumb_home = theme_get_setting('zen_breadcrumb_home');
47 if (!$show_breadcrumb_home) {
48 array_shift($breadcrumb);
49 }
50
51 // Return the breadcrumb with separators.
52 if (!empty($breadcrumb)) {
53 $breadcrumb_separator = theme_get_setting('zen_breadcrumb_separator');
54 $trailing_separator = $title = '';
55 if (theme_get_setting('zen_breadcrumb_title')) {
56 $item = menu_get_item();
57 if (!empty($item['tab_parent'])) {
58 // If we are on a non-default tab, use the tab's title.
59 $title = check_plain($item['title']);
60 }
61 else {
62 $title = drupal_get_title();
63 }
64 if ($title) {
65 $trailing_separator = $breadcrumb_separator;
66 }
67 }
68 elseif (theme_get_setting('zen_breadcrumb_trailing')) {
69 $trailing_separator = $breadcrumb_separator;
70 }
71
72 // Provide a navigational heading to give context for breadcrumb links to
73 // screen-reader users. Make the heading invisible with .element-invisible.
74 $heading = '<h2 class="element-invisible">' . t('You are here') . '</h2>';
75
76 return $heading . '<div class="breadcrumb">' . implode($breadcrumb_separator, $breadcrumb) . $trailing_separator . $title . '</div>';
77 }
78 }
79 // Otherwise, return an empty string.
80 return '';
81 }
82
83 /**
84 * Duplicate of theme_menu_local_tasks() but adds clearfix to tabs.
85 */
86 function zen_menu_local_tasks(&$variables) {
87 $output = '';
88
89 if (!empty($variables['primary'])) {
90 $variables['primary']['#prefix'] = '<h2 class="element-invisible">' . t('Primary tabs') . '</h2>';
91 $variables['primary']['#prefix'] .= '<ul class="tabs primary clearfix">';
92 $variables['primary']['#suffix'] = '</ul>';
93 $output .= drupal_render($variables['primary']);
94 }
95 if (!empty($variables['secondary'])) {
96 $variables['secondary']['#prefix'] = '<h2 class="element-invisible">' . t('Secondary tabs') . '</h2>';
97 $variables['secondary']['#prefix'] .= '<ul class="tabs secondary clearfix">';
98 $variables['secondary']['#suffix'] = '</ul>';
99 $output .= drupal_render($variables['secondary']);
100 }
101
102 return $output;
103 }
104
105 /**
106 * Override or insert variables into theme_menu_local_task().
107 */
108 function zen_preprocess_menu_local_task(&$variables) {
109 $link =& $variables['element']['#link'];
110
111 // If the link does not contain HTML already, check_plain() it now.
112 // After we set 'html'=TRUE the link will not be sanitized by l().
113 if (empty($link['localized_options']['html'])) {
114 $link['title'] = check_plain($link['title']);
115 }
116 $link['localized_options']['html'] = TRUE;
117 $link['title'] = '<span class="tab">' . $link['title'] . '</span>';
118 }
119
120 /**
121 * Adds conditional CSS from the .info file.
122 *
123 * Copy of conditional_styles_preprocess_html().
124 */
125 function zen_add_conditional_styles() {
126 // Make a list of base themes and the current theme.
127 $themes = $GLOBALS['base_theme_info'];
128 $themes[] = $GLOBALS['theme_info'];
129 foreach (array_keys($themes) as $key) {
130 $theme_path = dirname($themes[$key]->filename) . '/';
131 if (isset($themes[$key]->info['stylesheets-conditional'])) {
132 foreach (array_keys($themes[$key]->info['stylesheets-conditional']) as $condition) {
133 foreach (array_keys($themes[$key]->info['stylesheets-conditional'][$condition]) as $media) {
134 foreach ($themes[$key]->info['stylesheets-conditional'][$condition][$media] as $stylesheet) {
135 // Add each conditional stylesheet.
136 drupal_add_css(
137 $theme_path . $stylesheet,
138 array(
139 'group' => CSS_THEME,
140 'browsers' => array(
141 'IE' => $condition,
142 '!IE' => FALSE,
143 ),
144 'every_page' => TRUE,
145 )
146 );
147 }
148 }
149 }
150 }
151 }
152 }
153
154 /**
155 * Override or insert variables into the html template.
156 *
157 * @param $vars
158 * An array of variables to pass to the theme template.
159 * @param $hook
160 * The name of the template being rendered ("html" in this case.)
161 */
162 function zen_preprocess_html(&$vars, $hook) {
163 // If the user is silly and enables Zen as the theme, add some styles.
164 if ($GLOBALS['theme'] == 'zen') {
165 include_once './' . drupal_get_path('theme', 'zen') . '/zen-internals/template.zen.inc';
166 _zen_preprocess_html($vars, $hook);
167 }
168 elseif (!module_exists('conditional_styles')) {
169 zen_add_conditional_styles();
170 }
171
172 // Classes for body element. Allows advanced theming based on context
173 // (home page, node of certain type, etc.)
174 if (!$vars['is_front']) {
175 // Add unique class for each page.
176 $path = drupal_get_path_alias($_GET['q']);
177 // Add unique class for each website section.
178 list($section, ) = explode('/', $path, 2);
179 if (arg(0) == 'node') {
180 if (arg(1) == 'add') {
181 $section = 'node-add';
182 }
183 elseif (is_numeric(arg(1)) && (arg(2) == 'edit' || arg(2) == 'delete')) {
184 $section = 'node-' . arg(2);
185 }
186 }
187 $vars['classes_array'][] = drupal_html_class('section-' . $section);
188 }
189 if (theme_get_setting('zen_wireframes')) {
190 $vars['classes_array'][] = 'with-wireframes'; // Optionally add the wireframes style.
191 }
192 // Store the menu item since it has some useful information.
193 $vars['menu_item'] = menu_get_item();
194 switch ($vars['menu_item']['page_callback']) {
195 case 'views_page':
196 // Is this a Views page?
197 $vars['classes_array'][] = 'page-views';
198 break;
199 case 'page_manager_page_execute':
200 case 'page_manager_node_view':
201 case 'page_manager_contact_site':
202 // Is this a Panels page?
203 $vars['classes_array'][] = 'page-panels';
204 break;
205 }
206 }
207
208 /**
209 * Override or insert variables into the maintenance page template.
210 *
211 * @param $vars
212 * An array of variables to pass to the theme template.
213 * @param $hook
214 * The name of the template being rendered ("maintenance_page" in this case.)
215 */
216 function zen_preprocess_maintenance_page(&$vars, $hook) {
217 // If Zen is the maintenance theme, add some styles.
218 if ($GLOBALS['theme'] == 'zen') {
219 include_once './' . drupal_get_path('theme', 'zen') . '/zen-internals/template.zen.inc';
220 _zen_preprocess_html($vars, $hook);
221 }
222 elseif (!module_exists('conditional_styles')) {
223 zen_add_conditional_styles();
224 }
225 }
226
227 /**
228 * Override or insert variables into the node templates.
229 *
230 * @param $vars
231 * An array of variables to pass to the theme template.
232 * @param $hook
233 * The name of the template being rendered ("node" in this case.)
234 */
235 function zen_preprocess_node(&$vars, $hook) {
236 if (isset($vars['node_title'])) {
237 // $node_title is idiotic. Fixed in Alpha 4.
238 $vars['title'] = $vars['node_title'];
239 }
240
241 // Special classes for nodes.
242 // Class for node type: "node-type-page", "node-type-story", "node-type-my-custom-type", etc.
243 $vars['classes_array'][] = drupal_html_class('node-type-' . $vars['type']);
244 if ($vars['promote']) {
245 $vars['classes_array'][] = 'node-promoted';
246 }
247 if ($vars['sticky']) {
248 $vars['classes_array'][] = 'node-sticky';
249 }
250 if (!$vars['status']) {
251 $vars['classes_array'][] = 'node-unpublished';
252 $vars['unpublished'] = TRUE;
253 }
254 else {
255 $vars['unpublished'] = FALSE;
256 }
257 if ($vars['uid'] && $vars['uid'] == $GLOBALS['user']->uid) {
258 $vars['classes_array'][] = 'node-by-viewer'; // Node is authored by current user.
259 }
260 if ($vars['teaser']) {
261 $vars['classes_array'][] = 'node-teaser'; // Node is displayed as teaser.
262 }
263 if (isset($vars['preview'])) {
264 $vars['classes_array'][] = 'node-preview';
265 }
266
267 $vars['title_attributes_array']['class'][] = 'node-title';
268 }
269
270 /**
271 * Override or insert variables into the comment templates.
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 ("comment" in this case.)
277 */
278 function zen_preprocess_comment(&$vars, $hook) {
279 // If comment subjects are disabled, don't display them.
280 if (variable_get('comment_subject_field_' . $vars['node']->type, 1) == 0) {
281 $vars['title'] = '';
282 }
283
284 // Zebra striping.
285 if ($vars['id'] == 1) {
286 $vars['classes_array'][] = 'first';
287 }
288 if ($vars['id'] == $vars['node']->comment_count) {
289 $vars['classes_array'][] = 'last';
290 }
291 $vars['classes_array'][] = $vars['zebra'];
292
293 $vars['title_attributes_array']['class'][] = 'comment-title';
294 }
295
296 /**
297 * Preprocess variables for region.tpl.php
298 *
299 * Prepare the values passed to the theme_region function to be passed into a
300 * pluggable template engine. Uses the region name to generate a template file
301 * suggestions. If none are found, the default region.tpl.php is used.
302 *
303 * @see region.tpl.php
304 */
305 function zen_preprocess_region(&$vars, $hook) {
306 // Sidebar regions get some extra classes and a common template suggestion.
307 if (strpos($vars['region'], 'sidebar_') === 0) {
308 $vars['classes_array'][] = 'column';
309 $vars['classes_array'][] = 'sidebar';
310 $vars['theme_hook_suggestions'][] = 'region__sidebar';
311 // Allow a region-specific template to override Zen's region--sidebar.
312 $vars['theme_hook_suggestions'][] = 'region__' . $vars['region'];
313 }
314 }
315
316 /**
317 * Override or insert variables into the block templates.
318 *
319 * @param $vars
320 * An array of variables to pass to the theme template.
321 * @param $hook
322 * The name of the template being rendered ("block" in this case.)
323 */
324 function zen_preprocess_block(&$vars, $hook) {
325 // Special classes for blocks.
326 $vars['classes_array'][] = 'region-' . $vars['block_zebra'];
327 $vars['classes_array'][] = 'region-count-' . $vars['block_id'];
328
329 $vars['title_attributes_array']['class'][] = 'block-title';
330 }
331
332 /**
333 * Override or insert variables into the block templates.
334 *
335 * @param $vars
336 * An array of variables to pass to the theme template.
337 * @param $hook
338 * The name of the template being rendered ("block" in this case.)
339 */
340 function zen_process_block(&$vars, $hook) {
341 // Drupal 7 should use a $title variable instead of $block->subject.
342 $vars['title'] = $vars['block']->subject;
343 }