Make region__sidebar suggestion lower than region__NAME suggestion.
[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 // Determine if we are to display the breadcrumb.
46 $show_breadcrumb = theme_get_setting('zen_breadcrumb');
47 if ($show_breadcrumb == 'yes' || $show_breadcrumb == 'admin' && arg(0) == 'admin') {
48
49 // Optionally get rid of the homepage link.
50 $show_breadcrumb_home = theme_get_setting('zen_breadcrumb_home');
51 if (!$show_breadcrumb_home) {
52 array_shift($breadcrumb);
53 }
54
55 // Return the breadcrumb with separators.
56 if (!empty($breadcrumb)) {
57 $breadcrumb_separator = theme_get_setting('zen_breadcrumb_separator');
58 $trailing_separator = $title = '';
59 if (theme_get_setting('zen_breadcrumb_title')) {
60 $item = menu_get_item();
61 if (!empty($item['tab_parent'])) {
62 // If we are on a non-default tab, use the tab's title.
63 $title = check_plain($item['title']);
64 }
65 else {
66 $title = drupal_get_title();
67 }
68 if ($title) {
69 $trailing_separator = $breadcrumb_separator;
70 }
71 }
72 elseif (theme_get_setting('zen_breadcrumb_trailing')) {
73 $trailing_separator = $breadcrumb_separator;
74 }
75
76 // Provide a navigational heading to give context for breadcrumb links to
77 // screen-reader users.
78 if (empty($variables['title'])) {
79 $variables['title'] = t('You are here');
80 }
81 // Unless overridden by a preprocess function, make the heading invisible.
82 if (!isset($variables['title_attributes_array']['class'])) {
83 $variables['title_attributes_array']['class'][] = 'element-invisible';
84 }
85 $heading = '<h2' . drupal_attributes($variables['title_attributes_array']) . '>' . $variables['title'] . '</h2>';
86
87 return '<div class="breadcrumb">' . $heading . implode($breadcrumb_separator, $breadcrumb) . $trailing_separator . $title . '</div>';
88 }
89 }
90 // Otherwise, return an empty string.
91 return '';
92 }
93
94 /**
95 * Duplicate of theme_menu_local_tasks() but adds clearfix to tabs.
96 */
97 function zen_menu_local_tasks(&$variables) {
98 $output = '';
99
100 if ($primary = drupal_render($variables['primary'])) {
101 $output .= '<h2 class="element-invisible">' . t('Primary tabs') . '</h2>';
102 $output .= '<ul class="tabs primary clearfix">' . $primary . '</ul>';
103 }
104 if ($secondary = drupal_render($variables['secondary'])) {
105 $output .= '<h2 class="element-invisible">' . t('Secondary tabs') . '</h2>';
106 $output .= '<ul class="tabs secondary clearfix">' . $secondary . '</ul>';
107 }
108
109 return $output;
110 }
111
112 /**
113 * Override or insert variables into theme_menu_local_task().
114 */
115 function zen_preprocess_menu_local_task(&$variables) {
116 $link =& $variables['element']['#link'];
117
118 // If the link does not contain HTML already, check_plain() it now.
119 // After we set 'html'=TRUE the link will not be sanitized by l().
120 if (empty($link['localized_options']['html'])) {
121 $link['title'] = check_plain($link['title']);
122 }
123 $link['localized_options']['html'] = TRUE;
124 $link['title'] = '<span class="tab">' . $link['title'] . '</span>';
125 }
126
127 /**
128 * Adds conditional CSS from the .info file.
129 *
130 * Copy of conditional_styles_preprocess_html().
131 */
132 function zen_add_conditional_styles() {
133 // Make a list of base themes and the current theme.
134 $themes = $GLOBALS['base_theme_info'];
135 $themes[] = $GLOBALS['theme_info'];
136 foreach (array_keys($themes) as $key) {
137 $theme_path = dirname($themes[$key]->filename) . '/';
138 if (isset($themes[$key]->info['stylesheets-conditional'])) {
139 foreach (array_keys($themes[$key]->info['stylesheets-conditional']) as $condition) {
140 foreach (array_keys($themes[$key]->info['stylesheets-conditional'][$condition]) as $media) {
141 foreach ($themes[$key]->info['stylesheets-conditional'][$condition][$media] as $stylesheet) {
142 // Add each conditional stylesheet.
143 drupal_add_css(
144 $theme_path . $stylesheet,
145 array(
146 'group' => CSS_THEME,
147 'browsers' => array(
148 'IE' => $condition,
149 '!IE' => FALSE,
150 ),
151 'every_page' => TRUE,
152 )
153 );
154 }
155 }
156 }
157 }
158 }
159 }
160
161 /**
162 * Override or insert variables into the html template.
163 *
164 * @param $variables
165 * An array of variables to pass to the theme template.
166 * @param $hook
167 * The name of the template being rendered ("html" in this case.)
168 */
169 function zen_preprocess_html(&$variables, $hook) {
170 // If the user is silly and enables Zen as the theme, add some styles.
171 if ($GLOBALS['theme'] == 'zen') {
172 include_once './' . drupal_get_path('theme', 'zen') . '/zen-internals/template.zen.inc';
173 _zen_preprocess_html($variables, $hook);
174 }
175 elseif (!module_exists('conditional_styles')) {
176 zen_add_conditional_styles();
177 }
178
179 // Classes for body element. Allows advanced theming based on context
180 // (home page, node of certain type, etc.)
181 if (!$variables['is_front']) {
182 // Add unique class for each page.
183 $path = drupal_get_path_alias($_GET['q']);
184 // Add unique class for each website section.
185 list($section, ) = explode('/', $path, 2);
186 if (arg(0) == 'node') {
187 if (arg(1) == 'add') {
188 $section = 'node-add';
189 }
190 elseif (is_numeric(arg(1)) && (arg(2) == 'edit' || arg(2) == 'delete')) {
191 $section = 'node-' . arg(2);
192 }
193 }
194 $variables['classes_array'][] = drupal_html_class('section-' . $section);
195 }
196 if (theme_get_setting('zen_wireframes')) {
197 $variables['classes_array'][] = 'with-wireframes'; // Optionally add the wireframes style.
198 }
199 // Store the menu item since it has some useful information.
200 $variables['menu_item'] = menu_get_item();
201 switch ($variables['menu_item']['page_callback']) {
202 case 'views_page':
203 // Is this a Views page?
204 $variables['classes_array'][] = 'page-views';
205 break;
206 case 'page_manager_page_execute':
207 case 'page_manager_node_view':
208 case 'page_manager_contact_site':
209 // Is this a Panels page?
210 $variables['classes_array'][] = 'page-panels';
211 break;
212 }
213 $variables['jump_link_target'] = theme_get_setting('zen_jump_link_target');
214 $variables['jump_link_text'] = theme_get_setting('zen_jump_link_text');
215 }
216
217 /**
218 * Override or insert variables into the page template.
219 *
220 * @param $variables
221 * An array of variables to pass to the theme template.
222 * @param $hook
223 * The name of the template being rendered ("page" in this case.)
224 */
225 function zen_preprocess_page(&$variables, $hook) {
226 // Find the title of the menu used by the secondary links.
227 $secondary_links = variable_get('menu_secondary_links_source', 'user-menu');
228 if ($secondary_links) {
229 $menus = function_exists('menu_get_menus') ? menu_get_menus() : menu_list_system_menus();
230 $variables['secondary_menu_heading'] = $menus[$secondary_links];
231 }
232 else {
233 $variables['secondary_menu_heading'] = '';
234 }
235 }
236
237 /**
238 * Override or insert variables into the maintenance page template.
239 *
240 * @param $variables
241 * An array of variables to pass to the theme template.
242 * @param $hook
243 * The name of the template being rendered ("maintenance_page" in this case.)
244 */
245 function zen_preprocess_maintenance_page(&$variables, $hook) {
246 // If Zen is the maintenance theme, add some styles.
247 if ($GLOBALS['theme'] == 'zen') {
248 include_once './' . drupal_get_path('theme', 'zen') . '/zen-internals/template.zen.inc';
249 _zen_preprocess_html($variables, $hook);
250 }
251 elseif (!module_exists('conditional_styles')) {
252 zen_add_conditional_styles();
253 }
254 }
255
256 /**
257 * Override or insert variables into the node templates.
258 *
259 * @param $variables
260 * An array of variables to pass to the theme template.
261 * @param $hook
262 * The name of the template being rendered ("node" in this case.)
263 */
264 function zen_preprocess_node(&$variables, $hook) {
265 // Add $unpublished variable.
266 $variables['unpublished'] = (!$variables['status']) ? TRUE : FALSE;
267
268 // Add a class for the view mode.
269 if (!$variables['teaser']) {
270 $variables['classes_array'][] = 'view-mode-' . $variables['view_mode'];
271 }
272
273 // Add a class to show node is authored by current user.
274 if ($variables['uid'] && $variables['uid'] == $GLOBALS['user']->uid) {
275 $variables['classes_array'][] = 'node-by-viewer';
276 }
277
278 $variables['title_attributes_array']['class'][] = 'node-title';
279 }
280
281 /**
282 * Override or insert variables into the comment templates.
283 *
284 * @param $variables
285 * An array of variables to pass to the theme template.
286 * @param $hook
287 * The name of the template being rendered ("comment" in this case.)
288 */
289 function zen_preprocess_comment(&$variables, $hook) {
290 // If comment subjects are disabled, don't display them.
291 if (variable_get('comment_subject_field_' . $variables['node']->type, 1) == 0) {
292 $variables['title'] = '';
293 }
294
295 // Zebra striping.
296 if ($variables['id'] == 1) {
297 $variables['classes_array'][] = 'first';
298 }
299 if ($variables['id'] == $variables['node']->comment_count) {
300 $variables['classes_array'][] = 'last';
301 }
302 $variables['classes_array'][] = $variables['zebra'];
303
304 $variables['title_attributes_array']['class'][] = 'comment-title';
305 }
306
307 /**
308 * Preprocess variables for region.tpl.php
309 *
310 * @param $variables
311 * An array of variables to pass to the theme template.
312 * @param $hook
313 * The name of the template being rendered ("region" in this case.)
314 */
315 function zen_preprocess_region(&$variables, $hook) {
316 // Sidebar regions get some extra classes and a common template suggestion.
317 if (strpos($variables['region'], 'sidebar_') === 0) {
318 $variables['classes_array'][] = 'column';
319 $variables['classes_array'][] = 'sidebar';
320 // Allow a region-specific template to override Zen's region--sidebar.
321 array_unshift($variables['theme_hook_suggestions'], 'region__sidebar');
322 }
323 }
324
325 /**
326 * Override or insert variables into the block templates.
327 *
328 * @param $variables
329 * An array of variables to pass to the theme template.
330 * @param $hook
331 * The name of the template being rendered ("block" in this case.)
332 */
333 function zen_preprocess_block(&$variables, $hook) {
334 // Classes describing the position of the block within the region.
335 if ($variables['block_id'] == 1) {
336 $variables['classes_array'][] = 'first';
337 }
338 // The last_in_region property is set in zen_page_alter().
339 if (isset($variables['block']->last_in_region)) {
340 $variables['classes_array'][] = 'last';
341 }
342 $variables['classes_array'][] = $variables['block_zebra'];
343
344 $variables['title_attributes_array']['class'][] = 'block-title';
345 }
346
347 /**
348 * Override or insert variables into the block templates.
349 *
350 * @param $variables
351 * An array of variables to pass to the theme template.
352 * @param $hook
353 * The name of the template being rendered ("block" in this case.)
354 */
355 function zen_process_block(&$variables, $hook) {
356 // Drupal 7 should use a $title variable instead of $block->subject.
357 $variables['title'] = $variables['block']->subject;
358 }
359
360 /**
361 * Implements hook_page_alter().
362 *
363 * Look for the last block in the region. This is impossible to determine from
364 * within a preprocess_block function.
365 *
366 * @param $page
367 * Nested array of renderable elements that make up the page.
368 */
369 function zen_page_alter(&$page) {
370 // Look in each visible region for blocks.
371 foreach (system_region_list($GLOBALS['theme'], REGIONS_VISIBLE) as $region => $name) {
372 if (!empty($page[$region])) {
373 // Find the last block in the region.
374 $blocks = array_reverse(element_children($page[$region]));
375 while ($blocks && !isset($page[$region][$blocks[0]]['#block'])) {
376 array_shift($blocks);
377 }
378 if ($blocks) {
379 $page[$region][$blocks[0]]['#block']->last_in_region = TRUE;
380 }
381 }
382 }
383 }
384
385 /**
386 * Implements hook_form_BASE_FORM_ID_alter().
387 *
388 * Prevent user-facing field styling from screwing up node edit forms by
389 * renaming the classes on the node edit form's field wrappers.
390 */
391 function zen_form_node_form_alter(&$form, &$form_state, $form_id) {
392 // Remove if #1245218 is backported to D7 core.
393 foreach (array_keys($form) as $item) {
394 if (strpos($item, 'field_') === 0) {
395 if (!empty($form[$item]['#attributes']['class'])) {
396 foreach ($form[$item]['#attributes']['class'] as &$class) {
397 if (strpos($class, 'field-type-') === 0 || strpos($class, 'field-name-') === 0) {
398 // Make the class different from that used in theme_field().
399 $class = 'form-' . $class;
400 }
401 }
402 }
403 }
404 }
405 }