generated from backdrop-contrib/theme_template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
template.php
228 lines (200 loc) · 7.14 KB
/
template.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
/**
* @file
* MyTheme preprocess functions and theme function overrides.
*/
/*******************************************************************************
* Preprocess functions
******************************************************************************/
/**
* Prepares variables for page templates.
*
* @see page.tpl.php and derivatives.
*/
function d7compatible_preprocess_page(&$variables) {
// Add unique and section-based classes for each page.
$path = backdrop_get_path_alias($_GET['q']);
$unique_class = d7compatible_id_safe('page-' . $path);
$section_class = d7compatible_id_safe('page-' . arg(0));
if ($section_class != $unique_class) {
$variables['classes'][] = $section_class;
}
$variables['classes'][] = $unique_class;
// Add back legacy front/not-front classes.
if ($variables['is_front']) {
$variables['classes'][] = 'front';
}
else {
$variables['classes'][] = 'not-front';
}
}
/**
* Prepares variables for node templates.
*
* @see node.tpl.php and derivatives.
*/
function d7compatible_preprocess_node(&$variables) {
// Restore the legacy node ID as the css ID.
$variables['attributes']['id'] = 'node-' . $variables['node']->nid;
// Restore legacy `node-teaser` class.
if ($variables['view_mode'] == 'teaser') {
$variables['classes'][] = 'node-teaser';
}
// Restore classes to strings.
$variables['classes'] = implode(' ', $variables['classes']);
// Restore attributes to strings.
if (isset($variables['attributes'])) {
d7compatible_attributes_convert($variables['attributes']);
}
if (isset($variables['title_attributes'])) {
d7compatible_attributes_convert($variables['title_attributes']);
}
if ($variables['content_attributes']) {
d7compatible_attributes_convert($variables['content_attributes']);
}
// Add more template suggestions based on view mode.
if (!in_array($variables['view_mode'], array('full', 'teaser'))) {
$last = array_pop($variables['theme_hook_suggestions']);
$start = 'node__';
$view_mode = $variables['view_mode'];
$type = $variables['node']->type;
$variables['theme_hook_suggestions'][] = $start . $view_mode;
$variables['theme_hook_suggestions'][] = $start . $type . '__'. $view_mode;
$variables['theme_hook_suggestions'][] = $last;
}
}
/**
* Prepares variables for node templates.
*
* @see comment.tpl.php and derivatives.
*/
function d7compatible_preprocess_comment(&$variables) {
// Restore classes to strings.
$variables['classes'] = implode(' ', $variables['classes']);
// Restore attributes to strings.
if (isset($variables['attributes'])) {
d7compatible_attributes_convert($variables['attributes']);
}
if ($variables['content_attributes']) {
d7compatible_attributes_convert($variables['content_attributes']);
}
// Create D7 title attributes from B title classes.
$attributes = array(
'class' => $variables['title_classes'],
);
$variables['title_attributes'] = backdrop_attributes($attributes);
}
/**
* Prepare vairables for block templates.
*
* @see block.tpl.php and derivatives.
*/
function d7compatible_preprocess_block(&$variables) {
// Restore Drupal-7-style block-title class.
$variables['title_attributes']['class'][] = 'block-title';
// Restore Drupal-7-style legacy block ID.
$block = $variables['block'];
$block_class = backdrop_html_class('block-' . $block->module . '-' . (isset($block->childDelta) ? $block->childDelta : $block->delta));
$old_block_class = str_replace('--', '-', $block_class);
$variables['attributes']['id'] = $old_block_class;
$variables['header_menu'] = FALSE;
// Add the ID to the main menu block in the header.
if ($block->module == 'system' && $block->delta == 'main-menu') {
// Check the menu style to determine which is the header menu.
if (isset($block->settings['block_settings']['style']) && $block->settings['block_settings']['style'] == 'top_only') {
$variables['attributes']['id'] = 'navigation';
$variables['header_menu'] = TRUE;
}
}
// Restore classes to strings.
$variables['classes'] = implode(' ', $variables['classes']);
// Restore attributes to strings.
if (isset($variables['attributes'])) {
d7compatible_attributes_convert($variables['attributes']);
}
if (isset($variables['title_attributes'])) {
d7compatible_attributes_convert($variables['title_attributes']);
}
if ($variables['content_attributes']) {
d7compatible_attributes_convert($variables['content_attributes']);
}
}
/**
* Prepares variables for node templates.
*
* @see field.tpl.php and derivatives.
*/
function d7compatible_preprocess_field(&$variables) {
// Restore classes to strings.
$variables['classes'] = implode(' ', $variables['classes']);
// Restore attributes to strings.
if (isset($variables['attributes'])) {
d7compatible_attributes_convert($variables['attributes']);
}
if (isset($variables['title_attributes'])) {
d7compatible_attributes_convert($variables['title_attributes']);
}
if ($variables['content_attributes']) {
d7compatible_attributes_convert($variables['content_attributes']);
}
// Restore field item attributes to strings.
foreach ($variables['item_attributes'] as $delta => $item_attributes) {
d7compatible_attributes_convert($variables['item_attributes'][$delta]);
}
}
/**
* Overrides theme_menu_tree().
*/
function d7compatible_preprocess_menu_tree(&$variables) {
$links = element_children($variables['#tree']);
$first_key = reset($links);
// Add the ID `main-menu-links` to the UL tag for the main menu.
if ($variables['#tree'][$first_key]['#theme'] == 'menu_link__main_menu') {
$variables['attributes']['id'] = 'main-menu-links';
}
}
/*******************************************************************************
* Theme functions
******************************************************************************/
/**
* Overrides theme_breadcrumb().
*/
function d7compatible_breadcrumb($variables) {
$breadcrumb = $variables['breadcrumb'];
$output = '';
if (!empty($breadcrumb)) {
// Add useful aria-label for screen-readers.
$output .= '<nav role="navigation" class="breadcrumb" aria-label="Website Orientation">';
// Remove core's confusing you-are-here heading for screen-readers.
$output .= '<ol><li>' . implode(' » </li><li>', $breadcrumb) . '</li></ol>';
$output .= '</nav>';
}
return $output;
}
/*******************************************************************************
* Helper functions
******************************************************************************/
/**
* Helper function to restore attributes to strings.
*
* @param array $attributes
* Array of attributes common to Backdrop.
*
* @return string
* String of attributes as expected by Drupal.
*/
function d7compatible_attributes_convert(&$attributes = array()) {
if (!empty($attributes)) {
$attributes = backdrop_attributes($attributes);
}
else {
$attributes = '';
}
}
/**
* Helper function to create a HTML ID from any string.
*/
function d7compatible_id_safe($string) {
// Replace with dashes anything that isn't A-Z, numbers, dashes, or underscores.
return strtolower(preg_replace('/[^a-zA-Z0-9-]+/', '-', $string));
}