-
Notifications
You must be signed in to change notification settings - Fork 0
/
dta_uikit_base.theme
125 lines (106 loc) · 4.34 KB
/
dta_uikit_base.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
<?php
/**
*
* Implements hook_preprocess_HOOK()
*
* This fiddles about with some elements to make them compatible with the Design System.
*
**/
/* Alters the template for checkbox labels so it works properly. */
function dta_uikit_base_preprocess_form_element(&$variables) {
if ($variables['element']['#type'] === 'checkbox' || $variables['element']['#type'] === 'radio' ){
$variables['label']['#theme'] = 'form_element_label__design_system';
$variables['title'] = $variables['element']['#title'];
$variables['element']['#design_system_label'] = TRUE;
}
}
//
// /* Turns the 'wrapped label' setting on for radio buttons. */
// function dta_uikit_base_preprocess_input__radio(&$variables) {
// $variables['element']['#wrapped_label'] = TRUE;
// }
//
function dta_uikit_base_theme_suggestions_form_element_alter(array &$suggestions, array $variables) {
if ($variables['element']['#type'] === 'checkbox') {
$suggestions[] = 'form_element__checkbox';
}
if ($variables['element']['#type'] === 'radio') {
$suggestions[] = 'form_element__radio';
}
}
function dta_uikit_base_theme_suggestions_form_element_label__design_system_alter(array &$suggestions, array $variables) {
if (!empty($variables['element']['#design_system_label'])) {
$suggestions[] = 'form_element_label__design_system';
} else {
$suggestions[] = 'form_element_label';
}
}
//
// function dta_uikit_base_preprocess_form_element__wrapped(&$variables) {
// $variables['label']['#theme'] = 'form_element_label__open';
// $variables['label_open'] = $variables['label'];
// unset($variables['label']);
// $variables['title'] = $variables['element']['#title'];
// }
/**
* Implements hook_theme_suggestions_block_alter().
*
* As per https://www.drupal.org/docs/8/theming-drupal-8/creating-a-drupal-8-sub-theme-or-sub-theme-of-sub-theme
* to ensure that sub themes inherit block templates.
*/
function dta_uikit_base_theme_suggestions_block_alter(&$suggestions, $variables) {
foreach ($suggestions as &$suggestion) {
$suggestion = str_replace('stable_', 'dta_uikit_base_', $suggestion);
}
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function dta_uikit_base_theme_suggestions_page_alter(&$suggestions, $variables, $hook) {
/**
* 404 template suggestion.
*
* From https://stackoverflow.com/a/49579500/1550345.
*/
if (!is_null(Drupal::requestStack()->getCurrentRequest()->attributes->get('exception'))) {
$status_code = Drupal::requestStack()->getCurrentRequest()->attributes->get('exception')->getStatusCode();
if ($status_code == 403 || $status_code == 404) {
$suggestions[] = 'page__error';
$suggestions[] = 'page__error__' . (string) $status_code;
}
}
}
/**
* Implements hook_theme_suggestions_HOOK_alter()
*
* Adds theme suggestions to views templates.
*/
function dta_uikit_base_theme_suggestions_views_view_alter(array &$suggestions, array $variables) {
// Adds theme suggestions to views.
$id = $variables['view']->id();
$display = $variables['view']->current_display;
$suggestions[] = 'views_view__' . (string) $id;
$suggestions[] = 'views_view__' . (string) $display;
$suggestions[] = 'views_view__' . (string) $id . '__' . (string) $display;
}
function dta_uikit_base_theme_suggestions_fieldset_alter(array &$suggestions, array $variables) {
// Adds theme suggestions to fieldsets using the ID of the element (if it exists), and the
// ID of the parent form (for templating all the fieldsets in a form, for example).
$original = $variables['theme_hook_original'];
$element = $variables['element'];
if (isset($element['#attributes']['data-twig-suggestion'])) {
$suggestion_suffix = str_replace(['-'], '_', $element['#attributes']['data-twig-suggestion']);
$suggestions[] = $original . '__' . (string) $suggestion_suffix;
}
$id = (!empty($variables['element']['#id'])) ? str_replace('-','_', $variables['element']['#id']) : '';
if ($id) {
$suggestions[] = $original . '__' . (string) $id;
}
}
function dta_uikit_base_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
// Adds twig suggestions to elements of forms based on the form ID.
foreach (\Drupal\Core\Render\Element::children($form) as $key) {
$form[$key]['#attributes']['data-twig-suggestion'] = $form['#id'];
}
$form['actions']['submit']['#attributes']['data-twig-suggestion'] = $form['#id'];
}