-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnicsdru_unity_theme.theme
497 lines (445 loc) · 17.4 KB
/
nicsdru_unity_theme.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
<?php
/**
* @file
* Functions to support theming in the nicsdru_unity_theme.
*/
use Drupal\Component\Utility\Html;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Render\Markup;
use Drupal\block\Entity\Block;
use Drupal\file\FileInterface;
use Drupal\media\MediaInterface;
use Drupal\node\Entity\Node;
use Drupal\taxonomy\Entity\Term;
/**
* Implements hook_preprocess().
*/
function nicsdru_unity_theme_preprocess(&$variables) {
// Set a variable based on the path.matcher.
if (!isset($variables['is_front'])) {
try {
$variables['is_front'] = \Drupal::service('path.matcher')->isFrontPage();
}
catch (\Exception $e) {
$variables['is_front'] = FALSE;
}
}
// Ensure the cache varies correctly.
$variables['#cache']['contexts'][] = 'url.path.is_front';
}
/**
* Implements hook_preprocess_HOOK().
*/
function nicsdru_unity_theme_preprocess_page(&$variables) {
$variables['basic_page_structure'] = FALSE;
if (theme_get_setting('basic_page_structure')) {
$variables['basic_page_structure'] = TRUE;
}
}
/**
* Implements hook_preprocess_page_attachments_alter().
*/
function nicsdru_unity_theme_page_attachments_alter(array &$page) {
/*
* Tell IE to use latest rendering engine (not to use compatibility mode).
* /*$ie_edge = [
* '#type' => 'html_tag',
* '#tag' => 'meta',
* '#attributes' => [
* 'http-equiv' => 'X-UA-Compatible',
* 'content' => 'IE=edge',
* ],
* ];
* $page['#attached']['html_head'][] = [$ie_edge, 'ie_edge'];
*/
// Remove Drupal default favicon link in <head>, if present.
if (isset($page['#attached']['html_head_link'][0])) {
unset($page['#attached']['html_head_link'][0]);
}
// Attach our drupal components library for authenticated users.
$userCurrent = \Drupal::currentUser();
if ($userCurrent->isAuthenticated()) {
$page['#attached']['library'][] = 'nicsdru_unity_theme/nicsdru-authenticated-styles';
}
// Disable Google Analytics tracking if option is set in the theme, an
// analytics ID is present and optionally if the EU Cookie Compliance module
// is installed and options set in the theme.
if (theme_get_setting('ga_tracking_disabled')) {
// Fetch the account id from the Google Analytics module config.
$ga_account = \Drupal::config('google_analytics.settings')->get('account');
// If Google Analytics tracking option is set in theme and EU Cookie
// Compliance cookie is set to anything other than 2 (agree) then disable
// analytics.
// See https://developers.google.com/analytics/devguides/collection/analyticsjs/user-opt-out.
if ($ga_account) {
if (theme_get_setting('ga_tracking_disabled_eu')) {
$script = "if(document.cookie.valueOf('cookie-agreed') != 'cookie-agreed=2') {
window['ga-disable-" . $ga_account . "'] = true; console.log('block')}";
}
else {
// By default block Google Analytics.
$script = "window['ga-disable-" . $ga_account . "'] = true;";
}
// The 'ga-disable' call must come before the Google Analytics is
// initialised, using array_unshift to add the script to the first part
// of the HTML head.
array_unshift($page['#attached']['html_head'], [
[
'#tag' => 'script',
'#attributes' => ['type' => 'text/javascript'],
'#value' => $script,
],
'ga_tracking_options',
]);
}
}
return $page;
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function nicsdru_unity_theme_form_user_login_form_alter(&$form, FormStateInterface $form_state, $form_id = NULL) {
$form['#attached']['library'][] = 'nicsdru_unity_theme/nicsdru-authenticated-styles';
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function nicsdru_unity_theme_form_user_pass_alter(&$form, FormStateInterface $form_state, $form_id = NULL) {
$form['#attached']['library'][] = 'nicsdru_unity_theme/nicsdru-authenticated-styles';
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function nicsdru_unity_theme_form_node_preview_form_select_alter(&$form, FormStateInterface $form_state, $form_id) {
// Remove view mode selector from preview.
unset($form['view_mode']);
}
/**
* Implements hook_preprocess_html().
*/
function nicsdru_unity_theme_preprocess_html(&$variables) {
$variables['path_info']['args'] = FALSE;
$path = \Drupal::request()->getPathInfo();
$path_args = explode('/', $path);
if (count($path_args) >= 3) {
$variables['path_info']['args'] = Html::cleanCssIdentifier(ltrim($path, '/'));
}
// Add information about the number of sidebars.
if ((!empty($variables['page']['sidebar_first']) || !empty($variables['page']['search_facets']))
&& !empty($variables['page']['sidebar_second'])) {
$variables['attributes']['class'][] = 'two-sidebars';
}
elseif (!empty($variables['page']['sidebar_first']) || !empty($variables['page']['search_facets'])) {
$variables['attributes']['class'][] = 'one-sidebar';
$variables['attributes']['class'][] = 'sidebar-first';
}
elseif (!empty($variables['page']['sidebar_second'])) {
$variables['attributes']['class'][] = 'one-sidebar';
$variables['attributes']['class'][] = 'sidebar-second';
}
else {
$variables['attributes']['class'][] = 'no-sidebars';
}
}
/**
* Implements hook_preprocess_form().
*/
function nicsdru_unity_theme_preprocess_form(array &$variables) {
switch ($variables['element']['#id']) {
case "block-exposedformnewsnews-search-api":
case "block-exposedformpublication-search-api":
$variables['attributes']['class'][] = Html::cleanCssIdentifier('search-form');
$variables['attributes']['class'][] = Html::cleanCssIdentifier('search-form--page');
break;
}
}
/**
* Implements hook_preprocess_views_view().
*/
function nicsdru_unity_theme_preprocess_views_view(&$variables) {
$variables['attributes']['role'] = 'presentation';
if ($variables['id'] === 'search') {
$variables['exposed']['#attributes']['class'][] = Html::cleanCssIdentifier('search-form');
$variables['exposed']['#attributes']['class'][] = Html::cleanCssIdentifier('search-form--site');
// Make the site search required.
$variables['exposed']['query']['#required'] = TRUE;
// See nicsdru_unity_theme_form_alter for a required pattern on the input. The data-msg-pattern overrides the default error message.
$variables['exposed']['query']['#attributes']['data-msg-pattern'] = t('Query must begin with a letter or number.');
// If a label for the search input is not defined in the view, set
// default label text.
$search_label =& $variables['exposed']['#info']['filter-search_api_fulltext']['label'];
if (isset($search_label) && empty($search_label)) {
$search_label = t("Search this site");
}
// The label should be visually hidden.
$variables['exposed']['query']['#title_display'] = 'invisible';
}
}
/**
* Implements hook_preprocess_facets_item_list().
*/
function nicsdru_unity_theme_preprocess_facets_item_list(array &$variables) {
if (!empty($variables["facet"])) {
if (!empty($variables["facet"]->getWidget()) && $variables["facet"]->getWidget()["type"] == 'links') {
foreach ($variables["items"] as $key => $item) {
if (array_key_exists('class', $item["value"]["#attributes"])) {
$variables["items"][$key]["value"]["#attributes"]["class"][] = 'facet-item--link';
}
else {
$variables["items"][$key]["value"]["#attributes"]["class"] = ['facet-item--link'];
}
}
}
}
}
/**
* Implements hook_preprocess_media().
*/
function nicsdru_unity_theme_preprocess_media(&$variables) {
if (empty($variables['media']) || $variables['media']->bundle() != 'document') {
return;
}
if (!empty($variables['content']['field_media_file'])) {
// Map file mimetypes to user friendly document types.
$pretty_mimes = \Drupal::service('origins_media.pretty_mime_types')
->getMimeTypes();
$file = &$variables['content']['field_media_file'][0]['#file'];
if (!empty($file)) {
$variables['file_size'] = format_size($file->getSize(), \Drupal::languageManager()
->getCurrentLanguage()
->getId());
$variables['file_url'] = $file->createFileUrl();
$variables['file_extension'] = pathinfo($file->createFileUrl(), PATHINFO_EXTENSION);
$file_mimetype = $file->getMimeType();
if (array_key_exists($file->getMimeType(), $pretty_mimes)) {
$variables['file_type'] = $pretty_mimes[$file_mimetype];
}
else {
$variables['file_type'] = strtoupper(preg_replace('/^application\/(\w+)$/', '\1', $file_mimetype));
}
}
$variables['media_langcode'] = $variables['media']->language()->getId();
}
}
/**
* Implements hook_preprocess_slick().
*/
function nicsdru_unity_theme_preprocess_slick(&$variables) {
if (isset($variables['arrow_attributes']['role'])) {
unset($variables['arrow_attributes']['role']);
}
}
/**
* Implements hook_preprocess_container().
*
* Adds a suffix to elements with id #edit-actions to stop duplication of ids
* on the same page.
*/
function nicsdru_unity_theme_preprocess_container(&$variables) {
if (isset($variables['element']['#id'])) {
$id = $variables['element']['#id'];
if (in_array($id, ['edit-actions'])) {
$id = Html::getUniqueId($id);
}
$variables['element']['#id'] = $id;
$variables['attributes']['id'] = $id;
}
}
/**
* Implements hook_form_alter().
*/
function nicsdru_unity_theme_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if ($form['#id'] == 'views-exposed-form-search-site-search' || $form['#id'] == 'views-exposed-form-search-search-page') {
// Ensure the input field only accepts a letter or a number at the beginning of a search.
$form['query']['#pattern'] = '^[a-zA-Z0-9]+.*';
}
}
/**
* Implements hook_form_element_label().
*/
function nicsdru_unity_theme_preprocess_form_element_label(&$variables) {
// Search the exposed filter label for text: 'Search' any word e.g. Search consultations.
if (preg_match('/Search\D[a-z]+/', $variables['title']['#markup'])) {
// Make the label invisible.
$variables['title_display'] = "invisible";
}
}
/**
* Implements hook_preprocess_responsive_image_formatter().
*/
function nicsdru_unity_theme_preprocess_responsive_image_formatter(array &$variables) {
// Load the parent media entity that the responsive image is
// or might be associated to.
$media_image = $variables['item']->getEntity();
if ($media_image instanceof MediaInterface === FALSE) {
return;
}
// If we've got a parent media entity and caption field value
// then inject the value into the responsive image attributes collection.
// We're relying on Drupal input sanitising from the media form as
// well as safe Twig rendering in responsive-image.html.twig to cover
// any content that is migrated in from the legacy db and could
// potentially contain dodgy html.
$caption = $media_image->get('field_caption')->getString();
// Don't show the caption on the slideshow images.
if ($variables['responsive_image_style_id'] != 'slideshow') {
if (!empty($caption)) {
$variables['responsive_image']['#attributes']['caption'] = $caption;
}
}
}
/**
* Implements hook_preprocess_block().
*/
function nicsdru_unity_theme_preprocess_block(&$variables) {
// Pass block region value to content so this can be used in nicsdru_unity_theme_theme_suggestions_menu_alter.
if (!empty($variables['elements']['#id'])) {
$region = Block::load($variables['elements']['#id'])->getRegion();
$variables['content']['#attributes']['region'] = $region;
}
// Check the 'In This Section' block to see if there are any
// entries in it. If there are no entries then set the
// 'block_is_empty' flag to hide the block.
if ($variables['plugin_id'] == 'system_menu_block:main') {
if ($variables['elements']['#id'] == 'sidemenu') {
$variables['block_is_empty'] = TRUE;
foreach ($variables['content']['#items'] as $menu_item) {
if ($menu_item['is_expanded']) {
if (count($menu_item['below']) > 0) {
$variables['block_is_empty'] = FALSE;
}
}
}
}
}
// Social media variables to be used in the theme-settings.php.
$variables['twitter_profile_url'] = theme_get_setting('twitter_profile_url');
$variables['facebook_profile_url'] = theme_get_setting('facebook_profile_url');
$variables['linkedin_profile_url'] = theme_get_setting('linkedin_profile_url');
$variables['pinterest_profile_url'] = theme_get_setting('pinterest_profile_url');
$variables['youtube_profile_url'] = theme_get_setting('youtube_profile_url');
$variables['instagram_profile_url'] = theme_get_setting('instagram_profile_url');
}
/**
* Implements hook_theme_suggestions_menu_alter().
*
* Provide region based menu suggestions.
*/
function nicsdru_unity_theme_theme_suggestions_menu_alter(&$suggestions, array $variables) {
if (!empty($variables['attributes']['region'])) {
$suggestions[] = 'menu__' . $variables['menu_name'] . '__' . $variables['attributes']['region'];
}
}
/**
* Implements hook_preprocess_HOOK().
*/
function nicsdru_unity_theme_preprocess_field(&$variables) {
if ($variables['field_name'] == 'field_attachment') {
$path = \Drupal::service('path_alias.manager')
->getPathByAlias('/help-viewing-documents');
if (preg_match('/node\/(\d+)/', $path, $matches)) {
$node = Node::load($matches[1]);
$variables['node_id'] = $node->id();
}
}
}
/**
* Implements hook_theme_suggestions_field_alter().
*/
function nicsdru_unity_theme_theme_suggestions_field_alter(array &$suggestions, array $variables) {
// Add view mode to node field theme suggestions.
$element = $variables['element'];
$suggestions[] = 'field__node__' .
$element['#field_name'] . '__' .
$element['#view_mode'];
$suggestions[] = 'field__node__' .
$element['#field_name'] . '__' .
$element['#view_mode'] . '__' .
$element['#bundle'];
}
/**
* Implements template_preprocess_file_link().
*/
function nicsdru_unity_theme_preprocess_file_link(array &$variables) {
if (!$variables['file'] instanceof FileInterface) {
return;
}
$file_extension = pathinfo($variables['file']->getFilename(), PATHINFO_EXTENSION);
$variables['attributes']->addClass([
'file-link',
'file--ico',
'file--ico__' . $file_extension,
]);
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function nicsdru_unity_theme_form_node_landing_page_layout_builder_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Hide revision information section.
$form['revision_information']['#access'] = FALSE;
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function nicsdru_unity_theme_theme_suggestions_block_alter(array &$suggestions, array $variables) {
$plugin_id = $variables['elements']['#plugin_id'];
// Update Layout Builder Standard and Wide inline card blocks to use
// templates/block/block--inline-block--card.html.twig.
if ($plugin_id === 'inline_block:card_wide' || $plugin_id === 'inline_block:card_standard') {
$suggestions[] = 'block__inline_block__card';
}
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function nicsdru_unity_theme_theme_suggestions_views_view_unformatted_alter(array &$suggestions, array $variables) {
if (!empty($variables['view']->element['#display_id'])) {
$suggestions[] = 'views_view_unformatted__' . $variables['view']->element['#display_id'];
}
}
/**
* Implements template_preprocess_field_group_html_element().
* Preprocessing the Additional content details field group used in the
* revision comparison view mode to remove the need to the contrib classy theme Drupal 10.
*/
function nicsdru_unity_theme_preprocess_field_group_html_element(&$variables) {
$element = $variables['element'];
if ($element['#group_name'] === 'group_additional_content_details') {
// Get group element children.
$children = Element::children($element);
if (!empty($children)) {
$new_children_output = '';
foreach ($children as $child) {
if (!empty($element[$child]['#object']) && !empty($variables['element'][$child]['#title'])) {
$child_title = $element[$child]['#title'];
$child_object = $element[$child]['#object'];
// Build new HTML to add the required classes to make the revision comparison styling to work.
$new_children_output .= '<div class="field field--label-inline">';
$new_children_output .= '<div class="field__label">' . $child_title . '</div>';
// If value is a number load the taxonomy term or a node.
if (is_numeric(current($child_object->get($child)->getValue()[0]))) {
$term = Term::load(current($child_object->get($child)
->getValue()[0]));
// Check the term exists.
if (!empty($term)) {
$new_children_output .= '<div class="field__item">' . $term->getName() . '</div>';
// If it isn't a term then check if it is a node.
}
else {
$node = Node::load(current($child_object->get($child)->getValue()[0]));
if (!empty($node)) {
$new_children_output .= '<div class="field__item">' . $node->getTitle() . '</div>';
}
}
}
else {
$new_children_output .= '<div class="field__item">' . current($child_object->get($child)->getValue()[0]) . '</div>';
}
$new_children_output .= '</div>';
}
}
$variables['children'] = Markup::create($new_children_output);
}
}
}