-
Notifications
You must be signed in to change notification settings - Fork 0
/
iu.theme
477 lines (425 loc) · 14.7 KB
/
iu.theme
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
<?php
/**
* @file
* Functions to support theming in the IU theme.
*/
use Drupal\block\Entity\Block;
use Drupal\Core\Site\Settings;
use Drupal\Core\Url;
use Drupal\Component\Utility\Html;
use Drupal\Core\StringTranslation\TranslatableMarkup;
/**
* Implements hook_page_attachments_alter().
*/
function iu_page_attachments_alter(array &$attachments) {
// Permit developers to load the dev version of the IU Framework dependency
// via a config override in settings.php or settings.local.php
// E.g. $settings['iu_framework_assets'] = 'dev';.
$prod_library = 'iu/iu-framework-prod';
$dev_library = 'iu/iu-framework-dev';
if (Settings::get('iu_framework_assets', 'prod') == 'dev') {
if (in_array($prod_library, $attachments['#attached']['library'])) {
$index = array_search($prod_library, $attachments['#attached']['library']);
unset($attachments['#attached']['library'][$index]);
$attachments['#attached']['library'][] = $dev_library;
}
}
}
/**
* Implements hook_preprocess_html().
*/
function iu_preprocess_html(&$variables) {
$secondary_color = theme_get_setting('secondary_color');
$color_class_mappings = _iu_secondary_color_palette_options();
$variables['attributes']['class'][] = !empty($color_class_mappings[$secondary_color]) ? $secondary_color : 'none';
$variables['attributes']['class'][] = 'landmarks';
}
/**
* Implements hook_preprocess_page().
*/
function iu_preprocess_page(&$variables) {
$off_canvas_position = theme_get_setting('off_canvas_position');
$variables['off_canvas_position'] = !empty($off_canvas_position) ? $off_canvas_position : 'right';
$campus = theme_get_setting('campus');
$variables['campus'] = !empty($campus) ? $campus : 'iub';
}
/**
* Implements hook_preprocess_maintenance_page().
*/
function iu_preprocess_maintenance_page(&$variables) {
// Setup maintenance page with same variables as normal page.
iu_preprocess_page($variables);
}
/**
* Implements hook_theme_suggestions_HOOK_alter() for blocks.
*/
function iu_theme_suggestions_block_alter(array &$suggestions, array $variables) {
// Add a template suggestion for blocks based on region and block type.
if (isset($variables['elements']['#id'])) {
$block_id = $variables['elements']['#id'];
$base_plugin_id = $variables['elements']['#base_plugin_id'];
$block = Block::load($block_id);
// Use the same templates for both system menu blocks and menu_block.
if ($base_plugin_id == 'system_menu_block') {
$base_plugin_id = 'menu_block';
}
if ($block) {
$region = $block->getRegion();
$suggestions[] = $variables['theme_hook_original'] . '__' . $region;
$suggestions[] = $variables['theme_hook_original'] . '__' . $region . '__' . $base_plugin_id;
}
}
}
/**
* Prepares variables for `block.html.twig`.
*/
function iu_preprocess_block(&$variables) {
// Pass the region name via a custom data attribute for usage in
// hook_preprocess_menu.
if (isset($variables['elements']['#id'])) {
$block_id = $variables['elements']['#id'];
$block = Block::load($block_id);
if ($block) {
$region = $block->getRegion();
$variables['content']['#attributes']['data-region'] = $region;
$block_class = '';
// Create an HTML class based on the menu ID.
if (
!empty($variables['elements']['#base_plugin_id']) &&
!empty($variables['elements']['#derivative_plugin_id']) &&
in_array($variables['elements']['#base_plugin_id'], [
'menu_block',
'system_menu_block',
])
) {
$menu_id = $variables['elements']['#derivative_plugin_id'];
// Add 'menu' to the id for clarity in CSS.
if (strpos($menu_id, 'menu') === FALSE) {
$menu_id = 'menu-' . $menu_id;
}
$block_class = Html::cleanCssIdentifier($menu_id);
}
// Otherwise, create an HTML class based on the block ID.
else {
$block_class = Html::cleanCssIdentifier($block_id);
}
if ($block_class) {
$variables['attributes']['class'][] = 'block-' . $block_class;
}
}
}
}
/**
* Prepares variables for `breadcrumb.html.twig`.
*/
function iu_preprocess_breadcrumb(&$variables) {
// Remove 'Home' from breadcrumb trail based on theme settings variable.
//
// Possible values:
// - 0: do not remove
// - 1: remove
// - 2: remove if its the only item.
$hide_home_breadcrumb = theme_get_setting('hide_home_breadcrumb');
if ($hide_home_breadcrumb == '1' && count($variables['breadcrumb'])) {
array_shift($variables['breadcrumb']);
}
elseif ($hide_home_breadcrumb == '2' && count($variables['breadcrumb']) == 1) {
array_shift($variables['breadcrumb']);
}
}
/**
* Prepares variables for `menu.html.twig`.
*/
function iu_preprocess_menu(&$variables) {
$menu_name = isset($variables['menu_name']) ? $variables['menu_name'] : '';
// If this menu is inside a menu_block, find the region the block belongs to
// then clean up after ourselves to remove the data attribute created in
// hook_preprocess_block.
$region = '';
if (!empty($variables['attributes']['data-region'])) {
$region = $variables['attributes']['data-region'];
unset($variables['attributes']['data-region']);
}
// Expose the region variable to the template.
$variables['region'] = $region;
// Special case triggers to hide first/last menu items on desktop, and make
// them icons on the mobile sticky menu.
$has_search = FALSE;
$has_home = FALSE;
if ($region == 'primary_menu') {
@$last = &end($variables['items']);
if ((!$last['url']->isRouted() && $last['url']->getUri() == '/search')
|| ($last['url']->isRouted() && $last['url']->getRouteName() == 'search.view')) {
$last['attributes']->addClass('show-on-sticky search');
$attrs = $last['url']->getOption('attributes');
$attrs['class'] = ['search-toggle'];
$last['url']->setOption('attributes', $attrs);
$has_search = TRUE;
}
@$first = &reset($variables['items']);
if ($first['title'] == 'Home' || ($first['url']->isRouted() && $first['url']->getRouteName() == '<front>')) {
$first['attributes']->addClass('show-on-sticky home');
$has_home = TRUE;
}
}
// Add first/last and two/two-left classes on all menu items.
$i = 1;
$count = count($variables['items']);
foreach ($variables['items'] as &$item) {
// The order of these if/elseifs are relevent, and prevent "last two" or
// "first two-left" appearing on the same element.
if (($i == $count && !$has_search) || ($i == ($count - 1) && $has_search)) {
$item['attributes']->addClass('last');
}
elseif (($i == 2 && !$has_home) || ($i == 3 && $has_home)) {
$item['attributes']->addClass('two');
}
if (($i == 1 && !$has_home) || ($i == 2 && $has_home)) {
$item['attributes']->addClass('first');
}
elseif (($i == ($count - 1) && !$has_search) || ($i == ($count - 2) && $has_search)) {
$item['attributes']->addClass('two-left');
}
$i++;
}
}
/**
* Prepares variables for `menu-local-task.html.twig`.
*/
function iu_preprocess_menu_local_task(&$variables) {
$variables['attributes']['class'][] = 'nav-item';
$variables['link']['#options']['attributes']['class'][] = 'nav-link' . (!empty($variables['is_active']) ? ' active' : '');
}
/**
* Prepares variables for `node.html.twig`.
*/
function iu_preprocess_node(&$variables) {
static $cacheable_metadata_list = [];
$blockRepository = \Drupal::service('block.repository');
/** @var \Drupal\block\BlockInterface[] $blocks */
$blocks = $blockRepository->getVisibleBlocksPerRegion($cacheable_metadata_list);
$variables['has_page_title'] = (isset($blocks['page_title']) && !empty($blocks['page_title']));
}
/**
* Prepares variables for `form.html.twig`.
*/
function iu_preprocess_form(&$variables) {
}
/**
* Prepares variables for `input.html.twig`.
*/
function iu_preprocess_input(&$variables) {
if (!empty($variables['element']['#button_type'])) {
switch ($variables['element']['#button_type']) {
case 'primary':
$variables['attributes']['class'][] = 'primary';
break;
case 'danger':
$variables['attributes']['class'][] = 'warning';
break;
}
}
$classes = &$variables['attributes']['class'];
if (!empty($classes) && in_array('field-add-more-submit', $classes)) {
$classes[] = 'small invert';
}
elseif (!empty($classes) && in_array('button', $classes)) {
// Get attribute value from TranslatableMarkup when instance
// is provided or raw value when is string.
$attributes = $variables['attributes'];
if ($attributes['value'] instanceof TranslatableMarkup) {
$attribute_value = $attributes['value']->getUntranslatedString();
}
else {
$attribute_value = $attributes['value'];
}
if ($attribute_value == 'Remove') {
$classes[] = 'button--remove small secondary hollow no-margin';
}
}
if (!empty($classes) && in_array('error', $classes)) {
$classes[] = 'is-invalid-input';
}
}
/**
* Prepares variables for `input--submit.html.twig`.
*/
function iu_preprocess_input__submit(&$variables) {
$classes = &$variables['attributes']['class'];
if (in_array('paragraph-type-add-modal-button', $classes)) {
$classes[] = 'small invert no-margin';
}
}
/**
* Prepares variables for `input--submit--paragraph-action.html.twig`.
*/
function iu_preprocess_input__submit__paragraph_action(&$variables) {
// var_dump($variables);
$classes = &$variables['attributes']['class'];
if (in_array('paragraphs-dropdown-action', $classes)) {
$classes[] = 'tiny secondary hollow no-margin';
}
if (in_array('paragraphs-icon-button', $classes)) {
$classes[] = 'tiny secondary hollow no-margin';
}
elseif (in_array('field-add-more-submit', $classes)) {
$classes[] = 'small invert';
}
}
/**
* Prepares variables for `pager.html.twig`.
*/
function iu_preprocess_pager(&$variables) {
// @todo backend developer code review of pager 'total' calculation
// Needs approval from someone familiar with Drupal pagers to determine
// whether this RegEx can be considered safe or not, or maybe there's a
// better way to figure this out.
if (!empty($variables['items']['last'])) {
if (preg_match('/(\d+)/', $variables['items']['last']['href'], $matches)) {
$variables['total'] = ((int) $matches[0]) + 1;
}
}
elseif (!empty($variables['current'])) {
$variables['total'] = $variables['current'];
}
else {
$variables['total'] = t('several');
}
}
/**
* Prepares variables for `webform-email-html.html.twig`.
*/
function iu_preprocess_webform_email_html(&$variables) {
// Prepare site and theme variables to pass along to the IU branded
// email-template.html.twig.
$campus = theme_get_setting('campus');
$variables['campus'] = !empty($campus) ? $campus : 'iub';
$variables['site_name'] = \Drupal::config('system.site')->get('name');
// Use the webform submission URL as the "View online" link.
$variables['online_version_url'] = $variables['webform_submission']->toUrl('canonical', ['absolute' => TRUE]);
// Expose the message array to the Twig template. Remove some array keys of
// elements that are already exposed to Twig. Why this is not exposed
// by default is beyond reason.
$handler = $variables['handler'];
$webform_submission = $variables['webform_submission'];
$message = $handler->getMessage($webform_submission);
unset($message['webform_submission']);
unset($message['handler']);
$variables['message'] = $message;
// Expose the sender and recipient info to the Twig template.
$variables['to_mail'] = $message['to_mail'];
$variables['from_mail'] = $message['from_mail'];
$variables['from_name'] = $message['from_name'];
// Expose the form name and id to the Twig template. Again, no idea why
// this is not available out of the box.
$webform = $handler->getWebform();
$variables['form_name'] = $webform->label();
$variables['form_id'] = $webform->id();
// Provide the absolute path to the current page where the Webform is
// embedded. We use Drupal path service and alias manager instead of just
// using Drupal::request()->getRequestUri() because the form submission
// may be done via ajax, which adds unnecessary parameters to the URL that
// should not be revealed in the notification emails.
$current_path = \Drupal::service('path.current')->getPath();
$path_alias = \Drupal::service('path.alias_manager')->getAliasByPath($current_path);
$variables['form_url'] = Url::fromUserInput($path_alias, ['absolute' => 'true'])->toString();
}
/**
* Helper to expose IU secondary color palette data.
*
* @see https://styleguide.iu.edu/visual-style/colors.html
*/
function _iu_secondary_color_palette_options() {
return [
'crimson' => [
'name' => 'Crimson',
'hex' => '#990000',
'class' => 'bg-crimson bg-dark',
'is_dark' => TRUE,
],
'gray' => [
'name' => 'Light Gray',
'hex' => '#edebeb',
'class' => 'bg-gray',
'is_dark' => FALSE,
],
'gold-dark' => [
'name' => 'Dark Gold',
'hex' => '#dc8823',
'class' => 'bg-gold-dark',
'is_dark' => FALSE,
],
'gold' => [
'name' => 'Gold',
'hex' => '#f1be48',
'class' => 'bg-gold',
'is_dark' => FALSE,
],
'mint-dark' => [
'name' => 'Dark Mint',
'hex' => '#285c4d',
'class' => 'bg-mint-dark bg-dark',
'is_dark' => TRUE,
],
'mint' => [
'name' => 'Mint',
'hex' => '#008264',
'class' => 'bg-mint bg-dark',
'is_dark' => TRUE,
],
'midnight-dark' => [
'name' => 'Dark Midnight',
'hex' => '#01426a',
'class' => 'bg-midnight-dark bg-dark',
'is_dark' => TRUE,
],
'midnight' => [
'name' => 'Midnight',
'hex' => '#006298',
'class' => 'bg-midnight bg-dark',
'is_dark' => TRUE,
],
'majestic-dark' => [
'name' => 'Dark Majestic',
'hex' => '#512a44',
'class' => 'bg-majestic-dark bg-dark',
'is_dark' => TRUE,
],
'majestic' => [
'name' => 'Majestic',
'hex' => '#66435a',
'class' => 'bg-majestic bg-dark',
'is_dark' => TRUE,
],
'limestone-dark' => [
'name' => 'Dark Limestone',
'hex' => '#83786f',
'class' => 'bg-limestone-dark bg-dark',
'is_dark' => TRUE,
],
'limestone' => [
'name' => 'Limestone',
'hex' => '#aca39a',
'class' => 'bg-limestone',
'is_dark' => FALSE,
],
'black' => [
'name' => 'Black',
'hex' => '#191919',
'class' => 'bg-black bg-dark',
'is_dark' => TRUE,
],
'mahogany' => [
'name' => 'Mahogany',
'hex' => '#4a3c31',
'class' => 'bg-mahogany bg-dark',
'is_dark' => TRUE,
],
'none' => [
'name' => 'None',
'hex' => '#fff',
'class' => 'bg-none',
'is_dark' => FALSE,
],
];
}