forked from govCMS/ui-kit-base-theme
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.php
More file actions
500 lines (420 loc) · 15.3 KB
/
Copy pathtemplate.php
File metadata and controls
500 lines (420 loc) · 15.3 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
<?php
/**
* @file
* Contains the theme's functions to manipulate Drupal's default markup.
*
* Complete documentation for this file is available online.
* @see https://drupal.org/node/1728096
*/
/** Core hooks ****************************************************************/
/**
* Implements THEME_form_alter().
*/
function uikit_base_form_alter(&$form, &$form_state, $form_id) {
// If this form is a search api form, we want to remove the size attribute
// on the text input, it makes styling difficult. We also update the
// placeholder and apply a class to thr form for targeting in JS.
if (strpos($form_id, 'search_api') !== FALSE) {
$search_api_form_id = $form['id']['#value'];
unset($form['keys_' . $search_api_form_id]['#size']);
$form['keys_' . $search_api_form_id]['#attributes']['placeholder'] = t('Search');
$form['#attributes']['class'] = 'search-form';
}
}
/** Core pre-process functions ************************************************/
/**
* Implements THEME_preprocess_field().
*/
function uikit_base_preprocess_field(&$variables) {
if ($variables['element']['#field_name'] == 'field_tags') {
$variables['classes_array'][] = 'tags';
}
}
/**
* Implements THEME_preprocess_form_element().
*/
function uikit_base_preprocess_form_element(&$variables) {
$variables['element']['#children'] = str_replace('required error', 'required error invalid', $variables['element']['#children']);
}
/**
* Implements THEME_preprocess_node().
*/
function uikit_base_preprocess_node(&$variables) {
// Add UI KIT class to author and date information.
$variables['submitted'] = '<div class="meta">' . t('Submitted by !author on !date', array('!date' => '<time>' . $variables['date'] .'</time>', '!author' => $variables['name']));
// Add UI KIT class to readmore link in teaser view mode.
if (!empty($variables['content']['links']['node']['#links']['node-readmore'])) {
$variables['content']['links']['node']['#links']['node-readmore']['attributes']['class'] = 'see-more';
}
}
/**
* Implements THEME_preprocess_page().
*/
function uikit_base_preprocess_page(&$variables) {
// Get classes for <main> together
$variables['main_classes'] = array('main');
// Position sidebar based on theme settings
if (theme_get_setting('sidebar_position') == 'left') {
$variables['main_classes'][] = 'sidebar-has-controls';
}
$variables['main_classes'] = implode(' ', $variables['main_classes']);
}
/**
* Implements THEME_preprocess_block().
*/
function uikit_base_preprocess_block(&$variables) {
// Add some classes to the block title and content wrapper
$variables['title_attributes_array']['class'] = 'block__title';
$variables['content_attributes_array']['class'] = 'block__content content';
// Drupal menu blocks, and Menu Block's blocks, share the same template file
// to apply the <nav> element.
if (in_array($variables['block']->module, array('menu', 'menu_block'))) {
array_unshift($variables['theme_hook_suggestions'], 'block__menu_generic');
}
}
/**
* Implements THEME_preprocess_region().
*/
function uikit_base_preprocess_region(&$variables) {
// Add UI KIT nav menu class
if ($variables['region'] == 'sidebar') {
$variables['classes_array'][] = 'local-nav';
}
// Pre-process the header region to combine block content and site branding
if ($variables['region'] == 'header') {
_uikit_base_preprocess_region_header($variables);
}
// Drop in the footer layout classes
if (in_array($variables['region'], array('footer_top', 'footer_bottom'))) {
$variables['classes_array'][] = 'region--' . theme_get_setting($variables['region'] . '_layout');
}
}
/** Theme functions ***********************************************************/
/**
* Implements THEME_breadcrumb().
*/
function uikit_base_breadcrumb($variables) {
$breadcrumb = $variables['breadcrumb'];
if (!empty($breadcrumb)) {
// Provide a navigational heading to give context for breadcrumb links to
// screen-reader users. Make the heading invisible with .element-invisible.
$output = '<h2 class="element-invisible">' . t('You are here') . '</h2>';
// Process breadcrumb for UI KIT format.
$breadcrumb_list = '<ul>';
foreach($breadcrumb as $link) {
$breadcrumb_list .= '<li>' . $link . '</li>';
}
$breadcrumb_list .= '</ul>';
// Add UI KIT tag and style to breadcrumb.
$output .= '<nav class="breadcrumbs" aria-label="breadcrumb"><div class="wrapper">' . $breadcrumb_list . '</div></nav>';
return $output;
}
}
/**
* Implements THEME_menu_local_tasks().
*/
function uikit_base_menu_local_tasks(&$variables) {
$output = '';
// Add UI KIT class to the tabs.
if (!empty($variables['primary'])) {
$variables['primary']['#prefix'] = '<h2 class="element-invisible">' . t('Primary tabs') . '</h2>';
$variables['primary']['#prefix'] .= '<nav class="inline-tab-nav"><ul class="tabs primary">';
$variables['primary']['#suffix'] = '</ul></nav>';
$output .= drupal_render($variables['primary']);
}
if (!empty($variables['secondary'])) {
$variables['secondary']['#prefix'] = '<h2 class="element-invisible">' . t('Secondary tabs') . '</h2>';
$variables['secondary']['#prefix'] .= '<nav class="inline-tab-nav"><ul class="tabs secondary">';
$variables['secondary']['#suffix'] = '</ul></nav>';
$output .= drupal_render($variables['secondary']);
}
// Process tabs.
$output = _uikit_base_process_local_tasks($output);
return $output;
}
/**
* Implements THEME_link().
*/
function uikit_base_link($variables) {
// Check link classes.
if (isset($variables['options']['attributes']['class'])) {
$classes = $variables['options']['attributes']['class'];
// Compose the class array if single string given.
if (!is_array($classes)) {
$classes = array($classes);
}
// The class pairs we need to add.
$class_pairs = array(
'active' => 'is-current',
'active-trail' => 'is-active',
);
// Add additional UI KIT classes.
$variables['options']['attributes']['class'] = _uikit_base_active_link($class_pairs, $classes);
}
// Default theme_link() function.
return '<a href="' . check_plain(url($variables['path'], $variables['options'])) . '"' .
drupal_attributes($variables['options']['attributes']) . '>' . ($variables['options']['html'] ?
$variables['text'] : check_plain($variables['text'])) . '</a>';
}
/**
* Implements THEME_pager().
*/
function uikit_base_pager($variables) {
$tags = $variables['tags'];
$element = $variables['element'];
$parameters = $variables['parameters'];
$quantity = $variables['quantity'];
global $pager_page_array, $pager_total;
// Calculate various markers within this pager piece:
// Middle is used to "center" pages around the current page.
$pager_middle = ceil($quantity / 2);
// current is the page we are currently paged to
$pager_current = $pager_page_array[$element] + 1;
// first is the first page listed by this pager piece (re quantity)
$pager_first = $pager_current - $pager_middle + 1;
// last is the last page listed by this pager piece (re quantity)
$pager_last = $pager_current + $quantity - $pager_middle;
// max is the maximum page number
$pager_max = $pager_total[$element];
// End of marker calculations.
// Prepare for generation loop.
$i = $pager_first;
if ($pager_last > $pager_max) {
// Adjust "center" if at end of query.
$i = $i + ($pager_max - $pager_last);
$pager_last = $pager_max;
}
if ($i <= 0) {
// Adjust "center" if at start of query.
$pager_last = $pager_last + (1 - $i);
$i = 1;
}
// End of generation loop preparation.
$li_first = theme('pager_first', array('text' => (isset($tags[0]) ? $tags[0] : t('« first')), 'element' => $element, 'parameters' => $parameters));
$li_previous = theme('pager_previous', array('text' => (isset($tags[1]) ? $tags[1] : t('‹ previous')), 'element' => $element, 'interval' => 1, 'parameters' => $parameters));
$li_next = theme('pager_next', array('text' => (isset($tags[3]) ? $tags[3] : t('next ›')), 'element' => $element, 'interval' => 1, 'parameters' => $parameters));
$li_last = theme('pager_last', array('text' => (isset($tags[4]) ? $tags[4] : t('last »')), 'element' => $element, 'parameters' => $parameters));
if ($pager_total[$element] > 1) {
if ($li_first) {
$items[] = array(
'class' => array('pager-first'),
'data' => $li_first,
);
}
if ($li_previous) {
$items[] = array(
'class' => array('pager-previous'),
'data' => $li_previous,
);
}
// When there is more than one page, create the pager list.
if ($i != $pager_max) {
if ($i > 1) {
$items[] = array(
'class' => array('pager-ellipsis'),
'data' => '…',
);
}
// Now generate the actual pager piece.
for (; $i <= $pager_last && $i <= $pager_max; $i++) {
if ($i < $pager_current) {
$items[] = array(
'class' => array('pager-item'),
'data' => theme('pager_previous', array('text' => $i, 'element' => $element, 'interval' => ($pager_current - $i), 'parameters' => $parameters)),
);
}
if ($i == $pager_current) {
$items[] = array(
'class' => array('pager-current'),
'data' => $i,
);
}
if ($i > $pager_current) {
$items[] = array(
'class' => array('pager-item'),
'data' => theme('pager_next', array('text' => $i, 'element' => $element, 'interval' => ($i - $pager_current), 'parameters' => $parameters)),
);
}
}
if ($i < $pager_max) {
$items[] = array(
'class' => array('pager-ellipsis'),
'data' => '…',
);
}
}
// End generation.
if ($li_next) {
$items[] = array(
'class' => array('pager-next'),
'data' => $li_next,
);
}
if ($li_last) {
$items[] = array(
'class' => array('pager-last'),
'data' => $li_last,
);
}
// Swap the core pager class with UI KIT inline-links class.
return '<h2 class="element-invisible">' . t('Pages') . '</h2>' . theme('item_list', array(
'items' => $items,
'attributes' => array('class' => array('inline-links')),
));
}
}
/**
* Implements THEME_status_messages().
*/
function uikit_base_status_messages($variables) {
$display = $variables['display'];
$output = '';
$status_heading = array(
'status' => t('Status message'),
'error' => t('Error message'),
'warning' => t('Warning message'),
);
// Map the UI Kit classes to drupal
$ui_kit_statuses = array(
'status' => 'callout--success',
'error' => 'callout--error',
'warning' => 'callout--warning',
);
foreach (drupal_get_messages($display) as $type => $messages) {
// Add UI KIT index-link class to the message div.
$output .= "<div class=\"messages $ui_kit_statuses[$type] index-links\">\n";
if (!empty($status_heading[$type])) {
$output .= '<h2 class="element-invisible">' . $status_heading[$type] . "</h2>\n";
}
if (count($messages) > 1) {
$output .= " <ul>\n";
foreach ($messages as $message) {
$output .= ' <li>' . $message . "</li>\n";
}
$output .= " </ul>\n";
}
else {
$output .= reset($messages);
}
$output .= "</div>\n";
}
return $output;
}
/** Contrib Theme functions ***************************************************/
/**
* Implement THEME_toc_filter().
*/
function uikit_base_toc_filter($variables) {
$output = '<a name="top" class="toc-filter-top"></a>';
// Add UI KIT content links class.
$output .= '<div class="index-links toc-filter toc-filter-' . $variables['type'] . '">';
$output .= '<div class="toc-filter-content">' . $variables['content'] . '</div>';
$output .= '</div>';
return $output;
}
/** Helper functions **********************************************************/
/**
* Helper function to add is-current class to the active link.
*
* @param $children
* The origin link html.
*
* @return mixed
* The processed link html.
*/
function _uikit_base_process_local_tasks($children) {
$output = str_replace('class="active"', 'class="active is-current"', $children);
return $output;
}
/**
* Pre-process the logo for uikit_base_preprocess_page().
*
* Turn the logo from a URL into an image within a link, and also scale it so
* that it's no taller than specified in the theme settings.
*
* This may be useful to users who do not have the ability to adjust the image
* size. It also allows the use of svg images where the ability set the image
* size to a maximum height is useful.
*
* This can be overridden in CSS at various breakpoints if required for those
* users who want to customise the theme.
*
* @param array $variables
*
* @see uikit_base_preprocess_page().
*/
function _uikit_base_preprocess_region_header(&$variables) {
$site_name = variable_get('site_name', '');
$site_slogan = variable_get('site_slogan', '');
$output = '';
// Do we want to show a logo?
if (theme_get_setting('toggle_logo')) {
$logo = theme_get_setting('logo');
// Attempt to get the width and height of the logo
$max_height = theme_get_setting('logo_max_height');
list($width, $height) = getimagesize($logo);
// If we're dealing with an SVG, the width and height will be null, so we set
// a height and get the browser to pick up the width.
if (is_null($width) && is_null($height)) {
$height = $max_height;
}
// Bitmap images will give us values
elseif ($height > $max_height) {
$ratio = $width / $height;
$height = $max_height;
$width = round($height * $ratio);
}
// Create the image using theme_image().
$logo = theme('image', array(
'path' => $logo,
'alt' => t('@site_name logo', array('@site_name' => $site_name)),
'title' => filter_xss($site_name),
'width' => $width,
'height' => $height,
));
// Inline styling to prevent SVG container from collapsing and making the
// logo smaller or distorting it.
$output .= '<div class="page-header__logo" style="min-width: ' . $width . 'px">';
$output .= $logo;
$output .= '</div>';
}
// Do we need to show additional info?
$show_site_name = theme_get_setting('toggle_name');
$show_site_slogan = theme_get_setting('toggle_slogan');
if ($show_site_name || (!empty($site_slogan) && $show_site_slogan)) {
$output .= '<div class="page-header__site-info">';
// Do we want to show a site name?
if ($show_site_name) {
$output .= '<h1>' . filter_xss($site_name) . '</h1>';
}
// Do we want to show a site slogan?
if (!empty($site_slogan) && $show_site_slogan) {
$output .= '<h2>' . filter_xss($site_slogan) . '</h2>';
}
$output .= '</div>';
}
$output .= '<div class="page-header__content">';
$output .= $variables['content'];
$output .= '</div>';
$variables['content'] = $output;
}
/*
* Helper function to add UI KIT link class to link.
*
* @param $class_pairs
* The pairs of Drupal class and UI KIT class.
*
* @param $classes
* Origin class array from Drupal.
*
* @return array
* Class array.
*/
function _uikit_base_active_link($class_pairs, $classes) {
foreach ($class_pairs as $needle => $additional_class) {
if (in_array($needle, $classes)) {
$classes[] = $additional_class;
}
}
return $classes;
}