-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathlocalgov_base.theme
More file actions
532 lines (463 loc) · 21.8 KB
/
localgov_base.theme
File metadata and controls
532 lines (463 loc) · 21.8 KB
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
<?php
/**
* @file
* Theme hooks to support the LocalGov Base Theme.
*/
use Drupal\Component\Utility\Crypt;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\media\MediaInterface;
use Drupal\node\NodeInterface;
use Drupal\views\ViewExecutable;
/**
* Implements hook_form_system_theme_settings_alter().
*/
function localgov_base_form_system_theme_settings_alter(&$form, FormStateInterface $form_state, $form_id = NULL): void {
// Work-around for a core bug affecting admin themes. See issue #943212.
if (isset($form_id)) {
return;
}
// Add settings in a details element for better organization.
$theme_info = \Drupal::service('theme.manager')->getActiveTheme()->getExtension()->info;
$theme_human_name = $theme_info['name'] ?? \Drupal::service('theme.manager')->getActiveTheme()->getName();
$form['localgov_base_settings'] = [
'#type' => 'details',
'#title' => t('@theme Settings', ['@theme' => $theme_human_name]),
'#open' => TRUE,
];
// Create a fieldset for general settings and place at the top.
$form['localgov_base_settings']['general_fieldset'] = [
'#type' => 'fieldset',
'#title' => t('General Settings'),
'#description' => t('General layout and display settings.'),
'#weight' => -10,
];
$form['localgov_base_settings']['general_fieldset']['localgov_base_show_back_to_top_link'] = [
'#type' => 'checkbox',
'#title' => t('Display a "Back to Top" link on long pages.'),
'#default_value' => theme_get_setting('localgov_base_show_back_to_top_link'),
'#description' => t('This adds a link to the beginning of the page content (<code>#main-content</code>).'),
];
$form['localgov_base_settings']['general_fieldset']['localgov_base_header_behaviour'] = [
'#type' => 'radios',
'#title' => t('Header behaviour'),
'#default_value' => theme_get_setting('localgov_base_header_behaviour'),
'#options' => [
'default' => t('Default - scrolls away with the page'),
'sticky' => t('Sticky - remains at the top of the page'),
'appears_on_scroll' => t('Scroll - appears when scrolling up the page'),
],
'#description' => t('Select how you want the header to behave. This setting only apply to anonymous users.'),
];
$form['localgov_base_settings']['general_fieldset']['localgov_base_mobile_breakpoint_js'] = [
'#type' => 'number',
'#title' => t('Mobile breakpoint for JavaScript'),
'#default_value' => theme_get_setting('localgov_base_mobile_breakpoint_js') ? theme_get_setting('localgov_base_mobile_breakpoint_js') : 768,
'#description' => t('This is the mobile breakpoint in pixels. This is used by the JS to determine when to show the mobile menu. If left blank, this will default to 768px.'),
'#min' => 0,
'#attributes' => [
'placeholder' => '768',
],
];
$form['localgov_base_settings']['general_fieldset']['localgov_base_responsive_wysiwyg_tables'] = [
'#type' => 'checkbox',
'#title' => t('Make WYSIWYG tables responsive.'),
'#description' => t('Places tables inserted via WYSIWYG into a responsive container, so they can scroll horizontally on small screens.'),
'#default_value' => theme_get_setting('localgov_base_responsive_wysiwyg_tables') ? theme_get_setting('localgov_base_responsive_wysiwyg_tables') : FALSE,
];
// Create a fieldset for the CSS/JS library options.
$form['localgov_base_settings']['libraries_fieldset'] = [
'#type' => 'fieldset',
'#title' => t('Theme Libraries'),
'#description' => t('Control whether to load base theme CSS and JavaScript libraries.'),
];
$form['localgov_base_settings']['libraries_fieldset']['localgov_base_remove_css'] = [
'#type' => 'checkbox',
'#title' => t('Remove CSS libraries from base theme.'),
'#default_value' => theme_get_setting('localgov_base_remove_css'),
'#description' => t("Check this box to disable the base theme's CSS"),
];
$form['localgov_base_settings']['libraries_fieldset']['localgov_base_remove_js'] = [
'#type' => 'checkbox',
'#title' => t('Remove JS libraries from base theme.'),
'#default_value' => theme_get_setting('localgov_base_remove_js'),
'#description' => t("Check this box to disable the base theme's JavaScript"),
];
// Create a fieldset for content status related settings.
$form['localgov_base_settings']['content_status_fieldset'] = [
'#type' => 'fieldset',
'#title' => t('Content Status Display'),
'#description' => t('Control how unpublished and archived content is displayed.'),
];
$form['localgov_base_settings']['content_status_fieldset']['localgov_base_add_unpublished_background_colour'] = [
'#type' => 'checkbox',
'#title' => t('Add background colour to unpublished content.'),
'#default_value' => theme_get_setting('localgov_base_add_unpublished_background_colour'),
'#description' => t("If you remove the background colour, you should probably also check the box below to add '[Draft]' to the title of unpublished content."),
];
$form['localgov_base_settings']['content_status_fieldset']['localgov_base_add_draft_note_to_unpublished_content'] = [
'#type' => 'checkbox',
'#title' => t('Add "Draft" to title of draft unpublished content.'),
'#default_value' => theme_get_setting('localgov_base_add_draft_note_to_unpublished_content'),
'#description' => t('This adds the word "Draft" to the title of any draft unpublished content. This is useful if you have removed the pink background from unpublished content.'),
];
$form['localgov_base_settings']['content_status_fieldset']['localgov_base_add_archived_note_to_archived_content'] = [
'#type' => 'checkbox',
'#title' => t('Add "Archived" to title of archived content.'),
'#default_value' => theme_get_setting('localgov_base_add_archived_note_to_archived_content'),
'#description' => t('This adds the word "Archived" to the title of any content with the "archived" moderation state.'),
];
// Create a fieldset for guide-related settings.
$form['localgov_base_settings']['guides_fieldset'] = [
'#type' => 'fieldset',
'#title' => t('Guide Display Settings'),
'#description' => t('Control how guides are displayed on the site.'),
];
$form['localgov_base_settings']['guides_fieldset']['localgov_base_localgov_guides_stacked_heading'] = [
'#type' => 'checkbox',
'#title' => t('Use stacked heading pattern for guides.'),
'#default_value' => theme_get_setting('localgov_base_localgov_guides_stacked_heading'),
'#description' => t('This will stack the Guide title and page heading on top of each other.'),
];
$form['localgov_base_settings']['guides_fieldset']['localgov_base_localgov_guides_vertical_navigation'] = [
'#type' => 'checkbox',
'#title' => t('Use vertical navigation for guides.'),
'#default_value' => theme_get_setting('localgov_base_localgov_guides_vertical_navigation'),
'#description' => t('This will display the guide navigation vertically above the guide.'),
];
$form['#validate'][] = 'localgov_base_theme_settings_validate';
}
/**
* Validate the theme settings form.
*/
function localgov_base_theme_settings_validate(&$form, FormStateInterface $form_state): void {
// Check if the mobile breakpoint is a number.
if (!is_numeric($form_state->getValue('localgov_base_mobile_breakpoint_js'))) {
$form_state->setErrorByName('localgov_base_mobile_breakpoint_js', t('The mobile breakpoint must be a number.'));
}
}
/**
* Implements hook_preprocess_html().
*/
function localgov_base_preprocess_html(&$variables): void {
// Add the mobile breakpoint value to drupalSettings for use in JavaScript.
if (!empty(theme_get_setting('localgov_base_mobile_breakpoint_js'))) {
$mobile_breakpoint = theme_get_setting('localgov_base_mobile_breakpoint_js');
$variables['#attached']['drupalSettings']['localgov_base']['mobileBreakpointJS'] = $mobile_breakpoint;
}
else {
$variables['#attached']['drupalSettings']['localgov_base']['mobileBreakpointJS'] = 768;
}
// Add the 'sticky-header' library if the sticky header setting is enabled.
if (theme_get_setting('localgov_base_header_behaviour') !== 'default') {
// Check if the user is logged out.
// We have much fewer calculations if we keep this to anonymous users only.
if (\Drupal::currentUser()->isAnonymous()) {
$variables['#attached']['library'][] = 'localgov_base/sticky-header';
$variables['attributes']['class'][] = 'sticky-header';
$variables['html_attributes']['class'] = [];
$variables['html_attributes']['class'][] = 'sticky-header-html';
// If 'sticky' is chosen add a class to the body element.
if (theme_get_setting('localgov_base_header_behaviour') === 'sticky') {
$variables['attributes']['class'][] = 'sticky-header--sticky';
}
// If 'scroll' is chosen add a class to the body element.
if (theme_get_setting('localgov_base_header_behaviour') === 'appears_on_scroll') {
$variables['attributes']['class'][] = 'sticky-header--scroll';
}
}
}
// Add the 'localgov_base_responsive_wysiwyg_tables' value to drupalSettings.
if (!empty(theme_get_setting('localgov_base_responsive_wysiwyg_tables'))) {
if (theme_get_setting('localgov_base_responsive_wysiwyg_tables') === TRUE || 1) {
$variables['#attached']['library'][] = 'localgov_base/responsive-tables';
}
}
}
/**
* Implements hook_preprocess_HOOK().
*/
function localgov_base_preprocess_page(&$variables): void {
// Work around for Drupal core issue.
// Blocks employ lazy building. This makes it difficult to determine from
// **Twig templates** if they will eventually produce empty content or not.
// @see https://www.drupal.org/node/953034
// @see https://www.drupal.org/forum/support/module-development-and-code-questions/2016-04-07/drupal-8-regions-with-and-empty#comment-12149518
$active_theme = \Drupal::service('theme.manager')->getActiveTheme()->getName();
$regions = array_keys(system_region_list($active_theme, REGIONS_VISIBLE));
$excluded_regions = [
'messages',
'disabled',
];
foreach ($regions as $region) {
if (in_array($region, $excluded_regions, TRUE)) {
continue;
}
$copy = $variables['page'][$region];
/** @var \Drupal\Core\Render\RendererInterface $renderer */
$renderer = \Drupal::service('renderer');
$rendered = $renderer->renderInIsolation($copy);
$variables['has_' . $region] = strlen(trim(strip_tags($rendered, '<drupal-render-placeholder><embed><hr><iframe><img><input><link><object><script><source><style><video>'))) > 0;
}
$variables['has_sidebars'] = $variables['has_sidebar_first'] || $variables['has_sidebar_second'];
if (!isset($variables['localgov_base_remove_css'])) {
$variables['#attached']['library'][] = 'localgov_base/global';
}
// We have a theme setting boolean for removing the pink background from
// unpublished content. If that setting is false,
// we will add the 'unpublished' library to our page.
if (theme_get_setting('localgov_base_add_unpublished_background_colour') === TRUE) {
$variables['#attached']['library'][] = 'localgov_base/unpublished-bg';
}
// Render the Back to Top link or not according to the theme setting.
if (theme_get_setting('localgov_base_show_back_to_top_link')) {
$variables['back_to_top'] = TRUE;
}
// Load the site configuration.
$site_config = \Drupal::config('system.site');
$site_403 = $site_config->get('page.403');
$site_404 = $site_config->get('page.404');
// Custom 403 and 404 pages.
// We have a variable in page.html.twig called default_status_content.
// This is used to determine if we should show the default 403/404 content,
// or if we should show the content of the node that is set as the 403/404
// page.
$variables['default_status_content'] = TRUE;
$route_match = Drupal::routeMatch();
$route = $route_match->getRouteName();
if ($route === 'entity.node.canonical') {
$node = $route_match->getParameter('node');
$node_id = $node->id();
$is_404_page_node = $site_404 === 'node/' . $node_id;
$is_403_page_node = $site_403 === 'node/' . $node_id;
if ($is_404_page_node && is_null($site_404)) {
$variables['default_status_content'] = TRUE;
}
else {
$variables['default_status_content'] = FALSE;
}
if ($is_403_page_node && is_null($site_403)) {
$variables['default_status_content'] = TRUE;
}
else {
$variables['default_status_content'] = FALSE;
}
}
}
/**
* Get the restricted width content types.
*
* @return array
* An array of restricted width content types.
*/
function _localgov_get_restricted_width_content_types() {
return [
'localgov_services_page',
'localgov_event',
'localgov_services_status',
'localgov_step_by_step_overview',
'localgov_publication_cover_page',
];
}
/**
* Implements hook_preprocess_HOOK().
*/
function localgov_base_preprocess_node(&$variables): void {
$node = $variables['node'];
$node_status = $variables['node']->isPublished();
if ($node instanceof NodeInterface && $variables['view_mode'] === 'full') {
// Restrict width of the content area to two-thirds of the screen for
// some content types. This is helpful to reduce the line-length for
// better readability.
$variables['restricted_width_content_types'] = [];
if (in_array($node->bundle(), _localgov_get_restricted_width_content_types(), TRUE)) {
$variables['restricted_width_content_types'][] = $node->bundle();
}
}
if ($node_status === FALSE) {
if (theme_get_setting('localgov_base_add_draft_note_to_unpublished_content') === TRUE) {
$state = $variables['node']->get('moderation_state')->getString();
if ($state == 'draft') {
$variables['label'] = t('[Draft]') . " " . $variables['node']->label();
}
}
}
// We have a theme setting to prepend 'archived' to the title of
// archived content, but we don't mind whether it is published or not.
// An archived state does not have to always be unpubilshed.
if (theme_get_setting('localgov_base_add_archived_note_to_archived_content') === TRUE) {
$state = $variables['node']->get('moderation_state')->getString();
if ($state == 'archived') {
$variables['label'] = t('[Archived]') . " " . $variables['node']->label();
}
}
if ($variables['node']->getType() === 'localgov_guides_page') {
if (theme_get_setting('localgov_base_localgov_guides_stacked_heading') === TRUE) {
$variables['stacked_heading'] = TRUE;
}
}
}
/**
* Implements hook_preprocess_HOOK().
*/
function localgov_base_preprocess_block(&$variables): void {
$route_match = Drupal::routeMatch();
$route = $route_match->getRouteName();
if ($variables['plugin_id'] === 'localgov_page_header_block') {
// Check if we are on a node page, latest version or a preview link.
if ($route === 'entity.node.canonical' || $route === 'entity.node.latest_version' || $route === 'entity.node.preview_link') {
$node = $route_match->getParameter('node');
$node_status = $node->isPublished();
$node_type = $node->getType();
if ($node_status === FALSE) {
if (theme_get_setting('localgov_base_add_draft_note_to_unpublished_content') === TRUE) {
$current_title = $variables['content'][0]['#title'];
$state = $node->get('moderation_state')->getString();
if ($state == 'draft') {
$variables['content'][0]['#title'] = t('[Draft]') . " " . $current_title;
}
}
}
// We have a theme setting to prepend 'archived' to the title of
// archived content, but we don't mind whether it is published or not.
// An archived state does not have to always be unpubilshed.
if (theme_get_setting('localgov_base_add_archived_note_to_archived_content') === TRUE) {
$current_title = $variables['content'][0]['#title'];
$state = $node->get('moderation_state')->getString();
if ($state == 'archived') {
$variables['content'][0]['#title'] = t('[Archived]') . " " . $current_title;
}
}
// Check if we should use the stacked heading pattern for guides.
// This places the guide title on top of the node title, all within the
// same h1 element, like the NHS design system.
if ($node_type === 'localgov_guides_page' && theme_get_setting('localgov_base_localgov_guides_stacked_heading') === TRUE) {
$node_title = $node->getTitle();
$variables['content'][0]['#subtitle'] = $node_title;
}
}
}
// Add vertical or horizontal class to the guides navigation block.
// In config, this will default to vertical for new installations, but remain
// horizontal for existing installations.
if ($variables['plugin_id'] === 'localgov_guides_contents') {
if (theme_get_setting('localgov_base_localgov_guides_vertical_navigation') === TRUE) {
$variables['attributes']['class'][] = 'block-localgov-guides-contents--navigation-vertical';
}
else {
$variables['attributes']['class'][] = 'block-localgov-guides-contents--navigation-horizontal';
}
}
}
/**
* Implements hook_preprocess_file_link().
*
* Changes:
* - Inserts file *type* and size into the theme variable.
* - Reformats file size. Example: 123.4KB.
* - Appends file metadata to the file link text.
*
* @see template_preprocess_file_link()
*/
function localgov_base_preprocess_file_link(&$variables): void {
$file = $variables['file'];
$filename = $file->getFilename();
$file_extension = pathinfo($filename, PATHINFO_EXTENSION);
$variables['file_type'] = strtoupper($file_extension);
// 123.45 KB -> 123.45KB
$variables['file_size'] = strtr($variables['file_size'], [' ' => '']);
// Load media entity from file.
$entity = $file->_referringItem?->getParent()?->getParent()?->getEntity();
$media = ($entity instanceof MediaInterface) ? $entity : NULL;
if (isset($variables['link']['#title'])) {
$document_title = $variables['link']['#title'];
if ($media) {
// Assume some defaults for document media type.
$view_mode = 'default';
$media_file_field_name = ($media->bundle() === 'document') ? 'field_media_document' : NULL;
// Initialise description variables.
$use_description = FALSE;
$description = '';
if ($media_file_field_name) {
$entity_display_repository = \Drupal::service('entity_display.repository');
$entity_display = $entity_display_repository->getViewDisplay($media->getEntityTypeId(), $media->bundle(), $view_mode);
$field_display = $entity_display->getComponent($media_file_field_name);
if ($field_display && isset($field_display['settings']['use_description_as_link_text'])) {
$use_description = $field_display['settings']['use_description_as_link_text'];
}
}
// Get the description from the file field if available.
if ($use_description && $file->_referringItem) {
$description = $file->_referringItem->description ?? '';
}
// Use description as link text if enabled and description exists.
if ($use_description && !empty($description)) {
$document_title = $description;
}
else {
// Otherwise, see about using the media name.
$media_name = $media->get('name')->getString();
// If the media name is set and different to the
// $variables['link']['#title'], we will use that instead.
$document_title = ($media_name && $media_name !== $document_title) ? $media_name : $document_title;
}
}
$variables['link']['#title'] = [
'#markup' => "{$document_title} <span class=\"file-meta\">(<span class=\"file-type\">{$variables['file_type']}</span>, <span class=\"file-size\">{$variables['file_size']}</span>)</span>",
];
}
else {
$file_title = $variables['link'];
$variables['link'] = [
'#markup' => "{$file_title} <span class=\"file-meta\">(<span class=\"file-type\">{$variables['file_type']}</span>, <span class=\"file-size\">{$variables['file_size']}</span>)</span>",
];
}
}
/**
* Implements hook_views_pre_render().
*/
function localgov_base_views_pre_render(ViewExecutable $view): void {
if ($view->storage->id() === 'localgov_sitewide_search') {
$view->element['#attached']['library'][] = 'localgov_base/sitewide-search';
}
if ($view->id() === 'service_status' && $view->current_display === 'service_status_page') {
$view->element['#attached']['library'][] = 'localgov_base/service-statuses';
}
}
/**
* Implements hook_preprocess_container().
*/
function localgov_base_preprocess_container(&$variables): void {
// See https://www.drupal.org/project/drupal/issues/1852090
// Ensure the container has an ID.
if (isset($variables['element']['#id'])) {
$original_id = $variables['element']['#id'];
// Check if the ID starts with 'edit-actions'.
if (strpos($original_id, 'edit-actions') === 0) {
$random_suffix = Crypt::randomBytesBase64(8);
$random_id = $original_id . '--' . $random_suffix;
// Keep a stable selector but allow ID randomization.
$variables['attributes']['id'] = $random_id;
$variables['attributes']['data-drupal-selector'] = $original_id;
$variables['element']['#attributes']['data-drupal-selector'] = $original_id;
$variables['element']['#id'] = $random_id;
}
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function localgov_base_form_preview_link_entity_form_alter(&$form, $form_state, $form_id): void {
$form['#attached']['library'][''] = 'localgov_base/preview-link';
}
/**
* Implements hook_preprocess_guides_prev_next_block().
*/
function localgov_base_preprocess_guides_prev_next_block(&$variables): void {
if ($variables['previous_url'] instanceof Url) {
$variables['previous_url']->setOption('fragment', 'lgd-guides__title');
}
if ($variables['next_url'] instanceof Url) {
$variables['next_url']->setOption('fragment', 'lgd-guides__title');
}
}