Add missing Git attribution for contributor
[project/zen.git] / template.php
CommitLineData
0c91c6b4 1<?php
0c91c6b4 2/**
8c1acf80 3 * @file
0b1037f4 4 * Contains theme override functions and preprocess functions for the Zen theme.
7c5fa196 5 *
0b1037f4 6 * IMPORTANT WARNING: DO NOT MODIFY THIS FILE.
28511ba8
J
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
960bac1c
TS
12 */
13
89804871 14// Auto-rebuild the theme registry during theme development.
74cf7b61 15if (theme_get_setting('zen_rebuild_registry') && !defined('MAINTENANCE_MODE')) {
c37ef432 16 // Rebuild .info data.
e28f5335 17 system_rebuild_theme_data();
c37ef432
J
18 // Rebuild theme registry.
19 drupal_theme_rebuild();
89804871 20}
7c5fa196 21
719059ea 22
4f5c1d0c
J
23/**
24 * Implements HOOK_theme().
52521ba9 25 */
4f5c1d0c 26function zen_theme(&$existing, $type, $theme, $path) {
fb2d48c9 27 include_once './' . drupal_get_path('theme', 'zen') . '/zen-internals/template.theme-registry.inc';
4f5c1d0c
J
28 return _zen_theme($existing, $type, $theme, $path);
29}
52521ba9 30
7c5fa196
J
31/**
32 * Return a themed breadcrumb trail.
7081db43 33 *
3f537ac5
J
34 * @param $variables
35 * - title: An optional string to be used as a navigational heading to give
36 * context for breadcrumb links to screen-reader users.
37 * - title_attributes_array: Array of HTML attributes for the title. It is
38 * flattened into a string within the theme function.
39 * - breadcrumb: An array containing the breadcrumb links.
7c5fa196
J
40 * @return
41 * A string containing the breadcrumb output.
7081db43 42 */
fb2d48c9
J
43function zen_breadcrumb($variables) {
44 $breadcrumb = $variables['breadcrumb'];
261a11a7
J
45 $output = '';
46
d60c8c02 47 // Determine if we are to display the breadcrumb.
59794464 48 $show_breadcrumb = theme_get_setting('zen_breadcrumb');
52521ba9 49 if ($show_breadcrumb == 'yes' || $show_breadcrumb == 'admin' && arg(0) == 'admin') {
59794464 50
d60c8c02 51 // Optionally get rid of the homepage link.
59794464 52 $show_breadcrumb_home = theme_get_setting('zen_breadcrumb_home');
52521ba9 53 if (!$show_breadcrumb_home) {
52521ba9
J
54 array_shift($breadcrumb);
55 }
59794464 56
d60c8c02 57 // Return the breadcrumb with separators.
52521ba9 58 if (!empty($breadcrumb)) {
59794464 59 $breadcrumb_separator = theme_get_setting('zen_breadcrumb_separator');
d60c8c02
J
60 $trailing_separator = $title = '';
61 if (theme_get_setting('zen_breadcrumb_title')) {
660c4307
J
62 $item = menu_get_item();
63 if (!empty($item['tab_parent'])) {
f134138f 64 // If we are on a non-default tab, use the tab's title.
261a11a7 65 $breadcrumb[] = check_plain($item['title']);
660c4307
J
66 }
67 else {
261a11a7 68 $breadcrumb[] = drupal_get_title();
b6656287 69 }
d60c8c02
J
70 }
71 elseif (theme_get_setting('zen_breadcrumb_trailing')) {
72 $trailing_separator = $breadcrumb_separator;
73 }
95b795f9
J
74
75 // Provide a navigational heading to give context for breadcrumb links to
3f537ac5
J
76 // screen-reader users.
77 if (empty($variables['title'])) {
78 $variables['title'] = t('You are here');
79 }
80 // Unless overridden by a preprocess function, make the heading invisible.
81 if (!isset($variables['title_attributes_array']['class'])) {
82 $variables['title_attributes_array']['class'][] = 'element-invisible';
83 }
95b795f9 84
261a11a7
J
85 // Build the breadcrumb trail.
86 $output = '<nav class="breadcrumb">';
87 $output .= '<h2' . drupal_attributes($variables['title_attributes_array']) . '>' . $variables['title'] . '</h2>';
88 $output .= '<ul><li>' . implode($breadcrumb_separator . '</li><li>', $breadcrumb) . $trailing_separator . '</li></ul>';
89 $output .= '</nav>';
52521ba9 90 }
7c5fa196 91 }
261a11a7
J
92
93 return $output;
7c5fa196
J
94}
95
4f5c1d0c 96/**
fa28a506 97 * Override or insert variables into the html template.
87695e28 98 *
b6e4708e 99 * @param $variables
0b1037f4 100 * An array of variables to pass to the theme template.
e6f4a124 101 * @param $hook
500fee10
J
102 * The name of the template being rendered. This is usually "html", but can
103 * also be "maintenance_page" since zen_preprocess_maintenance_page() calls
104 * this function to have consistent variables.
87695e28 105 */
b6e4708e 106function zen_preprocess_html(&$variables, $hook) {
3a6e01fa 107 // Add variables and paths needed for HTML5 and responsive support.
c6e9e869 108 $variables['base_path'] = base_path();
dd468c11 109 $variables['path_to_zen'] = drupal_get_path('theme', 'zen');
3a6e01fa
J
110 $html5_respond_meta = theme_get_setting('zen_html5_respond_meta');
111 $variables['add_respond_js'] = in_array('respond', $html5_respond_meta);
112 $variables['add_html5_shim'] = in_array('html5', $html5_respond_meta);
113 $variables['add_responsive_meta'] = in_array('meta', $html5_respond_meta);
c6e9e869 114
78d8d215
J
115 // If the user is silly and enables Zen as the theme, add some styles.
116 if ($GLOBALS['theme'] == 'zen') {
dd468c11 117 include_once './' . $variables['path_to_zen'] . '/zen-internals/template.zen.inc';
b6e4708e 118 _zen_preprocess_html($variables, $hook);
78d8d215 119 }
719059ea 120
104f8e30
J
121 // Attributes for html element.
122 $variables['html_attributes_array'] = array(
123 'lang' => $variables['language']->language,
124 'dir' => $variables['language']->dir,
125 );
126
e0a8f494
J
127 // Classes for body element. Allows advanced theming based on context
128 // (home page, node of certain type, etc.)
500fee10 129 if (!$variables['is_front'] && $hook == 'html') {
363d1497 130 // Add unique class for each page.
9faf4798 131 $path = drupal_get_path_alias($_GET['q']);
363d1497 132 // Add unique class for each website section.
8357c010 133 list($section, ) = explode('/', $path, 2);
a2831350
J
134 $arg = explode('/', $_GET['q']);
135 if ($arg[0] == 'node') {
136 if ($arg[1] == 'add') {
363d1497 137 $section = 'node-add';
28511ba8 138 }
63231c44 139 elseif (isset($arg[2]) && is_numeric($arg[1]) && ($arg[2] == 'edit' || $arg[2] == 'delete')) {
a2831350 140 $section = 'node-' . $arg[2];
28511ba8
J
141 }
142 }
b6e4708e 143 $variables['classes_array'][] = drupal_html_class('section-' . $section);
960bac1c 144 }
2041a7c1 145 if (theme_get_setting('zen_wireframes')) {
b6e4708e 146 $variables['classes_array'][] = 'with-wireframes'; // Optionally add the wireframes style.
2041a7c1 147 }
711885f1 148 // Store the menu item since it has some useful information.
500fee10
J
149 if ($hook == 'html') {
150 $variables['menu_item'] = menu_get_item();
151 if ($variables['menu_item']) {
152 switch ($variables['menu_item']['page_callback']) {
153 case 'views_page':
154 // Is this a Views page?
155 $variables['classes_array'][] = 'page-views';
156 break;
157 case 'page_manager_page_execute':
158 case 'page_manager_node_view':
159 case 'page_manager_contact_site':
160 // Is this a Panels page?
161 $variables['classes_array'][] = 'page-panels';
162 break;
163 }
164 }
711885f1 165 }
aeec717d
J
166 $variables['skip_link_anchor'] = theme_get_setting('zen_skip_link_anchor');
167 $variables['skip_link_text'] = theme_get_setting('zen_skip_link_text');
e0a8f494 168}
7c5fa196 169
e0a8f494 170/**
104f8e30
J
171 * Override or insert variables into the html templates.
172 *
173 * @param $variables
174 * An array of variables to pass to the theme template.
175 * @param $hook
176 * The name of the template being rendered ("html" in this case.)
177 */
178function zen_process_html(&$variables, $hook) {
179 // Flatten out html_attributes.
180 $variables['html_attributes'] = drupal_attributes($variables['html_attributes_array']);
181}
182
183/**
abb4efe9
J
184 * Override or insert variables in the html_tag theme function.
185 */
186function zen_process_html_tag(&$variables) {
187 $tag = &$variables['element'];
188
189 if ($tag['#tag'] == 'style' || $tag['#tag'] == 'script') {
190 // Remove redundant type attribute and CDATA comments.
191 unset($tag['#attributes']['type'], $tag['#value_prefix'], $tag['#value_suffix']);
192
193 // Remove media="all" but leave others unaffected.
194 if (isset($tag['#attributes']['media']) && $tag['#attributes']['media'] === 'all') {
195 unset($tag['#attributes']['media']);
196 }
197 }
198}
199
200/**
1256823c
J
201 * Implement hook_html_head_alter().
202 */
203function zen_html_head_alter(&$head) {
204 // Simplify the meta tag for character encoding.
205 $head['system_meta_content_type']['#attributes'] = array('charset' => str_replace('text/html; charset=', '', $head['system_meta_content_type']['#attributes']['content']));
206}
207
208/**
551c3139
J
209 * Override or insert variables into the page template.
210 *
211 * @param $variables
212 * An array of variables to pass to the theme template.
213 * @param $hook
214 * The name of the template being rendered ("page" in this case.)
215 */
216function zen_preprocess_page(&$variables, $hook) {
217 // Find the title of the menu used by the secondary links.
218 $secondary_links = variable_get('menu_secondary_links_source', 'user-menu');
9d330c3b
J
219 if ($secondary_links) {
220 $menus = function_exists('menu_get_menus') ? menu_get_menus() : menu_list_system_menus();
221 $variables['secondary_menu_heading'] = $menus[$secondary_links];
222 }
223 else {
224 $variables['secondary_menu_heading'] = '';
225 }
551c3139
J
226}
227
228/**
3183d194
J
229 * Override or insert variables into the maintenance page template.
230 *
b6e4708e 231 * @param $variables
3183d194
J
232 * An array of variables to pass to the theme template.
233 * @param $hook
31c1fbc2 234 * The name of the template being rendered ("maintenance_page" in this case.)
3183d194 235 */
b6e4708e 236function zen_preprocess_maintenance_page(&$variables, $hook) {
500fee10 237 zen_preprocess_html($variables, $hook);
104f8e30
J
238}
239
240/**
241 * Override or insert variables into the maintenance page template.
242 *
243 * @param $variables
244 * An array of variables to pass to the theme template.
245 * @param $hook
246 * The name of the template being rendered ("maintenance_page" in this case.)
247 */
248function zen_process_maintenance_page(&$variables, $hook) {
500fee10 249 zen_process_html($variables, $hook);
3183d194
J
250}
251
252/**
0b1037f4 253 * Override or insert variables into the node templates.
e0a8f494 254 *
b6e4708e 255 * @param $variables
0b1037f4 256 * An array of variables to pass to the theme template.
e6f4a124 257 * @param $hook
0b1037f4 258 * The name of the template being rendered ("node" in this case.)
e0a8f494 259 */
b6e4708e 260function zen_preprocess_node(&$variables, $hook) {
e7ce78eb 261 // Add $unpublished variable.
b6e4708e 262 $variables['unpublished'] = (!$variables['status']) ? TRUE : FALSE;
911d5130 263
64d0d22d
J
264 // Add pubdate to submitted variable.
265 $variables['pubdate'] = '<time pubdate datetime="' . format_date($variables['node']->created, 'custom', 'c') . '">' . $variables['date'] . '</time>';
266 if ($variables['display_submitted']) {
267 $variables['submitted'] = t('Submitted by !username on !datetime', array('!username' => $variables['name'], '!datetime' => $variables['pubdate']));
268 }
269
bfea55ea
J
270 // Add a class for the view mode.
271 if (!$variables['teaser']) {
272 $variables['classes_array'][] = 'view-mode-' . $variables['view_mode'];
273 }
274
e7ce78eb 275 // Add a class to show node is authored by current user.
b6e4708e
J
276 if ($variables['uid'] && $variables['uid'] == $GLOBALS['user']->uid) {
277 $variables['classes_array'][] = 'node-by-viewer';
c91ed8a5 278 }
4e316b5a 279
b6e4708e 280 $variables['title_attributes_array']['class'][] = 'node-title';
e0a8f494 281}
7c5fa196 282
e0a8f494 283/**
0b1037f4 284 * Override or insert variables into the comment templates.
e0a8f494 285 *
b6e4708e 286 * @param $variables
0b1037f4 287 * An array of variables to pass to the theme template.
e6f4a124 288 * @param $hook
0b1037f4 289 * The name of the template being rendered ("comment" in this case.)
e0a8f494 290 */
b6e4708e 291function zen_preprocess_comment(&$variables, $hook) {
e8e12945 292 // If comment subjects are disabled, don't display them.
b6e4708e
J
293 if (variable_get('comment_subject_field_' . $variables['node']->type, 1) == 0) {
294 $variables['title'] = '';
e8e12945
J
295 }
296
64d0d22d
J
297 // Add pubdate to submitted variable.
298 $variables['pubdate'] = '<time pubdate datetime="' . format_date($variables['comment']->created, 'custom', 'c') . '">' . $variables['created'] . '</time>';
299 $variables['submitted'] = t('!username replied on !datetime', array('!username' => $variables['author'], '!datetime' => $variables['pubdate']));
300
e8e12945 301 // Zebra striping.
b6e4708e
J
302 if ($variables['id'] == 1) {
303 $variables['classes_array'][] = 'first';
e8e12945 304 }
b6e4708e
J
305 if ($variables['id'] == $variables['node']->comment_count) {
306 $variables['classes_array'][] = 'last';
e8e12945 307 }
b6e4708e 308 $variables['classes_array'][] = $variables['zebra'];
e8e12945 309
b6e4708e 310 $variables['title_attributes_array']['class'][] = 'comment-title';
960bac1c 311}
4dd0f1bf
JR
312
313/**
94f89279
J
314 * Preprocess variables for region.tpl.php
315 *
7f47e51d
J
316 * @param $variables
317 * An array of variables to pass to the theme template.
318 * @param $hook
319 * The name of the template being rendered ("region" in this case.)
94f89279 320 */
b6e4708e 321function zen_preprocess_region(&$variables, $hook) {
6b8155aa 322 // Sidebar regions get some extra classes and a common template suggestion.
b6e4708e
J
323 if (strpos($variables['region'], 'sidebar_') === 0) {
324 $variables['classes_array'][] = 'column';
325 $variables['classes_array'][] = 'sidebar';
6b8155aa 326 // Allow a region-specific template to override Zen's region--sidebar.
23e39483 327 array_unshift($variables['theme_hook_suggestions'], 'region__sidebar');
995d309c 328 }
67c4a567 329 // Use a template with no wrapper for the content region.
786931f7 330 elseif ($variables['region'] == 'content') {
23e39483
J
331 // Allow a region-specific template to override Zen's region--no-wrapper.
332 array_unshift($variables['theme_hook_suggestions'], 'region__no_wrapper');
786931f7 333 }
94f89279
J
334}
335
336/**
0b1037f4 337 * Override or insert variables into the block templates.
28511ba8 338 *
b6e4708e 339 * @param $variables
0b1037f4 340 * An array of variables to pass to the theme template.
e6f4a124 341 * @param $hook
0b1037f4 342 * The name of the template being rendered ("block" in this case.)
28511ba8 343 */
b6e4708e 344function zen_preprocess_block(&$variables, $hook) {
67c4a567 345 // Use a template with no wrapper for the page's main content.
786931f7 346 if ($variables['block_html_id'] == 'block-system-main') {
67c4a567 347 $variables['theme_hook_suggestions'][] = 'block__no_wrapper';
786931f7
J
348 }
349
288001f7 350 // Classes describing the position of the block within the region.
b6e4708e
J
351 if ($variables['block_id'] == 1) {
352 $variables['classes_array'][] = 'first';
288001f7
J
353 }
354 // The last_in_region property is set in zen_page_alter().
b6e4708e
J
355 if (isset($variables['block']->last_in_region)) {
356 $variables['classes_array'][] = 'last';
288001f7 357 }
b6e4708e 358 $variables['classes_array'][] = $variables['block_zebra'];
4e316b5a 359
b6e4708e 360 $variables['title_attributes_array']['class'][] = 'block-title';
94c5b270
J
361
362 // Add Aria Roles via attributes.
363 switch ($variables['block']->module) {
364 case 'system':
365 switch ($variables['block']->delta) {
366 case 'main':
367 // Note: the "main" role goes in the page.tpl, not here.
368 break;
369 case 'help':
370 case 'powered-by':
371 $variables['attributes_array']['role'] = 'complementary';
372 break;
373 default:
374 // Any other "system" block is a menu block.
375 $variables['attributes_array']['role'] = 'navigation';
376 break;
377 }
378 break;
379 case 'menu':
380 case 'menu_block':
381 case 'blog':
382 case 'book':
383 case 'comment':
384 case 'forum':
385 case 'shortcut':
386 case 'statistics':
387 $variables['attributes_array']['role'] = 'navigation';
388 break;
389 case 'search':
390 $variables['attributes_array']['role'] = 'search';
391 break;
392 case 'help':
393 case 'aggregator':
394 case 'locale':
395 case 'poll':
396 case 'profile':
397 $variables['attributes_array']['role'] = 'complementary';
398 break;
399 case 'node':
400 switch ($variables['block']->delta) {
401 case 'syndicate':
402 $variables['attributes_array']['role'] = 'complementary';
403 break;
404 case 'recent':
405 $variables['attributes_array']['role'] = 'navigation';
406 break;
407 }
408 break;
409 case 'user':
410 switch ($variables['block']->delta) {
411 case 'login':
412 $variables['attributes_array']['role'] = 'form';
413 break;
414 case 'new':
415 case 'online':
416 $variables['attributes_array']['role'] = 'complementary';
417 break;
418 }
419 break;
420 }
3183d194 421}
a3f7ecae
J
422
423/**
424 * Override or insert variables into the block templates.
425 *
b6e4708e 426 * @param $variables
a3f7ecae
J
427 * An array of variables to pass to the theme template.
428 * @param $hook
429 * The name of the template being rendered ("block" in this case.)
430 */
b6e4708e 431function zen_process_block(&$variables, $hook) {
a3f7ecae 432 // Drupal 7 should use a $title variable instead of $block->subject.
b6e4708e 433 $variables['title'] = $variables['block']->subject;
a3f7ecae 434}
288001f7
J
435
436/**
437 * Implements hook_page_alter().
438 *
439 * Look for the last block in the region. This is impossible to determine from
440 * within a preprocess_block function.
441 *
442 * @param $page
443 * Nested array of renderable elements that make up the page.
444 */
445function zen_page_alter(&$page) {
446 // Look in each visible region for blocks.
447 foreach (system_region_list($GLOBALS['theme'], REGIONS_VISIBLE) as $region => $name) {
448 if (!empty($page[$region])) {
449 // Find the last block in the region.
450 $blocks = array_reverse(element_children($page[$region]));
451 while ($blocks && !isset($page[$region][$blocks[0]]['#block'])) {
452 array_shift($blocks);
453 }
454 if ($blocks) {
455 $page[$region][$blocks[0]]['#block']->last_in_region = TRUE;
456 }
457 }
458 }
459}
e63ff516
J
460
461/**
462 * Implements hook_form_BASE_FORM_ID_alter().
463 *
464 * Prevent user-facing field styling from screwing up node edit forms by
465 * renaming the classes on the node edit form's field wrappers.
466 */
467function zen_form_node_form_alter(&$form, &$form_state, $form_id) {
468 // Remove if #1245218 is backported to D7 core.
469 foreach (array_keys($form) as $item) {
470 if (strpos($item, 'field_') === 0) {
471 if (!empty($form[$item]['#attributes']['class'])) {
472 foreach ($form[$item]['#attributes']['class'] as &$class) {
473 if (strpos($class, 'field-type-') === 0 || strpos($class, 'field-name-') === 0) {
474 // Make the class different from that used in theme_field().
475 $class = 'form-' . $class;
476 }
477 }
478 }
479 }
480 }
481}