-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrainsum_starterkit.theme
600 lines (531 loc) · 19.1 KB
/
brainsum_starterkit.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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
<?php
/**
* @file
* Functions to support theming in the BRAINSUM's Drupal 10 starterkit.
*/
use Drupal\Component\Utility\Html;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Template\Attribute;
use Drupal\node\NodeInterface;
/**
* Implements hook_theme_registry_alter().
*
* Use our version of toolbar.html.twig template instead of Claro's version.
*/
function brainsum_starterkit_theme_registry_alter(&$theme_registry) {
$theme_registry['toolbar']['path'] = \Drupal::request()->getBasePath() . '/' .
\Drupal::service('extension.list.theme')->getPath('brainsum_starterkit') .
'/templates/navigation';
}
/**
* Implements hook_preprocess_HOOK() for HTML document templates.
*
* Adds body classes if certain regions have content.
*/
function brainsum_starterkit_preprocess_html(&$variables) {
$path = \Drupal::request()->getPathInfo();
$path_args = explode('/', $path);
$language = \Drupal::languageManager()->getCurrentLanguage()->getId();
// Store theme's path in a variable to access it from any Twig templates.
$variables['current_theme_path'] = \Drupal::request()->getBasePath() . '/' .
\Drupal::service('extension.list.theme')->getPath('brainsum_starterkit');
$variables['path_info']['args'] = FALSE;
// Generating class name from current path and added to html element.
if (count($path_args) >= 3) {
$variables['path_info']['args'] = Html::cleanCssIdentifier(ltrim($path, '/'));
}
// Added current ID as class to html element.
if ($node = \Drupal::request()->attributes->get('node')) {
$variables['attributes']['class'][] = 'page-node-' . $node->id();
}
// Added a class to html element according to current language.
$variables['attributes']['class'][] = 'page--language-' . $language;
}
/**
* Implements hook_preprocess_HOOK() for page title templates.
*/
function brainsum_starterkit_preprocess_page_title(&$variables) {
// Since the title and the shortcut link are both block level elements,
// positioning them next to each other is much simpler with a wrapper div.
if (
!empty($variables['title_suffix']['add_or_remove_shortcut']) && $variables['title']
) {
// Add a wrapper div using the title_prefix and title_suffix render
// elements.
$variables['title_prefix']['shortcut_wrapper'] = [
'#markup' => '<div class="c-shortcut__wrapper">',
'#weight' => 100,
];
$variables['title_suffix']['shortcut_wrapper'] = [
'#markup' => '</div>',
'#weight' => -99,
];
// Make sure the shortcut link is the first item in title_suffix.
$variables['title_suffix']['add_or_remove_shortcut']['#weight'] = -100;
}
// Unset shortcut link on front page.
$variables['is_front'] = \Drupal::service('path.matcher')->isFrontPage();
if ($variables['is_front'] === TRUE) {
unset($variables['title_suffix']['add_or_remove_shortcut']);
}
}
/**
* Implements hook_preprocess_HOOK() for maintenance-page.html.twig.
*/
function brainsum_starterkit_preprocess_maintenance_page(&$variables) {
// By default, site_name is set to Drupal if no db connection is available
// or during site installation. Setting site_name to an empty string makes
// the site and update pages look cleaner.
// @see template_preprocess_maintenance_page
if (!$variables['db_is_active']) {
$variables['site_name'] = '';
}
// Startertheme has custom styling for the maintenance page.
$variables['#attached']['library'][] = 'brainsum_starterkit/page.maintenance';
}
/**
* Implements hook_preprocess_HOOK() for node.html.twig.
*/
function brainsum_starterkit_preprocess_node(&$variables) {
// Remove the "Add new comment" link on teasers or when the comment form is
// displayed on the page.
if (
$variables['teaser'] || !empty(
$variables['content']['comments']['comment_form']
)
) {
unset($variables['content']['links']['comment']['#links']['comment-add']);
}
}
/**
* Implements hook_preprocess_HOOK() for page templates.
*/
function brainsum_starterkit_suggestions_page_alter(
array &$suggestions,
array $variables,
) {
$node = \Drupal::routeMatch()->getParameter('node');
if ($node instanceof NodeInterface) {
$suggestions[] = 'page__' . $node->bundle();
$suggestions[] = 'page__node_' . $node->bundle();
}
}
/**
* Implements hook_preprocess_page() for PAGE document templates.
*/
function brainsum_starterkit_preprocess_page(array &$variables) {
// Provide the path as a variable to the page template.
$path = \Drupal::request()->getPathInfo();
$trimmed_path = Html::cleanCssIdentifier(ltrim($path, '/'));
$variables['path_info'] = $trimmed_path;
if ($variables['logged_in'] == TRUE) {
$variables['#attached']['library'][] = 'brainsum_starterkit/admin.toolbar';
}
}
/**
* Implements hook_theme_suggestions_HOOK_alter() for menu.
*/
function brainsum_starterkit_theme_suggestions_menu_alter(&$suggestions, array $variables) {
if (isset($variables['attributes']['region'])) {
$suggestions[] = 'menu__' . $variables['attributes']['region'];
}
}
/**
* Implements hook_theme_suggestions_taxonomy_term_alter().
*/
function brainsum_starterkit_theme_suggestions_taxonomy_term_alter(
array &$suggestions,
array $variables,
) {
/** @var \Drupal\taxonomy\TermInterface $term */
$term = $variables['elements']['#taxonomy_term'];
$sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
// Add view mode theme suggestions.
$suggestions[] = 'taxonomy_term__' . $sanitized_view_mode;
$suggestions[] = 'taxonomy_term__' . $term->bundle() . '__' . $sanitized_view_mode;
$suggestions[] = 'taxonomy_term__' . $term->id() . '__' . $sanitized_view_mode;
}
/**
* Implements hook_theme_suggestions_HOOK_alter() for block().
*/
function brainsum_starterkit_theme_suggestions_block_alter(&$suggestions, array $variables) {
if (!empty($variables['elements']['#id'])) {
/** @var \Drupal\block\BlockInterface $block */
$block = \Drupal::entityTypeManager()
->getStorage('block')
->load($variables['elements']['#id']);
if ($block) {
// Add region-specific block theme suggestions.
$region = $block
->getRegion();
$suggestions[] = 'block__' . $region;
$suggestions[] = 'block__' . $region . '__' . 'plugin_id' . '__' . $variables['elements']['#plugin_id'];
$suggestions[] = 'block__' . $region . '__' . 'id' . '__' . $variables['elements']['#id'];
}
}
}
/**
* Implements hook_preprocess_HOOK() for menu-local-tasks templates.
*/
function brainsum_starterkit_preprocess_menu_local_tasks(&$variables) {
foreach (Element::children($variables['primary']) as $key) {
$variables['primary'][$key]['#level'] = 'primary';
}
foreach (Element::children($variables['secondary']) as $key) {
$variables['secondary'][$key]['#level'] = 'secondary';
}
}
/**
* Implements hook_theme_suggestions_HOOK_alter() for form templates.
*/
function brainsum_starterkit_theme_suggestions_form_alter(array &$suggestions, array $variables) {
if (isset($variables['element']['#form_id'])) {
$form_id = $variables['element']['#form_id'];
$suggestions[] = 'form__' . $form_id;
}
}
/**
* Implements hook_form_alter() for adding classes and placeholder text to the search forms.
*/
function brainsum_starterkit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if (isset($form['actions']['submit']) && (count($form['actions'])) <= 2) {
$form['actions']['submit']['#attributes']['class'][] = 'c-button--primary';
}
switch ($form_id) {
case 'search_block_form':
// Add placeholder text to keys input.
$form['keys']['#attributes']['placeholder'] = t(
'Search by keyword or phrase.'
);
// Add classes to the search form submit input.
$form['actions']['submit']['#attributes']['class'][] = 'c-search-form__submit';
break;
case 'search_form':
$form['basic']['keys']['#attributes']['placeholder'] = t(
'Search by keyword or phrase.'
);
$form['basic']['submit']['#attributes']['class'][] = 'c-button--primary';
$form['advanced']['submit']['#attributes']['class'][] = 'c-button--primary';
break;
}
}
/**
* Implements hook_preprocess_form_element().
*/
function brainsum_starterkit_preprocess_form_element(&$variables) {
if (
in_array($variables['element']['#type'] ?? FALSE, ['checkbox', 'radio'], TRUE)
) {
$variables['attributes']['class'][] = 'c-form-type--boolean';
}
if (!empty($variables['description']['attributes'])) {
$variables['description']['attributes']->addClass('c-form-item__description');
}
if ($variables['disabled']) {
$variables['label']['#attributes']['class'][] = 'is-disabled';
}
}
/**
* Implements hook_preprocess_HOOK().
*/
function brainsum_starterkit_preprocess_form_element_label(&$variables) {
$variables['attributes']['class'][] = 'c-form-item__label';
}
/**
* Implements hook_preprocess_HOOK().
*/
function brainsum_starterkit_preprocess_input(&$variables) {
if (
!empty($variables['element']['#title_display']) &&
$variables['element']['#title_display'] === 'attribute' &&
!empty((string) $variables['element']['#title'])
) {
$variables['attributes']['title'] = (string) $variables['element']['#title'];
}
$type_api = $variables['element']['#type'];
$type_html = $variables['attributes']['type'];
$text_types_html = [
'text',
'email',
'tel',
'number',
'search',
'password',
'date',
'time',
'file',
'color',
'datetime-local',
'url',
'month',
'week',
];
if (in_array($type_html, $text_types_html, TRUE)) {
$variables['attributes']['class'][] = 'c-form-element';
$variables['attributes']['class'][] = Html::getClass(
'c-form-element--type-' . $type_html
);
$variables['attributes']['class'][] = Html::getClass(
'c-form-element--api-' . $type_api
);
/*
* Add form label value as placeholder attribute.
* Uncomment this if you want to add the label as a placeholder.
* if (isset($variables['element']['#title'])) {
* $variables['attributes']['placeholder'] = (string) $variables['element']['#title'];
* }
*/
if (!empty($variables['element']['#autocomplete_route_name'])) {
$variables['autocomplete_message'] = t('Loading…');
}
}
if (in_array($type_html, ['checkbox', 'radio'], TRUE)) {
$variables['attributes']['class'][] = 'c-form-boolean';
$variables['attributes']['class'][] = Html::getClass(
'c-form-boolean--type-' . $type_html
);
}
}
/**
* Implements hook_preprocess_HOOK().
*/
function brainsum_starterkit_preprocess_textarea(&$variables) {
$variables['attributes']['class'][] = 'c-form-element';
$variables['attributes']['class'][] = 'c-form-element--type-textarea';
$variables['attributes']['class'][] = 'c-form-element--api-textarea';
/*
* Add form label value as placeholder attribute.
* Uncomment this if you want to add the label as a placeholder.
* if (isset($variables['element']['#title'])) {
* $variables['attributes']['placeholder'] = (string) $variables['element']['#title'];
* }
*/
}
/**
* Implements hook_preprocess_HOOK().
*/
function brainsum_starterkit_preprocess_select(&$variables) {
$variables['attributes']['class'][] = 'c-form-element';
$variables['attributes']['class'][] = $variables['element']['#multiple'] ?
'c-form-element--type-select-multiple' :
'c-form-element--type-select';
}
/**
* Implements hook_preprocess_HOOK().
*/
function brainsum_starterkit_preprocess_checkboxes(&$variables) {
$variables['attributes']['class'][] = 'c-form-boolean-group';
}
/**
* Implements hook_preprocess_HOOK().
*/
function brainsum_starterkit_preprocess_radios(&$variables) {
$variables['attributes']['class'][] = 'c-form-boolean-group';
}
/**
* Implements hook_preprocess_HOOK().
*/
function brainsum_starterkit_preprocess_field(&$variables) {
$rich_field_types = ['text_with_summary', 'text', 'text_long'];
if (in_array($variables['field_type'], $rich_field_types, TRUE)) {
$variables['attributes']['class'][] = 'text-content';
}
}
/**
* Implements hook_preprocess_HOOK().
*/
function brainsum_starterkit_preprocess_field_multiple_value_form(&$variables) {
// Make disabled available for the template.
$variables['disabled'] = !empty($variables['element']['#disabled']);
if (!empty($variables['multiple'])) {
// Add an additional CSS class for the field label table cell.
// This repeats the logic of template_preprocess_field_multiple_value_form()
// without using '#prefix' and '#suffix' for the wrapper element.
//
// If the field is multiple, we don't have to check the existence of the
// table header cell.
//
// @see template_preprocess_field_multiple_value_form().
$header_attributes = [
'class' => [
'c-form-item__label',
'c-form-item__label--multiple-value-form',
],
];
if (!empty($variables['element']['#required'])) {
$header_attributes['class'][] = 'js-form-required';
$header_attributes['class'][] = 'form-required';
$header_attributes['class'][] = 'is-required';
}
// Using array_key_first() for addressing the first header cell would be
// more elegant here, but we can rely on the related theme.inc preprocess.
// @todo change this after https://www.drupal.org/node/3099026 has landed.
$variables['table']['#header'][0]['data'] = [
'#type' => 'html_tag',
'#tag' => 'h4',
'#value' => $variables['element']['#title'],
'#attributes' => $header_attributes,
];
if ($variables['disabled']) {
$variables['table']['#attributes']['class'][] = 'tabledrag-disabled';
$variables['table']['#attributes']['class'][] = 'js-tabledrag-disabled';
// We will add the 'is-disabled' CSS class to the disabled table header
// cells.
$header_attributes['class'][] = 'is-disabled';
foreach ($variables['table']['#header'] as &$cell) {
if (is_array($cell) && isset($cell['data'])) {
$cell = $cell + ['class' => []];
$cell['class'][] = 'is-disabled';
}
else {
// We have to modify the structure of this header cell.
$cell = [
'data' => $cell,
'class' => ['is-disabled'],
];
}
}
}
}
}
/**
* Implements template_preprocess_HOOK() for fieldset.
*/
function brainsum_starterkit_preprocess_fieldset(&$variables) {
$element = $variables['element'];
$composite_types = ['checkboxes', 'radios'];
if (
!empty($element['#type']) && in_array($element['#type'], $composite_types)
&& !empty($variables['element']['#children_errors'])
) {
$variables['legend_span']['attributes']->addClass('has-error');
}
if (!empty($element['#disabled'])) {
$variables['legend_span']['attributes']->addClass('is-disabled');
if (
!empty($variables['description']) &&
!empty($variables['description']['attributes'])
) {
$variables['description']['attributes']->addClass('is-disabled');
}
}
// Remove 'container-inline' class from the main attributes and add a flag
// instead.
// @todo remove this after https://www.drupal.org/node/3059593 has been
// resolved.
if (!empty($variables['attributes']['class'])) {
$container_inline_key = array_search(
'container-inline', $variables['attributes']['class']
);
if ($container_inline_key !== FALSE) {
unset($variables['attributes']['class'][$container_inline_key]);
$variables['inline_items'] = TRUE;
}
}
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function brainsum_starterkit_theme_suggestions_fieldset_alter(
array &$suggestions,
array $variables,
$hook,
) {
if (isset($variables['element']['#id'])) {
$id = str_replace("-", "_", $variables['element']['#id']);
$suggestions[] = $hook . '__' . $id;
}
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function brainsum_starterkit_theme_suggestions_user_alter(&$suggestions, $variables) {
$suggestions[] = 'user__' . $variables['elements']['#view_mode'];
}
/**
* Implements hook_preprocess_HOOK().
*/
function brainsum_starterkit_preprocess_field__node__created(&$variables) {
foreach (Element::children($variables['items']) as $item) {
unset($variables['items'][$item]['content']['#prefix']);
}
}
/**
* Implements hook_preprocess_HOOK() for setting classes.
*/
function brainsum_starterkit_preprocess_filter_caption(&$variables) {
$variables['classes'] = isset($variables['classes']) && !empty(
$variables['classes']
) ? $variables['classes'] . ' caption' : 'caption';
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function brainsum_starterkit_form_comment_form_alter(
&$form,
FormStateInterface $form_state,
$form_id,
) {
$form['backlink']['#options']['attributes']['class'][] = 'c-button';
$form['backlink']['#options']['attributes']['class'][] = 'c-button--small';
$form['backlink']['#options']['attributes']['class'][] = 'c-button--icon-back';
$form['backlink']['#options']['attributes']['class'][] = 'c-button--primary';
$form['view_mode']['#attributes']['class'][] = 'c-form-element--small';
}
/**
* Implements template_preprocess_text_format_wrapper().
*
* @todo Remove when https://www.drupal.org/node/3016343 is fixed.
*/
function brainsum_starterkit_preprocess_text_format_wrapper(&$variables) {
$description_attributes = [];
if (!empty($variables['attributes']['id'])) {
$description_attributes['id'] = $variables['attributes']['aria-describedby'] = $variables['attributes']['id'];
unset($variables['attributes']['id']);
}
$variables['description_attributes'] = new Attribute($description_attributes);
}
/**
* Implements hook_preprocess_table().
*/
function brainsum_starterkit_preprocess_table(&$variables) {
// Mark the whole table and the first cells if rows are draggable.
if (!empty($variables['rows'])) {
$draggable_row_found = FALSE;
foreach ($variables['rows'] as &$row) {
/** @var \Drupal\Core\Template\Attribute $row['attributes'] */
if (!empty($row['attributes']) && $row['attributes']->hasClass('draggable')) {
if (!$draggable_row_found) {
$variables['attributes']['class'][] = 'draggable-table';
$draggable_row_found = TRUE;
}
}
}
}
}
/**
* Implements hook_form_views_exposed_form_alter().
*/
function brainsum_starterkit_form_views_exposed_form_alter(&$form) {
$form['#attributes']['class'][] = 'form--inline';
}
/**
* Implements template_preprocess_menu_local_task().
*
* Modified visually-hidden class to utility class.
* Added extra CSS classes.
*/
function brainsum_starterkit_preprocess_menu_local_task(&$variables) {
$variables['link']['#options']['attributes']['class'][] = 'c-tabs__link';
$variables['link']['#options']['attributes']['class'][] = 'js-tabs-link';
// Ensure is-active class is set when the tab is active. The generic active
// link handler applies stricter comparison rules than what is necessary for
// tabs.
if (isset($variables['is_active']) && $variables['is_active'] === TRUE) {
$variables['link']['#options']['attributes']['class'][] = 'is-active';
}
if (isset($variables['element']['#level'])) {
$variables['level'] = $variables['element']['#level'];
}
}