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