Render $edit_links_array in zen_process_block
[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 // When #341140 is fixed, replace _zen_path() with drupal_get_path().
27 include_once './' . _zen_path() . '/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($breadcrumb) {
40 // Determine if we are to display the breadcrumb.
41 $show_breadcrumb = theme_get_setting('zen_breadcrumb');
42 if ($show_breadcrumb == 'yes' || $show_breadcrumb == 'admin' && arg(0) == 'admin') {
43
44 // Optionally get rid of the homepage link.
45 $show_breadcrumb_home = theme_get_setting('zen_breadcrumb_home');
46 if (!$show_breadcrumb_home) {
47 array_shift($breadcrumb);
48 }
49
50 // Return the breadcrumb with separators.
51 if (!empty($breadcrumb)) {
52 $breadcrumb_separator = theme_get_setting('zen_breadcrumb_separator');
53 $trailing_separator = $title = '';
54 if (theme_get_setting('zen_breadcrumb_title')) {
55 $trailing_separator = $breadcrumb_separator;
56 $title = drupal_get_title();
57 }
58 elseif (theme_get_setting('zen_breadcrumb_trailing')) {
59 $trailing_separator = $breadcrumb_separator;
60 }
61 return '<div class="breadcrumb">' . implode($breadcrumb_separator, $breadcrumb) . "$trailing_separator$title</div>";
62 }
63 }
64 // Otherwise, return an empty string.
65 return '';
66 }
67
68 /**
69 * Implements theme_menu_item_link()
70 */
71 function zen_menu_item_link($link) {
72 if (empty($link['localized_options'])) {
73 $link['localized_options'] = array();
74 }
75
76 // If an item is a LOCAL TASK, render it as a tab
77 if ($link['type'] & MENU_IS_LOCAL_TASK) {
78 $link['title'] = '<span class="tab">' . check_plain($link['title']) . '</span>';
79 $link['localized_options']['html'] = TRUE;
80 }
81
82 return l($link['title'], $link['href'], $link['localized_options']);
83 }
84
85 /**
86 * Duplicate of theme_menu_local_tasks() but adds clear-block to tabs.
87 */
88 function zen_menu_local_tasks() {
89 $output = '';
90
91 if ($primary = menu_primary_local_tasks()) {
92 $output .= '<ul class="tabs primary clear-block">' . $primary . '</ul>';
93 }
94 if ($secondary = menu_secondary_local_tasks()) {
95 $output .= '<ul class="tabs secondary clear-block">' . $secondary . '</ul>';
96 }
97
98 return $output;
99 }
100
101
102 /**
103 * Override or insert variables into the page templates.
104 *
105 * @param $vars
106 * An array of variables to pass to the theme template.
107 * @param $hook
108 * The name of the template being rendered ("page" in this case.)
109 */
110 function zen_preprocess_page(&$vars, $hook) {
111 // If the user is silly and enables Zen as the theme, add some styles.
112 if ($GLOBALS['theme'] == 'zen') {
113 include_once './' . _zen_path() . '/zen-internals/template.zen.inc';
114 _zen_preprocess_page($vars, $hook);
115 }
116 // Add conditional stylesheets.
117 elseif (!module_exists('conditional_styles')) {
118 $vars['styles'] .= $vars['conditional_styles'] = variable_get('conditional_styles_' . $GLOBALS['theme'], '');
119 }
120
121 // Classes for body element. Allows advanced theming based on context
122 // (home page, node of certain type, etc.)
123 $classes = explode(' ', $vars['body_classes']);
124 // Remove the mostly useless page-ARG0 class.
125 if ($index = array_search(preg_replace('![^abcdefghijklmnopqrstuvwxyz0-9-_]+!s', '', 'page-'. drupal_strtolower(arg(0))), $classes)) {
126 unset($classes[$index]);
127 }
128 if (!$vars['is_front']) {
129 // Add unique class for each page.
130 $path = drupal_get_path_alias($_GET['q']);
131 $classes[] = zen_id_safe('page-' . $path);
132 // Add unique class for each website section.
133 list($section, ) = explode('/', $path, 2);
134 if (arg(0) == 'node') {
135 if (arg(1) == 'add') {
136 $section = 'node-add';
137 }
138 elseif (is_numeric(arg(1)) && (arg(2) == 'edit' || arg(2) == 'delete')) {
139 $section = 'node-' . arg(2);
140 }
141 }
142 $classes[] = zen_id_safe('section-' . $section);
143 }
144 if (theme_get_setting('zen_wireframes')) {
145 $classes[] = 'with-wireframes'; // Optionally add the wireframes style.
146 }
147 // Add new sidebar classes in addition to Drupal core's sidebar-* classes.
148 // This provides some backwards compatibility with Zen 6.x-1.x themes.
149 if ($vars['layout'] != 'both') {
150 $new_layout = ($vars['layout'] == 'left') ? 'first' : 'second';
151 if (array_search('sidebar-' . $vars['layout'], $classes)) {
152 $classes[] = 'sidebar-' . $new_layout;
153 }
154 // Replace core's $layout variable with our naming of sidebars.
155 $vars['layout'] = $new_layout;
156 }
157 $vars['body_classes_array'] = $classes;
158 $vars['body_classes'] = implode(' ', $classes); // Concatenate with spaces.
159 }
160
161 /**
162 * Override or insert variables into the maintenance page template.
163 *
164 * @param $vars
165 * An array of variables to pass to the theme template.
166 * @param $hook
167 * The name of the template being rendered ("maintenance_page" in this case.)
168 */
169 function zen_preprocess_maintenance_page(&$vars, $hook) {
170 // If Zen is the maintenance theme, add some styles.
171 if ($GLOBALS['theme'] == 'zen') {
172 include_once './' . _zen_path() . '/zen-internals/template.zen.inc';
173 _zen_preprocess_page($vars, $hook);
174 }
175 // Add conditional stylesheets.
176 elseif (!module_exists('conditional_styles')) {
177 $vars['styles'] .= $vars['conditional_styles'] = variable_get('conditional_styles_' . $GLOBALS['theme'], '');
178 }
179
180 // Classes for body element. Allows advanced theming based on context
181 // (home page, node of certain type, etc.)
182 $vars['body_classes_array'] = explode(' ', $vars['body_classes']);
183 }
184
185 /**
186 * Override or insert variables into the node templates.
187 *
188 * @param $vars
189 * An array of variables to pass to the theme template.
190 * @param $hook
191 * The name of the template being rendered ("node" in this case.)
192 */
193 function zen_preprocess_node(&$vars, $hook) {
194 // Create the build_mode variable.
195 switch ($vars['node']->build_mode) {
196 case NODE_BUILD_NORMAL:
197 $vars['build_mode'] = $vars['teaser'] ? 'teaser' : 'full';
198 break;
199 case NODE_BUILD_PREVIEW:
200 $vars['build_mode'] = 'preview';
201 break;
202 case NODE_BUILD_SEARCH_INDEX:
203 $vars['build_mode'] = 'search_index';
204 break;
205 case NODE_BUILD_SEARCH_RESULT:
206 $vars['build_mode'] = 'search_result';
207 break;
208 case NODE_BUILD_RSS:
209 $vars['build_mode'] = 'rss';
210 break;
211 case NODE_BUILD_PRINT:
212 $vars['build_mode'] = 'print';
213 break;
214 }
215
216 // Create the user_picture variable.
217 $vars['user_picture'] = $vars['picture'];
218
219 // Special classes for nodes.
220 $classes = array('node');
221 // Class for node type: "node-type-page", "node-type-story", "node-type-my-custom-type", etc.
222 $classes[] = zen_id_safe('node-type-' . $vars['type']);
223 if ($vars['promote']) {
224 $vars['classes_array'][] = 'node-promoted';
225 }
226 if ($vars['sticky']) {
227 $classes[] = 'node-sticky';
228 }
229 if (!$vars['status']) {
230 $classes[] = 'node-unpublished';
231 $vars['unpublished'] = TRUE;
232 }
233 else {
234 $vars['unpublished'] = FALSE;
235 }
236 if ($vars['uid'] && $vars['uid'] == $GLOBALS['user']->uid) {
237 $classes[] = 'node-mine'; // Node is authored by current user.
238 }
239 if ($vars['teaser']) {
240 $classes[] = 'node-teaser'; // Node is displayed as teaser.
241 }
242 if (isset($vars['preview'])) {
243 $vars['classes_array'][] = 'node-preview';
244 }
245 $vars['classes_array'] = $classes;
246 }
247
248 /**
249 * Override or insert variables into the comment templates.
250 *
251 * @param $vars
252 * An array of variables to pass to the theme template.
253 * @param $hook
254 * The name of the template being rendered ("comment" in this case.)
255 */
256 function zen_preprocess_comment(&$vars, $hook) {
257 include_once './' . _zen_path() . '/zen-internals/template.comment.inc';
258 _zen_preprocess_comment($vars, $hook);
259 }
260
261 /**
262 * Override or insert variables into the block templates.
263 *
264 * @param $vars
265 * An array of variables to pass to the theme template.
266 * @param $hook
267 * The name of the template being rendered ("block" in this case.)
268 */
269 function zen_preprocess_block(&$vars, $hook) {
270 $block = $vars['block'];
271
272 // Special classes for blocks.
273 $classes = array('block');
274 $classes[] = 'block-' . $block->module;
275 $classes[] = 'region-' . $vars['block_zebra'];
276 $classes[] = $vars['zebra'];
277 $classes[] = 'region-count-' . $vars['block_id'];
278 $classes[] = 'count-' . $vars['id'];
279
280 $vars['edit_links_array'] = array();
281 if (theme_get_setting('zen_block_editing') && user_access('administer blocks')) {
282 include_once './' . _zen_path() . '/zen-internals/template.block-editing.inc';
283 zen_preprocess_block_editing($vars, $hook);
284 $classes[] = 'with-block-editing';
285 }
286
287 // Render block classes.
288 $vars['classes_array'] = $classes;
289 }
290
291 /**
292 * Override or insert variables into templates after preprocess functions have run.
293 *
294 * @param $vars
295 * An array of variables to pass to the theme template.
296 * @param $hook
297 * The name of the template being rendered.
298 */
299 function zen_process(&$vars, $hook) {
300 switch ($hook) {
301 case 'page':
302 case 'maintenance_page':
303 $vars['body_classes'] = !empty($vars['body_classes_array']) ? implode(' ', $vars['body_classes_array']) : '';
304 break;
305 default:
306 $vars['classes'] = !empty($vars['classes_array']) ? implode(' ', $vars['classes_array']) : '';
307 break;
308 }
309 }
310
311 /**
312 * Override or insert variables into the block templates after preprocess functions have run.
313 *
314 * @param $vars
315 * An array of variables to pass to the theme template.
316 * @param $hook
317 * The name of the template being rendered ("block" in this case.)
318 */
319 function zen_process_block(&$vars, $hook) {
320 $vars['edit_links'] = !empty($vars['edit_links_array']) ? '<div class="edit">' . implode(' ', $vars['edit_links_array']) . '</div>' : '';
321 }
322
323 /**
324 * Converts a string to a suitable html ID attribute.
325 *
326 * http://www.w3.org/TR/html4/struct/global.html#h-7.5.2 specifies what makes a
327 * valid ID attribute in HTML. This function:
328 *
329 * - Ensure an ID starts with an alpha character by optionally adding an 'id'.
330 * - Replaces any character except alphanumeric characters with dashes.
331 * - Converts entire string to lowercase.
332 *
333 * @param $string
334 * The string
335 * @return
336 * The converted string
337 */
338 function zen_id_safe($string) {
339 // Replace with dashes anything that isn't A-Z, numbers, dashes, or underscores.
340 $string = strtolower(preg_replace('/[^a-zA-Z0-9-]+/', '-', $string));
341 // If the first character is not a-z, add 'id' in front.
342 if (!ctype_lower($string{0})) { // Don't use ctype_alpha since its locale aware.
343 $string = 'id' . $string;
344 }
345 return $string;
346 }
347
348 /**
349 * Returns the path to the Zen theme.
350 *
351 * drupal_get_filename() is broken; see #341140. When that is fixed in Drupal 6,
352 * replace _zen_path() with drupal_get_path('theme', 'zen').
353 */
354 function _zen_path() {
355 static $path = FALSE;
356 if (!$path) {
357 $matches = drupal_system_listing('zen\.info$', 'themes', 'name', 0);
358 if (!empty($matches['zen']->filename)) {
359 $path = dirname($matches['zen']->filename);
360 }
361 }
362 return $path;
363 }