-
Notifications
You must be signed in to change notification settings - Fork 31
/
oe_theme.theme
2600 lines (2339 loc) · 96.2 KB
/
oe_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
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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* Functions to support theming.
*/
declare(strict_types=1);
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Template\Attribute;
use Drupal\Core\Url;
use Drupal\block\BlockInterface;
use Drupal\media\MediaInterface;
use Drupal\media\Plugin\media\Source\Image;
use Drupal\media\Plugin\media\Source\OEmbed;
use Drupal\media_avportal\Plugin\media\Source\MediaAvPortalPhotoSource;
use Drupal\media_avportal\Plugin\media\Source\MediaAvPortalSourceInterface;
use Drupal\media_avportal\Plugin\media\Source\MediaAvPortalVideoSource;
use Drupal\node\NodeInterface;
use Drupal\oe_content_person\Entity\PersonJobInterface;
use Drupal\oe_media_iframe\Plugin\media\Source\Iframe;
use Drupal\oe_theme\DocumentMediaValueExtractor;
use Drupal\oe_theme\ValueObject\DateValueObject;
use Drupal\oe_theme\ValueObject\FileValueObject;
use Drupal\oe_theme\ValueObject\GalleryItemValueObject;
use Drupal\oe_theme\ValueObject\ImageValueObject;
use Drupal\oe_theme_helper\EuropeanUnionLanguages;
/**
* Implements hook_form_FORM_ID_alter().
*/
function oe_theme_form_system_theme_settings_alter(&$form, FormStateInterface &$form_state, $form_id = NULL) {
// Work-around for a core bug affecting admin themes.
// @todo remove this once the core issue below is fixed.
// @link https://www.drupal.org/node/943212
if (isset($form_id)) {
return;
}
$form['component_library'] = [
'#type' => 'select',
'#title' => t('Component library'),
'#options' => [
'ec' => t('European Commission'),
'eu' => t('European Union'),
],
'#default_value' => theme_get_setting('component_library'),
'#description' => t('Choose "European Commission" for websites hosted under the ec.europa.eu domain and "European Union" for websites hosted under the europa.eu domain.'),
];
$form['branding'] = [
'#type' => 'select',
'#title' => t('Branding'),
'#options' => [
'core' => t('Core'),
'standardised' => t('Standardised'),
],
'#default_value' => theme_get_setting('branding') ?? 'core',
'#description' => t('Choose the ECL branding for your website. Use "Core" for cross-institutional sites and "Standardised" for DG/Agency specific sites.'),
];
}
/**
* Implements hook_preprocess().
*/
function oe_theme_preprocess(&$variables) {
$variables['ecl_component_library'] = theme_get_setting('component_library');
$variables['ecl_branding'] = theme_get_setting('branding') ?? 'core';
$variables['ecl_images_path'] = base_path() . \Drupal::service('extension.list.theme')->getPath('oe_theme') . '/dist/' . $variables['ecl_component_library'] . '/images';
$variables['ecl_icon_path'] = $variables['ecl_images_path'] . '/icons/sprites/icons.svg';
$variables['ecl_logo_path'] = $variables['ecl_images_path'] . '/logo';
$variables['ecl_social_icon_path'] = base_path() . \Drupal::service('extension.list.theme')->getPath('oe_theme') . '/dist/ec/images/social-icons/sprites/icons-social.svg';
$variables['ecl_icon_social_media_path'] = $variables['ecl_images_path'] . '/icons-social-media/sprites/icons-social-media.svg';
$variables['ecl_icon_flag_path'] = $variables['ecl_images_path'] . '/icons-flag/sprites/icons-flag.svg';
$variables['ecl_icon_flag_non_members_path'] = $variables['ecl_images_path'] . '/icons-flag/sprites/icons-flag-non-members.svg';
$variables['current_language_id'] = \Drupal::languageManager()->getCurrentLanguage()->getId();
}
/**
* Implements hook_preprocess_breadcrumb().
*/
function oe_theme_preprocess_breadcrumb(array &$variables): void {
$cacheability = CacheableMetadata::createFromRenderArray($variables);
$cacheability->addCacheContexts(['route']);
$cacheability->applyTo($variables);
$request = \Drupal::request();
$route_match = \Drupal::routeMatch();
$route = $route_match->getRouteObject();
$title = \Drupal::service('title_resolver')->getTitle($request, $route);
unset($variables['links']);
$variables['links'] = array_map(function ($item) use ($variables) {
return [
'path' => _oe_theme_sanitize_url($item['url']),
'label' => $item['text'],
'negative' => $variables['ecl_branding'] === 'core' && $variables['ecl_component_library'] === 'ec',
];
}, $variables['breadcrumb']);
// Add the title to the segments only if it's not empty.
if (!empty($title)) {
$variables['links'][] = [
'label' => $title,
];
}
$variables['navigation_text'] = '';
$variables['ellipsis_label'] = '';
$variables['icon_file_path'] = $variables['ecl_icon_path'];
}
/**
* Implements hook_preprocess_menu__main().
*/
function oe_theme_preprocess_menu__main(array &$variables): void {
// Massage data to be compliant with ECL navigation menu data structure.
$variables['links'] = array_map(function ($item) {
return [
'label' => $item['title'],
'href' => _oe_theme_sanitize_url($item['url']),
'is_current' => $item['in_active_trail'],
'external' => \Drupal::service('oe_theme_helper.external_links')->isExternalLink($item['url']),
];
}, $variables['items']);
foreach ($variables['items'] as $name => $link) {
$variables['links'][$name]['children'] = array_map(function ($item) {
return [
'label' => $item['title'],
'href' => _oe_theme_sanitize_url($item['url']),
'is_current' => $item['in_active_trail'],
'external' => \Drupal::service('oe_theme_helper.external_links')->isExternalLink($item['url']),
];
}, $variables['items'][$name]['below']);
}
$variables['close'] = [
'label' => t('Close'),
'icon_name' => $variables['ecl_component_library'] === 'ec' ? 'close' : 'close-filled',
'icon_size' => $variables['ecl_component_library'] === 'ec' ? 'm' : 's',
'hide_label' => $variables['ecl_component_library'] === 'ec',
];
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*
* Adds a template suggestion for type time input fields.
*/
function oe_theme_theme_suggestions_input_alter(array &$suggestions, array $variables): void {
$element = $variables['element'];
if ($element['#type'] === 'date' && $element['#attributes']['type'] === 'time') {
$suggestions[] = 'input__time';
}
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*
* Adds a bare, markup-free template suggestion to all paragraph fields.
*/
function oe_theme_theme_suggestions_field_alter(array &$suggestions, array $variables): void {
$element = $variables['element'];
// Do not output field labels and wrapping markup for paragraph fields.
if (isset($element['#entity_type']) && $element['#entity_type'] === 'paragraph') {
// Prepend the new suggestion to the list. This will put it right after the
// default field template. By doing this we allow to override single
// fields, while keeping all the rest markup-free.
array_unshift($suggestions, 'field__bare');
}
// Add suggestions for all 'Entity reference revisions' for each target type.
if ($element['#field_type'] === 'entity_reference_revisions') {
/** @var \Drupal\field\Entity\FieldConfig $field_definition */
$field_definition = $element['#items']->getFieldDefinition();
// Add suggestions based on the referenced entity type.
$suggestions[] = 'field__entity_reference_revisions__' . $field_definition->getSetting('target_type');
$suggestions[] = 'field__entity_reference_revisions__' . $field_definition->getSetting('target_type') . '__' . $field_definition->getName();
// Add suggestions based on the parent entity and bundle types.
$suggestions[] = 'field__entity_reference_revisions__' . $element['#entity_type'] . '__' . $element['#bundle'];
$suggestions[] = 'field__entity_reference_revisions__' . $element['#entity_type'] . '__' . $element['#bundle'] . '__' . $field_definition->getName();
}
// Append view mode to default field suggestions, if any.
if (isset($element['#view_mode'])) {
$suggestions[] = 'field__' . $element['#field_type'] . '__' . $element['#view_mode'];
$suggestions[] = 'field__' . $element['#field_name'] . '__' . $element['#view_mode'];
$suggestions[] = 'field__' . $element['#entity_type'] . '__' . $element['#bundle'] . '__' . $element['#view_mode'];
$suggestions[] = 'field__' . $element['#entity_type'] . '__' . $element['#field_name'] . '__' . $element['#view_mode'];
$suggestions[] = 'field__' . $element['#entity_type'] . '__' . $element['#field_name'] . '__' . $element['#bundle'] . '__' . $element['#view_mode'];
}
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function oe_theme_theme_suggestions_paragraph_alter(array &$suggestions, array $variables): void {
/** @var \Drupal\paragraphs\ParagraphInterface $paragraph */
$paragraph = $variables['elements']['#paragraph'];
if ($paragraph->bundle() === 'oe_content_row') {
$variant = $paragraph->get('oe_paragraphs_variant')->first()->value;
$suggestions[] = 'paragraph__' . $paragraph->bundle() . '__variant_' . $variant;
}
}
/**
* Implements hook_preprocess_paragraph().
*/
function oe_theme_preprocess_paragraph__oe_links_block(array &$variables): void {
$external_links_service = \Drupal::service('oe_theme_helper.external_links');
// Massage data to be compliant with ECL links block component data structure.
foreach (Element::children($variables['content']['field_oe_links']) as $index) {
$url = $variables['content']['field_oe_links'][$index]['#url'];
$variables['links'][] = [
'label' => $variables['content']['field_oe_links'][$index]['#title'],
'url' => _oe_theme_sanitize_url($url),
'is_external' => $external_links_service->isExternalLink($url),
];
}
}
/**
* Implements hook_preprocess_paragraph__oe_accordion().
*/
function oe_theme_preprocess_paragraph__oe_accordion(array &$variables): void {
// Massage data to be compliant with ECL Accordion2 component data structure.
$builder = \Drupal::entityTypeManager()->getViewBuilder('paragraph');
$variables['identifier'] = 'paragraph-' . $variables['paragraph']->id();
$variables['items'] = [];
/** @var \Drupal\entity_reference_revisions\Plugin\Field\FieldType\EntityReferenceRevisionsItem $field_item */
foreach ($variables['paragraph']->get('field_oe_paragraphs') as $field_item) {
$paragraph = \Drupal::service('entity.repository')->getTranslationFromContext($field_item->entity);
$variables['items'][] = [
'title' => $builder->viewField($paragraph->get('field_oe_text')),
'body' => $builder->viewField($paragraph->get('field_oe_text_long')),
];
}
}
/**
* Implements hook_preprocess_paragraph__oe_contextual_navigation().
*/
function oe_theme_preprocess_paragraph__oe_contextual_navigation(array &$variables): void {
// Massage data to be compliant with ECL 'context_nav' pattern data structure.
/** @var \Drupal\paragraphs\Entity\Paragraph $paragraph */
$paragraph = $variables['paragraph'];
$variables['limit'] = $paragraph->get('field_oe_limit')->value;
$variables['more_label'] = $paragraph->get('field_oe_text')->value;
$variables['items'] = [];
$external_links_service = \Drupal::service('oe_theme_helper.external_links');
foreach (Element::children($variables['content']['field_oe_links']) as $index) {
$url = $variables['content']['field_oe_links'][$index]['#url'];
$variables['items'][] = [
'label' => $variables['content']['field_oe_links'][$index]['#title'],
'href' => _oe_theme_sanitize_url($url),
'is_external' => $external_links_service->isExternalLink($url),
];
}
}
/**
* Implements hook_preprocess_menu_local_tasks().
*/
function oe_theme_preprocess_menu_local_tasks(array &$variables): void {
// Sort local tasks by weight.
uasort($variables['primary'], [
'\Drupal\Component\Utility\SortArray',
'sortByWeightProperty',
]);
uasort($variables['secondary'], [
'\Drupal\Component\Utility\SortArray',
'sortByWeightProperty',
]);
foreach ($variables['primary'] as $link) {
/** @var Drupal\Core\Url $url */
$url = $link['#link']['url'];
if ($url->access($variables['user'])) {
$variables['primary_links'][] = [
'link' => [
'path' => $url,
'label' => $link['#link']['title'],
'type' => 'standalone',
],
'is_active' => $link['#active'],
];
}
}
foreach ($variables['secondary'] as $link) {
/** @var Drupal\Core\Url $url */
$url = $link['#link']['url'];
if ($url->access($variables['user'])) {
$variables['secondary_links'][] = [
'link' => [
'path' => $url,
'label' => $link['#link']['title'],
'type' => 'standalone',
],
'is_active' => $link['#active'],
];
}
}
}
/**
* Implements hook_preprocess_input__radio().
*/
function oe_theme_preprocess_input__radio(array &$variables): void {
_oe_theme_preprocess_input_label_wrapper($variables);
}
/**
* Implements hook_preprocess_input__checkbox().
*/
function oe_theme_preprocess_input__checkbox(array &$variables): void {
_oe_theme_preprocess_input_label_wrapper($variables);
}
/**
* Implements template_preprocess_input__date().
*/
function oe_theme_preprocess_input__date(array &$variables): void {
$php_date_format = $variables['element']['#date_date_format'] ?? 'Y-m-d';
$format_map = [
// @todo find a cleverer way of mapping PHP date formats to the expected
// ECL ones.
'Y-m-d' => 'YYYY-MM-DD',
];
$ecl_format = $format_map[$php_date_format] ?? 'YYYY-MM-DD';
$variables['#attached']['library'][] = 'oe_theme/ecl_datepicker';
$variables['#attached']['drupalSettings']['oe_theme']['ecl_datepicker_format'] = $ecl_format;
$variables['placeholder'] = $ecl_format;
}
/**
* Implements template_preprocess_input__submit().
*/
function oe_theme_preprocess_input__submit(array &$variables): void {
if (isset($variables['element']['#button_type'])) {
$variables['variant'] = $variables['element']['#button_type'];
}
}
/**
* Helper function to additionally preprocess checkbox and radio elements.
*
* Moves the label element to the input template and creates some helper
* variables to be used with the ECL templates.
*
* @param array $variables
* Set of available variables.
*/
function _oe_theme_preprocess_input_label_wrapper(array &$variables): void {
$element = &$variables['element'];
$variables['invalid'] = !empty($element['#errors']);
if (isset($element['#title']) && $element['#title'] !== '') {
$variables['title'] = ['#markup' => $element['#title']];
}
// Generate the extra attributes array from the general attributes.
$extra_attributes = !empty($variables['attributes']) ? $variables['attributes'] : [];
$predefined_attributes = [
'id' => 'id',
'name' => 'name',
'value' => 'value',
'disabled' => 'disabled',
];
$extra_attributes = array_diff_key($extra_attributes, $predefined_attributes);
foreach ($extra_attributes as $key => $value) {
if (is_string($value)) {
$variables['input_attributes'][] = [
'name' => $key,
'value' => $value,
];
}
}
}
/**
* Implements hook_preprocess_radios().
*/
function oe_theme_preprocess_radios(array &$variables): void {
$element = &$variables['element'];
$variables['invalid'] = !empty($element['#errors']);
if (isset($element['#title']) && $element['#title'] !== '') {
$variables['label'] = ['#markup' => $element['#title']];
}
}
/**
* Implements hook_preprocess_fieldset().
*/
function oe_theme_preprocess_fieldset(array &$variables): void {
if (isset($variables['element']['#type']) && $variables['element']['#type'] == 'radios') {
$variables['legend_display'] = FALSE;
}
}
/**
* Implements hook_preprocess_form_element().
*
* Disables displaying of the label for checkbox and radio elements, as the
* label is already rendered in the input template.
*/
function oe_theme_preprocess_form_element(array &$variables): void {
if (in_array($variables['element']['#type'], ['checkbox', 'radio'])) {
$variables['label_display'] = 'none';
}
$ecl_type_mappings = [
'textfield' => 'text-input',
'password' => 'text-input',
'textarea' => 'text-area',
'select' => 'select',
];
if (isset($ecl_type_mappings[$variables['element']['#type']])) {
$variables['ecl_type'] = $ecl_type_mappings[$variables['element']['#type']];
}
}
/**
* Implements hook_preprocess_field().
*
* Add paragraph formatting to body.
*/
function oe_theme_preprocess_field(&$variables, $hook): void {
// Add the ECL image class to the image fields.
$field_name = $variables['field_name'];
if (in_array($field_name, ['oe_media_avportal_photo'])) {
foreach ($variables['items'] as &$item) {
$item['content']['#attributes']['class'][] = 'ecl-u-width-100';
$item['content']['#attributes']['class'][] = 'ecl-u-height-auto';
}
}
}
/**
* Implements hook_preprocess_pager().
*
* @see template_preprocess_pager()
*
* Generates pagination items for ECL pagination component.
*/
function oe_theme_preprocess_pager(array &$variables): void {
/** @var \Drupal\Core\Pager\PagerManagerInterface $pager_manager */
$pager_manager = \Drupal::service('pager.manager');
$element = $variables['pager']['#element'];
$parameters = $variables['pager']['#parameters'];
$route_name = $variables['pager']['#route_name'];
$route_parameters = $variables['pager']['#route_parameters'] ?? [];
$pager = $pager_manager->getPager($element);
global $pager_page_array, $pager_total;
// Nothing to do if there is no pager.
if (!isset($pager)) {
return;
}
// Maximum page number for this pager.
$pager_max = $pager->getTotalPages();
// Nothing to do if there is only one page.
if ($pager_max <= 1) {
return;
}
// Max number of pages to display next to the current page (left/right).
// The first and last page links are excluded from this count.
$pager_offset = 2;
// The page we are currently paged to.
$pager_current = $pager->getCurrentPage() + 1;
// First page of the pagination calculated by offset from current.
$pager_first = $pager_offset < $pager_current ? $pager_current - $pager_offset : 1;
// Last page of the pagination calculated by offset from current.
$pager_last = $pager_current + $pager_offset;
// Remove extra pages if last page is larger than maximum.
if ($pager_last > $pager_max) {
$pager_last = $pager_max;
}
// Array to collect the pagination items.
$ecl_items = [];
// Prepare navigation link item for previous/next links with defaults.
$nav_link = [
'link' => [
'icon' => [
'path' => $variables['ecl_icon_path'],
'type' => 'ui',
'name' => 'corner-arrow',
'size' => 'xs',
'transform' => 'rotate-270',
],
],
];
// Prepare ellipsis item.
$ellipsis = [
'type' => 'ellipsis',
'label' => '...',
];
// Add 'Previous' navigation link.
if ($pager_current > 1) {
$options = [
'query' => $pager_manager->getUpdatedParameters($parameters, $element, $pager->getCurrentPage() - 1),
];
$nav_link['link']['link'] = [
'path' => Url::fromRoute($route_name, $route_parameters, $options),
'label' => t('Previous'),
'icon_position' => 'before',
'hide_label' => $variables['ecl_component_library'] === 'ec',
];
$nav_link['type'] = 'previous';
$nav_link['aria_label'] = t('Go to previous page');
$ecl_items[] = $nav_link;
}
// Add first page link.
if ($pager_first != $pager_current) {
$options = [
'query' => $pager_manager->getUpdatedParameters($parameters, $element, 0),
];
$ecl_items[] = [
'type' => 'first',
'link' => [
'link' => [
'path' => Url::fromRoute($route_name, $route_parameters, $options),
'label' => '1',
],
],
'aria_label' => t('Go to page @number', ['@number' => 1]),
];
}
// Add first ellipsis.
if ($pager_current - $pager_offset > 2) {
$ecl_items[] = $ellipsis;
}
// Generate page link items.
for ($i = $pager_first; $i <= $pager_current + $pager_offset && $i <= $pager_last; $i++) {
$label = (string) $i;
if ($i == $pager_current) {
$ecl_items[] = [
'type' => 'current',
'label' => $label,
'aria_label' => t('Page @number', ['@number' => $label]),
];
}
else {
// Skip first or last page.
if ($i == 1 || $i == $pager_max) {
continue;
}
$options = [
'query' => $pager_manager->getUpdatedParameters($parameters, $element, $i - 1),
];
$ecl_items[] = [
'link' => [
'link' => [
'path' => Url::fromRoute($route_name, $route_parameters, $options),
'label' => $label,
],
],
'aria_label' => t('Go to page @number', ['@number' => $i]),
];
}
}
// Add second ellipsis.
if ($pager_last < $pager_max - 1) {
$ecl_items[] = $ellipsis;
}
// Add last page link.
if ($pager_max != $pager_current) {
$options = [
'query' => $pager_manager->getUpdatedParameters($parameters, $element, $pager_max - 1),
];
$ecl_items[] = [
'type' => 'last',
'link' => [
'link' => [
'path' => Url::fromRoute($route_name, $route_parameters, $options),
'label' => (string) $pager_max,
],
],
'aria_label' => t('Go to page @number', ['@number' => $pager_max]),
];
}
// Add 'Next' navigation link.
if ($pager_current < $pager_max) {
$options = [
'query' => $pager_manager->getUpdatedParameters($parameters, $element, $pager->getCurrentPage() + 1),
];
$nav_link['link']['link'] = [
'path' => Url::fromRoute($route_name, $route_parameters, $options),
'label' => t('Next'),
'icon_position' => 'after',
'hide_label' => $variables['ecl_component_library'] === 'ec',
];
$nav_link['type'] = 'next';
$nav_link['aria_label'] = t('Go to next page');
$nav_link['link']['icon']['transform'] = 'rotate-90';
$ecl_items[] = $nav_link;
}
$variables['label'] = t('Pagination');
$variables['ecl_items'] = $ecl_items;
}
/**
* Implements hook_preprocess_links().
*/
function oe_theme_preprocess_links__language_block(array &$variables): void {
$processed_languages = _oe_theme_preprocess_languages($variables['links'], $variables['current_language_id']);
$variables = array_merge($variables, $processed_languages);
}
/**
* Implements template_preprocess_language_selection_page_content().
*/
function oe_theme_preprocess_language_selection_page_content(&$variables) {
$processed_languages = _oe_theme_preprocess_languages($variables['language_links']['#items']);
$variables = array_merge($variables, $processed_languages);
}
/**
* Helper function to process links for language switcher block and page.
*
* @param array $link_items
* The link items passed from the preprocess function.
* @param string $current_language_id
* The current language id.
*
* @return array
* Array of variables to be used for rendering.
*/
function _oe_theme_preprocess_languages(array $link_items, string $current_language_id = ''): array {
$variables = [];
$language_config_storage = \Drupal::entityTypeManager()->getStorage('configurable_language');
// Map Norwegian Bokmål and Chinese language codes.
$map_other_language_codes = [
'nb' => 'no',
'zh-hans' => 'zh',
];
foreach ($link_items as $language_code => $link) {
// Handle both cases (block, page) when the link array structure differ.
if (isset($link['link'])) {
/** @var \Drupal\Core\Url $url */
$url = $link['link']['#url'];
$href = $url
->setOptions($link['link']['#options'])
->setAbsolute(TRUE)
->toString();
$label = $link['link']['#title'];
}
else {
$href = $link['#url']->toString();
$label = $link['#title'];
}
if (EuropeanUnionLanguages::hasLanguage($language_code)) {
$language_internal = EuropeanUnionLanguages::getInternalLanguageCode($language_code);
}
else {
$language_internal = $map_other_language_codes[$language_code] ?? $language_code;
}
$language = [
'path' => $href,
'label' => $label,
// Pass original language code which can be useful for some other
// preprocessors.
'hreflang' => $language_code,
'lang' => $language_internal,
];
if (!empty($current_language_id) && $language_code === $current_language_id) {
$variables['current_language'] = [
'path' => $href,
'hreflang' => $language_code,
'lang' => $language_internal,
'label' => $link['link']['#title'],
];
$language['active'] = TRUE;
}
// Take language categories and order languages accordingly.
$language_config = $language_config_storage->load($language_code);
$category = $language_config->getThirdPartySetting('oe_multilingual', 'category');
switch ($category) {
case 'eu':
$variables['eu_languages'][] = $language;
break;
case 'non_eu':
$variables['non_eu_languages'][] = $language;
break;
default:
$variables['languages'][] = $language;
}
}
return $variables;
}
/**
* Implements hook_preprocess_links__oe_multilingual_content_language_block().
*
* Adds extra variables regarding the current and the unavailable languages,
* and prepares the links for rendering.
*/
function oe_theme_preprocess_links__oe_multilingual_content_language_block(array &$variables): void {
// Generate the label for the unavailable language.
/** @var \Drupal\Core\Language\LanguageInterface[] $languages */
$languages = \Drupal::service('language_manager')->getNativeLanguages();
$variables['unavailable_language'] = $languages[$variables['current_language_id']]->getName();
// Normalize the links to an array of optional languages suitable for the ECL.
$variables['languages'] = [];
foreach ($variables['links'] as $language_code => $link) {
/** @var \Drupal\Core\Url $url */
$url = $link['link']['#url'];
$href = $url
->setOptions($link['link']['#options'])
->setAbsolute(TRUE)
->toString();
$variables['languages'][] = [
'href' => $href,
'hreflang' => $language_code,
'label' => $link['link']['#title'],
'current' => FALSE,
];
}
// Add the current language to the list.
$multilingual_helper = \Drupal::service('oe_multilingual.helper');
$entity = $multilingual_helper->getEntityFromCurrentRoute();
/** @var \Drupal\Core\Entity\EntityInterface $translation */
$translation = $multilingual_helper->getCurrentLanguageEntityTranslation($entity);
// If we don't have a language id defined yet, the current translation wasn't
// saved, so we don't add it to the list.
if ($translation->language()->getId() !== 'und') {
$variables['languages'][] = [
'href' => $translation->toUrl()->setAbsolute(TRUE)->toString(),
'hreflang' => $translation->language()->getId(),
'label' => $languages[$translation->language()->getId()]->getName(),
'current' => TRUE,
];
}
// Generate required ids.
$variables['expandable_id'] = Html::getUniqueId('ecl-expandable');
}
/**
* Implements hook_preprocess_HOOK().
*/
function oe_theme_preprocess_pattern_page_header(array &$variables): void {
// Generate unique element ID for the expandable component.
$variables['expandable_id'] = Html::getUniqueId('ecl-expandable');
}
/**
* Implements hook_preprocess_HOOK().
*/
function oe_theme_preprocess_pattern_gallery__preview(array &$variables): void {
foreach ($variables['items'] as &$item) {
// Being 'type' an UI Patterns reserved word, we need to use 'media_type'.
$item['type'] = $item['media_type'];
$item = GalleryItemValueObject::fromArray($item);
}
}
/**
* Implements hook_preprocess_HOOK().
*/
function oe_theme_preprocess_pattern_accordion(array &$variables): void {
// If no identifier is provided generate a random, unique one.
if (empty($variables['identifier'])) {
$variables['identifier'] = Html::getUniqueId('ecl-accordion');
}
}
/**
* Implements hook_preprocess_HOOK().
*/
function oe_theme_preprocess_pattern_dropdown(array &$variables): void {
$variables['id'] = Html::getUniqueId('ecl-button-dropdown');
}
/**
* Implements hook_preprocess_html() for html.html.twig.
*
* Add css class for splash page.
*/
function oe_theme_preprocess_html(array &$variables): void {
if ('language_selection_page' === \Drupal::request()->attributes->get('_route')) {
$variables['attributes']['class'][] = 'page-splash';
}
// Load specific ECL component library rtl styling for oe_theme and its
// subthemes for pages with html attribute of rtl.
if ($variables['html_attributes']['dir']->value() === 'rtl') {
$active_theme = \Drupal::theme()->getActiveTheme();
if ($active_theme->getName() === 'oe_theme' || array_key_exists('oe_theme', $active_theme->getBaseThemeExtensions())) {
$component_library = theme_get_setting('component_library') ?? 'ec';
$variables['#attached']['library'][] = 'oe_theme/style_' . $component_library . '_rtl';
}
}
}
/**
* Implements hook_preprocess_paragraph() for paragraph--oe-list-item.html.twig.
*/
function oe_theme_preprocess_paragraph__oe_list_item(array &$variables): void {
/** @var \Drupal\paragraphs\Entity\Paragraph $paragraph */
$paragraph = $variables['paragraph'];
$list_item_variant = $paragraph->get('oe_paragraphs_variant')->first()->value;
$variables['variant'] = $list_item_variant ?? 'default';
if (!$paragraph->get('field_oe_link')->isEmpty()) {
$url = $paragraph->get('field_oe_link')->first()->getUrl();
if (!$url->isRouted() || !in_array($url->getRouteName(), [
'<nolink>',
'<button>',
])) {
$variables['url'] = $url;
$variables['external_link'] = \Drupal::service('oe_theme_helper.external_links')->isExternalLink($url);
}
}
$cacheability = CacheableMetadata::createFromRenderArray($variables);
// Extract the image if present.
if (!$paragraph->get('field_oe_image')->isEmpty() && $variables['variant'] !== 'default') {
/** @var \Drupal\file\Plugin\Field\FieldType\FileItem $image_item */
$image_item = $paragraph->get('field_oe_image')->first();
$file = $image_item->get('entity')->getValue();
$variables['image'] = [
'src' => \Drupal::service('file_url_generator')->generateString($file->getFileUri()),
'alt' => $image_item->get('alt')->getValue(),
];
// Caches are handled by the formatter usually. Since we are not rendering
// the original render arrays, we need to propagate our caches to the
// paragraph template.
$cacheability->addCacheableDependency($file);
}
// Prepare the date fields if date is available.
if (!$paragraph->get('field_oe_date')->isEmpty()) {
$variables['date'] = DateValueObject::fromDateTimeItem($paragraph->get('field_oe_date')->first());
// Add the timezone context to the cache.
// @see \Drupal\datetime\Plugin\Field\FieldFormatter\DateTimeFormatterBase::buildDate()
$cacheability->addCacheContexts(['timezone']);
}
// Prepare the metas if available.
if (!$paragraph->get('field_oe_meta')->isEmpty()) {
$metas = [];
foreach ($paragraph->get('field_oe_meta') as $item) {
$metas[] = $item->value;
}
$variables['meta'] = $metas;
}
$cacheability->applyTo($variables);
}
/**
* Implements hook_preprocess_paragraph() for paragraph--oe-list-item-block.html.twig.
*/
function oe_theme_preprocess_paragraph__oe_list_item_block(array &$variables): void {
/** @var \Drupal\paragraphs\Entity\Paragraph $paragraph */
$paragraph = $variables['paragraph'];
// Set up the correct variant name.
$variables['variant'] = 'list_item_block_' . $paragraph->get('field_oe_list_item_block_layout')->first()->value;
$variables['items'] = [];
foreach ($variables['paragraph']->get('field_oe_paragraphs') as $delta => $item) {
$variables['items'][] = $variables['content']['field_oe_paragraphs'][$delta];
}
// Prepare the button variables if a link has been specified.
if ($paragraph->get('field_oe_link')->isEmpty()) {
return;
}
/** @var \Drupal\link\Plugin\Field\FieldType\LinkItem $link_item */
$link_item = $paragraph->get('field_oe_link')->first();
$url = $link_item->getUrl();
if (!$url->isRouted() || !in_array($url->getRouteName(), [
'<nolink>',
'<button>',
])) {
$variables['button_url'] = $url;
$variables['external_link'] = \Drupal::service('oe_theme_helper.external_links')->isExternalLink($url);
}
$variables['button_label'] = $link_item->get('title')->getValue();
}
/**
* Implements hook_preprocess_paragraph() for paragraph--oe-content-row--variant-inpage_navigation.html.twig.
*
* Prepares the inpage navigation by creating links for the inner paragraphs
* that implement a title.
*/
function oe_theme_preprocess_paragraph__oe_content_row__variant_inpage_navigation(array &$variables): void {
/** @var \Drupal\paragraphs\Entity\Paragraph $paragraph */
$paragraph = $variables['paragraph'];
if ($paragraph->get('field_oe_paragraphs')->isEmpty()) {
return;
}
$title = t('Page contents');
if (!$paragraph->get('field_oe_title')->isEmpty()) {
$title = $paragraph->get('field_oe_title')->value;
}
// Add the inpage nav library.
$variables['inpage'] = [
'#theme' => 'oe_theme_helper_inpage_navigation_block',
'#title' => $title,
'#attached' => [
'library' => [
'oe_theme/inpage_navigation',
],
],
];
}
/**
* Implements hook_preprocess_paragraph() for paragraph--oe-text-feature-media.html.twig.
*/
function oe_theme_preprocess_paragraph__oe_text_feature_media(array &$variables): void {
$variables['image'] = [];
/** @var \Drupal\paragraphs\Entity\Paragraph $paragraph */
$paragraph = $variables['paragraph'];
// Set pattern variant.
if (!$paragraph->get('oe_paragraphs_variant')->isEmpty()) {
$variant = $paragraph->get('oe_paragraphs_variant')->first()->value;
// The default variant of the paragraph is the 'Left simple'
// variant of pattern.
$variables['variant'] = $variant === 'default' ? 'left_simple' : $variant;
}
// Link field doesn't exist in oe_paragraphs <= 1.8.0, so ensure it exists.
if ($paragraph->hasField('field_oe_link') && !$paragraph->get('field_oe_link')->isEmpty()) {
$link_field = $paragraph->get('field_oe_link')->first();
$variables['link'] = [
'path' => $link_field->getUrl()->toString(),
'label' => $link_field->get('title')->getValue(),
];
}
// Set image caption.
$variables['caption'] = '';
if (!$paragraph->get('field_oe_plain_text_long')->isEmpty()) {
$variables['caption'] = $variables['elements']['field_oe_plain_text_long'];
}
if ($paragraph->hasField('field_oe_highlighted')) {
$variables['highlighted'] = (boolean) $paragraph->get('field_oe_highlighted')->value;
}
// Bail out if there is no media present.
if ($paragraph->get('field_oe_media')->isEmpty()) {
return;
}
$cacheability = CacheableMetadata::createFromRenderArray($variables);
/** @var \Drupal\media\Entity\Media $media */
$media = $paragraph->get('field_oe_media')->entity;