Added $build_mode variable to node.tpl.php
[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 // Create the build_mode variable.
173 switch ($vars['node']->build_mode) {
174 case NODE_BUILD_NORMAL:
175 $vars['build_mode'] = $vars['teaser'] ? 'teaser' : 'full';
176 break;
177 case NODE_BUILD_PREVIEW:
178 $vars['build_mode'] = 'preview';
179 break;
180 case NODE_BUILD_SEARCH_INDEX:
181 $vars['build_mode'] = 'search_index';
182 break;
183 case NODE_BUILD_SEARCH_RESULT:
184 $vars['build_mode'] = 'search_result';
185 break;
186 case NODE_BUILD_RSS:
187 $vars['build_mode'] = 'rss';
188 break;
189 case NODE_BUILD_PRINT:
190 $vars['build_mode'] = 'print';
191 break;
192 }
193
194 // Special classes for nodes.
195 $classes = array('node');
196 if ($vars['sticky']) {
197 $classes[] = 'sticky';
198 }
199 if (!$vars['status']) {
200 $classes[] = 'node-unpublished';
201 $vars['unpublished'] = TRUE;
202 }
203 else {
204 $vars['unpublished'] = FALSE;
205 }
206 if ($vars['uid'] && $vars['uid'] == $GLOBALS['user']->uid) {
207 $classes[] = 'node-mine'; // Node is authored by current user.
208 }
209 if ($vars['teaser']) {
210 $classes[] = 'node-teaser'; // Node is displayed as teaser.
211 }
212 // Class for node type: "node-type-page", "node-type-story", "node-type-my-custom-type", etc.
213 $classes[] = zen_id_safe('node-type-' . $vars['type']);
214 $vars['classes_array'] = $classes;
215 $vars['classes'] = implode(' ', $classes); // Concatenate with spaces
216 }
217
218 /**
219 * Override or insert variables into the comment templates.
220 *
221 * @param $vars
222 * An array of variables to pass to the theme template.
223 * @param $hook
224 * The name of the template being rendered ("comment" in this case.)
225 */
226 function zen_preprocess_comment(&$vars, $hook) {
227 include_once './' . drupal_get_path('theme', 'zen') . '/zen-internals/template.comment.inc';
228 _zen_preprocess_comment($vars, $hook);
229 }
230
231 /**
232 * Override or insert variables into the block templates.
233 *
234 * @param $vars
235 * An array of variables to pass to the theme template.
236 * @param $hook
237 * The name of the template being rendered ("block" in this case.)
238 */
239 function zen_preprocess_block(&$vars, $hook) {
240 $block = $vars['block'];
241
242 // Special classes for blocks.
243 $classes = array('block');
244 $classes[] = 'block-' . $block->module;
245 $classes[] = 'region-' . $vars['block_zebra'];
246 $classes[] = $vars['zebra'];
247 $classes[] = 'region-count-' . $vars['block_id'];
248 $classes[] = 'count-' . $vars['id'];
249
250 $vars['edit_links_array'] = array();
251 $vars['edit_links'] = '';
252 if (theme_get_setting('zen_block_editing') && user_access('administer blocks')) {
253 include_once './' . drupal_get_path('theme', 'zen') . '/zen-internals/template.block-editing.inc';
254 zen_preprocess_block_editing($vars, $hook);
255 $classes[] = 'with-block-editing';
256 }
257
258 // Render block classes.
259 $vars['classes_array'] = $classes;
260 $vars['classes'] = implode(' ', $classes);
261 }
262
263 /**
264 * Converts a string to a suitable html ID attribute.
265 *
266 * http://www.w3.org/TR/html4/struct/global.html#h-7.5.2 specifies what makes a
267 * valid ID attribute in HTML. This function:
268 *
269 * - Ensure an ID starts with an alpha character by optionally adding an 'id'.
270 * - Replaces any character except alphanumeric characters with dashes.
271 * - Converts entire string to lowercase.
272 *
273 * @param $string
274 * The string
275 * @return
276 * The converted string
277 */
278 function zen_id_safe($string) {
279 // Replace with dashes anything that isn't A-Z, numbers, dashes, or underscores.
280 $string = strtolower(preg_replace('/[^a-zA-Z0-9-]+/', '-', $string));
281 // If the first character is not a-z, add 'id' in front.
282 if (!ctype_lower($string{0})) { // Don't use ctype_alpha since its locale aware.
283 $string = 'id' . $string;
284 }
285 return $string;
286 }