-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathclaro.theme
292 lines (259 loc) · 8.99 KB
/
claro.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
<?php
/**
* @file
* Functions to support theming in the Claro theme.
*/
use Drupal\Component\Utility\Html;
use Drupal\Core\Link;
use Drupal\Core\Form\FormStateInterface;
use Drupal\media\MediaForm;
/**
* Implements hook_preprocess_HOOK() for HTML document templates.
*/
function claro_preprocess_html(&$variables) {
// If on a node add or edit page, add a node-layout class.
$path_args = explode('/', \Drupal::request()->getPathInfo());
if ($suggestions = theme_get_suggestions($path_args, 'page', '-')) {
foreach ($suggestions as $suggestion) {
if ($suggestion === 'page-node-edit' || strpos($suggestion, 'page-node-add') !== FALSE) {
$variables['attributes']['class'][] = 'node-form-layout';
}
}
}
}
/**
* Implements hook_preprocess_HOOK() for menu-local-tasks templates.
*
* Use preprocess hook to set #attached to child elements
* because they will be processed by Twig and drupal_render will
* be invoked.
*/
function claro_preprocess_menu_local_tasks(&$variables) {
if (!empty($variables['primary'])) {
$variables['primary']['#attached'] = [
'library' => [
'claro/drupal.nav-tabs',
],
];
}
elseif (!empty($variables['secondary'])) {
$variables['secondary']['#attached'] = [
'library' => [
'claro/drupal.nav-tabs',
],
];
}
}
/**
* Implements hook_preprocess_HOOK() for menu-local-task templates.
*/
function claro_preprocess_menu_local_task(&$variables) {
$variables['attributes']['class'][] = 'tabs__tab';
}
/**
* Implements hook_preprocess_HOOK() for list of available node type templates.
*/
function claro_preprocess_node_add_list(&$variables) {
if (!empty($variables['content'])) {
/** @var \Drupal\node\NodeTypeInterface $type */
foreach ($variables['content'] as $type) {
$variables['types'][$type->id()]['label'] = $type->label();
$variables['types'][$type->id()]['url'] = \Drupal::url('node.add', ['node_type' => $type->id()]);
}
}
}
/**
* Implements hook_preprocess_HOOK() for block content add list templates.
*
* Displays the list of available custom block types for creation, adding
* separate variables for the label and url.
*/
function claro_preprocess_block_content_add_list(&$variables) {
if (!empty($variables['content'])) {
foreach ($variables['content'] as $type) {
$variables['types'][$type->id()]['label'] = $type->label();
$options = ['query' => \Drupal::request()->query->all()];
$variables['types'][$type->id()]['url'] = \Drupal::url('block_content.add_form', ['block_content_type' => $type->id()], $options);
}
}
}
/**
* Implements hook_preprocess_block() for block content.
*
* Disables contextual links for all blocks.
*/
function claro_preprocess_block(&$variables) {
if (isset($variables['title_suffix']['contextual_links'])) {
unset($variables['title_suffix']['contextual_links']);
unset($variables['elements']['#contextual_links']);
$variables['attributes']['class'] = array_diff($variables['attributes']['class'], ['contextual-region']);
}
}
/**
* Implements template_preprocess_HOOK() for admin_block.
*/
function claro_preprocess_admin_block(&$variables) {
if (!empty($variables['block']['content'])) {
$variables['block']['content']['#attributes']['class'][] = 'admin-list--panel';
}
}
/**
* Implements template_preprocess_HOOK() for admin_block.
*/
function claro_preprocess_admin_block_content(&$variables) {
foreach ($variables['content'] as $id => &$item) {
$link_attributes = $item['url']->getOption('attributes') ?: [];
$link_attributes['class'][] = 'admin-item__link';
$item['url']->setOption('attributes', $link_attributes);
$item['link'] = Link::fromTextAndUrl($item['title'], $item['url']);
if (empty($item['description']) || empty($item['description']['#markup'])) {
unset($item['description']);
}
}
}
/**
* Implements hook_preprocess_HOOK() for menu-local-action templates.
*/
function claro_preprocess_menu_local_action(array &$variables) {
$variables['link']['#options']['attributes']['class'][] = 'button--primary';
$variables['link']['#options']['attributes']['class'][] = 'button--small';
// We require Modernizr's touch test for button styling.
$variables['#attached']['library'][] = 'core/modernizr';
$variables['#attached']['library'][] = 'core/modernizr';
}
/**
* Implements hook_element_info_alter().
*/
function claro_element_info_alter(&$type) {
// We require Modernizr for button styling.
if (isset($type['button'])) {
$type['button']['#attached']['library'][] = 'core/modernizr';
}
}
/**
* Implements hook_theme_registry_alter().
*/
function claro_theme_registry_alter(&$theme_registry) {
if (!empty($theme_registry['admin_block_content'])) {
$theme_registry['admin_block_content']['variables']['attributes'] = [];
}
}
/**
* Implements hook_preprocess_install_page().
*/
function claro_preprocess_install_page(&$variables) {
// Claro has custom styling for the install page.
$variables['#attached']['library'][] = 'claro/install-page';
}
/**
* Implements hook_preprocess_maintenance_page().
*/
function claro_preprocess_maintenance_page(&$variables) {
// Claro has custom styling for the maintenance page.
$variables['#attached']['library'][] = 'claro/maintenance-page';
}
/**
* Implements hook_form_BASE_FORM_ID_alter() for \Drupal\node\NodeForm.
*
* Changes vertical tabs to container.
*/
function claro_form_node_form_alter(&$form, FormStateInterface $form_state) {
$form['#theme'] = ['node_edit_form'];
$form['#attached']['library'][] = 'claro/node-form';
$form['advanced']['#type'] = 'container';
$form['meta']['#type'] = 'container';
$form['meta']['#access'] = TRUE;
$form['meta']['changed']['#wrapper_attributes']['class'][] = 'container-inline';
$form['meta']['author']['#wrapper_attributes']['class'][] = 'container-inline';
$form['revision_information']['#type'] = 'container';
$form['revision_information']['#group'] = 'meta';
}
/**
* Implements hook_form_BASE_FORM_ID_alter() for \Drupal\media\MediaForm.
*/
function claro_form_media_form_alter(&$form, FormStateInterface $form_state) {
// Only attach CSS from core if this form comes from Media core, and not from
// the contrib Media Entity 1.x branch.
if (\Drupal::moduleHandler()->moduleExists('media') && $form_state->getFormObject() instanceof MediaForm) {
// @todo Revisit after https://www.drupal.org/node/2892304 is in. It
// introduces a footer region to these forms which will allow for us to
// display a top border over the published checkbox by defining a
// media-edit-form.html.twig template the same way node does.
$form['#attached']['library'][] = 'claro/media-form';
}
}
/**
* Implements hook_preprocess_form_element().
*/
function claro_preprocess_form_element(&$variables) {
if (!empty($variables['element']['#errors'])) {
$variables['label']['#attributes']['class'][] = 'has-error';
}
if ($variables['disabled']) {
$variables['label']['#attributes']['class'][] = 'is-disabled';
if (!empty($variables['description']['attributes'])) {
$variables['description']['attributes']->addClass('is-disabled');
}
}
}
/**
* Implements template_preprocess_HOOK() for input.
*/
function claro_preprocess_input(&$variables) {
$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)) {
$variables['attributes']['class'][] = 'form-element';
$variables['attributes']['class'][] = Html::getClass('form-element--type-' . $type_html);
$variables['attributes']['class'][] = Html::getClass('form-element--api-' . $type_api);
}
}
/**
* Implements template_preprocess_HOOK() for textarea.
*/
function claro_preprocess_textarea(&$variables) {
$variables['attributes']['class'][] = 'form-element';
$variables['attributes']['class'][] = 'form-element--type-textarea';
$variables['attributes']['class'][] = 'form-element--api-textarea';
}
/**
* Implements template_preprocess_HOOK() for datetime_wrapper.
*/
function claro_preprocess_datetime_wrapper(&$variables) {
if (!empty($variables['element']['#errors'])) {
$variables['title_attributes']['class'][] = 'has-error';
}
if (!empty($variables['element']['#disabled'])) {
$variables['title_attributes']['class'][] = 'is-disabled';
if (!empty($variables['description_attributes'])) {
$variables['description_attributes']->addClass('is-disabled');
}
}
}
/**
* Implements template_preprocess_HOOK() for fieldset.
*/
function claro_preprocess_fieldset(&$variables) {
if (!empty($variables['element']['#disabled'])) {
$variables['legend_span']['attributes']->addClass('is-disabled');
if (!empty($variables['description']) && !empty($variables['description']['attributes'])) {
$variables['description']['attributes']->addClass('is-disabled');
}
}
}