Update zen-grids to 1.0.beta.2
[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') && !defined('MAINTENANCE_MODE')) {
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 $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.
40 * @return
41 * A string containing the breadcrumb output.
42 */
43 function zen_breadcrumb($variables) {
44 $breadcrumb = $variables['breadcrumb'];
45 $output = '';
46
47 // Determine if we are to display the breadcrumb.
48 $show_breadcrumb = theme_get_setting('zen_breadcrumb');
49 if ($show_breadcrumb == 'yes' || $show_breadcrumb == 'admin' && arg(0) == 'admin') {
50
51 // Optionally get rid of the homepage link.
52 $show_breadcrumb_home = theme_get_setting('zen_breadcrumb_home');
53 if (!$show_breadcrumb_home) {
54 array_shift($breadcrumb);
55 }
56
57 // Return the breadcrumb with separators.
58 if (!empty($breadcrumb)) {
59 $breadcrumb_separator = theme_get_setting('zen_breadcrumb_separator');
60 $trailing_separator = $title = '';
61 if (theme_get_setting('zen_breadcrumb_title')) {
62 $item = menu_get_item();
63 if (!empty($item['tab_parent'])) {
64 // If we are on a non-default tab, use the tab's title.
65 $breadcrumb[] = check_plain($item['title']);
66 }
67 else {
68 $breadcrumb[] = drupal_get_title();
69 }
70 }
71 elseif (theme_get_setting('zen_breadcrumb_trailing')) {
72 $trailing_separator = $breadcrumb_separator;
73 }
74
75 // Provide a navigational heading to give context for breadcrumb links to
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 }
84
85 // Build the breadcrumb trail.
86 $output = '<nav class="breadcrumb" role="navigation">';
87 $output .= '<h2' . drupal_attributes($variables['title_attributes_array']) . '>' . $variables['title'] . '</h2>';
88 $output .= '<ol><li>' . implode($breadcrumb_separator . '</li><li>', $breadcrumb) . $trailing_separator . '</li></ol>';
89 $output .= '</nav>';
90 }
91 }
92
93 return $output;
94 }
95
96 /**
97 * Override or insert variables into the html template.
98 *
99 * @param $variables
100 * An array of variables to pass to the theme template.
101 * @param $hook
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.
105 */
106 function zen_preprocess_html(&$variables, $hook) {
107 // Add variables and paths needed for HTML5 and responsive support.
108 $variables['base_path'] = base_path();
109 $variables['path_to_zen'] = drupal_get_path('theme', 'zen');
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);
114
115 // If the user is silly and enables Zen as the theme, add some styles.
116 if ($GLOBALS['theme'] == 'zen') {
117 include_once './' . $variables['path_to_zen'] . '/zen-internals/template.zen.inc';
118 _zen_preprocess_html($variables, $hook);
119 }
120
121 // Attributes for html element.
122 $variables['html_attributes_array'] = array(
123 'lang' => $variables['language']->language,
124 'dir' => $variables['language']->dir,
125 );
126
127 // Classes for body element. Allows advanced theming based on context
128 // (home page, node of certain type, etc.)
129 if (!$variables['is_front'] && $hook == 'html') {
130 // Add unique class for each page.
131 $path = drupal_get_path_alias($_GET['q']);
132 // Add unique class for each website section.
133 list($section, ) = explode('/', $path, 2);
134 $arg = explode('/', $_GET['q']);
135 if ($arg[0] == 'node') {
136 if ($arg[1] == 'add') {
137 $section = 'node-add';
138 }
139 elseif (isset($arg[2]) && is_numeric($arg[1]) && ($arg[2] == 'edit' || $arg[2] == 'delete')) {
140 $section = 'node-' . $arg[2];
141 }
142 }
143 $variables['classes_array'][] = drupal_html_class('section-' . $section);
144 }
145 if (theme_get_setting('zen_wireframes')) {
146 $variables['classes_array'][] = 'with-wireframes'; // Optionally add the wireframes style.
147 }
148 // Store the menu item since it has some useful information.
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 }
165 }
166 $variables['skip_link_anchor'] = theme_get_setting('zen_skip_link_anchor');
167 $variables['skip_link_text'] = theme_get_setting('zen_skip_link_text');
168 }
169
170 /**
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 */
178 function zen_process_html(&$variables, $hook) {
179 // Flatten out html_attributes.
180 $variables['html_attributes'] = drupal_attributes($variables['html_attributes_array']);
181 }
182
183 /**
184 * Override or insert variables in the html_tag theme function.
185 */
186 function 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 /**
201 * Implement hook_html_head_alter().
202 */
203 function 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 /**
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 */
216 function 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');
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 }
226 }
227
228 /**
229 * Override or insert variables into the maintenance page template.
230 *
231 * @param $variables
232 * An array of variables to pass to the theme template.
233 * @param $hook
234 * The name of the template being rendered ("maintenance_page" in this case.)
235 */
236 function zen_preprocess_maintenance_page(&$variables, $hook) {
237 zen_preprocess_html($variables, $hook);
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 */
248 function zen_process_maintenance_page(&$variables, $hook) {
249 zen_process_html($variables, $hook);
250 // Ensure default regions get a variable. Theme authors often forget to remove
251 // a deleted region's variable in maintenance-page.tpl.
252 foreach (array('header', 'navigation', 'highlighted', 'help', 'content', 'sidebar_first', 'sidebar_second', 'footer', 'bottom') as $region) {
253 if (!isset($variables[$region])) {
254 $variables[$region] = '';
255 }
256 }
257 }
258
259 /**
260 * Override or insert variables into the node templates.
261 *
262 * @param $variables
263 * An array of variables to pass to the theme template.
264 * @param $hook
265 * The name of the template being rendered ("node" in this case.)
266 */
267 function zen_preprocess_node(&$variables, $hook) {
268 // Add $unpublished variable.
269 $variables['unpublished'] = (!$variables['status']) ? TRUE : FALSE;
270
271 // Add pubdate to submitted variable.
272 $variables['pubdate'] = '<time pubdate datetime="' . format_date($variables['node']->created, 'custom', 'c') . '">' . $variables['date'] . '</time>';
273 if ($variables['display_submitted']) {
274 $variables['submitted'] = t('Submitted by !username on !datetime', array('!username' => $variables['name'], '!datetime' => $variables['pubdate']));
275 }
276
277 // Add a class for the view mode.
278 if (!$variables['teaser']) {
279 $variables['classes_array'][] = 'view-mode-' . $variables['view_mode'];
280 }
281
282 // Add a class to show node is authored by current user.
283 if ($variables['uid'] && $variables['uid'] == $GLOBALS['user']->uid) {
284 $variables['classes_array'][] = 'node-by-viewer';
285 }
286
287 $variables['title_attributes_array']['class'][] = 'node-title';
288 }
289
290 /**
291 * Override or insert variables into the comment templates.
292 *
293 * @param $variables
294 * An array of variables to pass to the theme template.
295 * @param $hook
296 * The name of the template being rendered ("comment" in this case.)
297 */
298 function zen_preprocess_comment(&$variables, $hook) {
299 // If comment subjects are disabled, don't display them.
300 if (variable_get('comment_subject_field_' . $variables['node']->type, 1) == 0) {
301 $variables['title'] = '';
302 }
303
304 // Add pubdate to submitted variable.
305 $variables['pubdate'] = '<time pubdate datetime="' . format_date($variables['comment']->created, 'custom', 'c') . '">' . $variables['created'] . '</time>';
306 $variables['submitted'] = t('!username replied on !datetime', array('!username' => $variables['author'], '!datetime' => $variables['pubdate']));
307
308 // Zebra striping.
309 if ($variables['id'] == 1) {
310 $variables['classes_array'][] = 'first';
311 }
312 if ($variables['id'] == $variables['node']->comment_count) {
313 $variables['classes_array'][] = 'last';
314 }
315 $variables['classes_array'][] = $variables['zebra'];
316
317 $variables['title_attributes_array']['class'][] = 'comment-title';
318 }
319
320 /**
321 * Preprocess variables for region.tpl.php
322 *
323 * @param $variables
324 * An array of variables to pass to the theme template.
325 * @param $hook
326 * The name of the template being rendered ("region" in this case.)
327 */
328 function zen_preprocess_region(&$variables, $hook) {
329 // Sidebar regions get some extra classes and a common template suggestion.
330 if (strpos($variables['region'], 'sidebar_') === 0) {
331 $variables['classes_array'][] = 'column';
332 $variables['classes_array'][] = 'sidebar';
333 // Allow a region-specific template to override Zen's region--sidebar.
334 array_unshift($variables['theme_hook_suggestions'], 'region__sidebar');
335 }
336 // Use a template with no wrapper for the content region.
337 elseif ($variables['region'] == 'content') {
338 // Allow a region-specific template to override Zen's region--no-wrapper.
339 array_unshift($variables['theme_hook_suggestions'], 'region__no_wrapper');
340 }
341 }
342
343 /**
344 * Override or insert variables into the block templates.
345 *
346 * @param $variables
347 * An array of variables to pass to the theme template.
348 * @param $hook
349 * The name of the template being rendered ("block" in this case.)
350 */
351 function zen_preprocess_block(&$variables, $hook) {
352 // Use a template with no wrapper for the page's main content.
353 if ($variables['block_html_id'] == 'block-system-main') {
354 $variables['theme_hook_suggestions'][] = 'block__no_wrapper';
355 }
356
357 // Classes describing the position of the block within the region.
358 if ($variables['block_id'] == 1) {
359 $variables['classes_array'][] = 'first';
360 }
361 // The last_in_region property is set in zen_page_alter().
362 if (isset($variables['block']->last_in_region)) {
363 $variables['classes_array'][] = 'last';
364 }
365 $variables['classes_array'][] = $variables['block_zebra'];
366
367 $variables['title_attributes_array']['class'][] = 'block-title';
368
369 // Add Aria Roles via attributes.
370 switch ($variables['block']->module) {
371 case 'system':
372 switch ($variables['block']->delta) {
373 case 'main':
374 // Note: the "main" role goes in the page.tpl, not here.
375 break;
376 case 'help':
377 case 'powered-by':
378 $variables['attributes_array']['role'] = 'complementary';
379 break;
380 default:
381 // Any other "system" block is a menu block.
382 $variables['attributes_array']['role'] = 'navigation';
383 break;
384 }
385 break;
386 case 'menu':
387 case 'menu_block':
388 case 'blog':
389 case 'book':
390 case 'comment':
391 case 'forum':
392 case 'shortcut':
393 case 'statistics':
394 $variables['attributes_array']['role'] = 'navigation';
395 break;
396 case 'search':
397 $variables['attributes_array']['role'] = 'search';
398 break;
399 case 'help':
400 case 'aggregator':
401 case 'locale':
402 case 'poll':
403 case 'profile':
404 $variables['attributes_array']['role'] = 'complementary';
405 break;
406 case 'node':
407 switch ($variables['block']->delta) {
408 case 'syndicate':
409 $variables['attributes_array']['role'] = 'complementary';
410 break;
411 case 'recent':
412 $variables['attributes_array']['role'] = 'navigation';
413 break;
414 }
415 break;
416 case 'user':
417 switch ($variables['block']->delta) {
418 case 'login':
419 $variables['attributes_array']['role'] = 'form';
420 break;
421 case 'new':
422 case 'online':
423 $variables['attributes_array']['role'] = 'complementary';
424 break;
425 }
426 break;
427 }
428 }
429
430 /**
431 * Override or insert variables into the block templates.
432 *
433 * @param $variables
434 * An array of variables to pass to the theme template.
435 * @param $hook
436 * The name of the template being rendered ("block" in this case.)
437 */
438 function zen_process_block(&$variables, $hook) {
439 // Drupal 7 should use a $title variable instead of $block->subject.
440 $variables['title'] = $variables['block']->subject;
441 }
442
443 /**
444 * Implements hook_page_alter().
445 *
446 * Look for the last block in the region. This is impossible to determine from
447 * within a preprocess_block function.
448 *
449 * @param $page
450 * Nested array of renderable elements that make up the page.
451 */
452 function zen_page_alter(&$page) {
453 // Look in each visible region for blocks.
454 foreach (system_region_list($GLOBALS['theme'], REGIONS_VISIBLE) as $region => $name) {
455 if (!empty($page[$region])) {
456 // Find the last block in the region.
457 $blocks = array_reverse(element_children($page[$region]));
458 while ($blocks && !isset($page[$region][$blocks[0]]['#block'])) {
459 array_shift($blocks);
460 }
461 if ($blocks) {
462 $page[$region][$blocks[0]]['#block']->last_in_region = TRUE;
463 }
464 }
465 }
466 }
467
468 /**
469 * Implements hook_form_BASE_FORM_ID_alter().
470 *
471 * Prevent user-facing field styling from screwing up node edit forms by
472 * renaming the classes on the node edit form's field wrappers.
473 */
474 function zen_form_node_form_alter(&$form, &$form_state, $form_id) {
475 // Remove if #1245218 is backported to D7 core.
476 foreach (array_keys($form) as $item) {
477 if (strpos($item, 'field_') === 0) {
478 if (!empty($form[$item]['#attributes']['class'])) {
479 foreach ($form[$item]['#attributes']['class'] as &$class) {
480 if (strpos($class, 'field-type-') === 0 || strpos($class, 'field-name-') === 0) {
481 // Make the class different from that used in theme_field().
482 $class = 'form-' . $class;
483 }
484 }
485 }
486 }
487 }
488 }