-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblellow.theme
1092 lines (966 loc) · 34.3 KB
/
blellow.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 in the oe_theme theme.
*/
declare(strict_types = 1);
use Drupal\Component\Utility\Html;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Render\Element;
use Drupal\Component\Utility\Random;
use Drupal\blellow\ValueObject\FileValueObject;
use Drupal\blellow\ValueObject\DateValueObject;
use Drupal\blellow\ValueObject\GalleryItemValueObject;
use Drupal\group\Entity\GroupContent;
/**
* Implements hook_preprocess_breadcrumb().
*/
function blellow_preprocess_breadcrumb(&$variables) {
$request = \Drupal::request();
$route_match = \Drupal::routeMatch();
$title = \Drupal::service('title_resolver')
->getTitle($request, $route_match->getRouteObject());
$variables['segments'] = array_map(function ($item) {
return [
'href' => $item['url'],
'label' => $item['text'],
];
}, $variables['breadcrumb']);
// Add the title to the segments only if it's not empty.
if (!empty($title)) {
$variables['segments'][] = [
'label' => $title,
];
}
}
/**
* Implements hook_preprocess_menu__main().
*/
function blellow_preprocess_menu__main(array &$variables): void {
// Massage data to be compliant with ECL navigation menu data structure.
$variables['links'] = array_map(function ($item) {
return [
'href' => $item['url'],
'label' => $item['title'],
'is_active' => $item['in_active_trail'],
];
}, $variables['items']);
foreach ($variables['items'] as $name => $link) {
$variables['links'][$name]['children_links'] = array_map(function ($item) {
return [
'links' => [[
'href' => $item['url'],
'label' => $item['title'],
'is_active' => $item['in_active_trail'],
],
],
];
}, $variables['items'][$name]['below']);
}
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*
* Adds a bare, markup-free template suggestion to all paragraph fields.
*/
function blellow_theme_suggestions_field_alter(array &$suggestions, array $variables): void {
// Do not output field labels and wrapping markup for paragraph fields.
if (isset($variables['element']['#entity_type']) && $variables['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');
}
// Prepare field suggestions related to the display mode.
$suggestions[] = 'field__' .
$variables['element']['#field_name'] . '__' .
$variables['element']['#view_mode'];
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function blellow_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 blellow_preprocess_paragraph__oe_links_block(array &$variables): void {
// Massage data to be compliant with ECL links block component data structure.
foreach (Element::children($variables['content']['field_oe_links']) as $index) {
$variables['links'][] = [
'label' => $variables['content']['field_oe_links'][$index]['#title'],
'url' => $variables['content']['field_oe_links'][$index]['#url'],
];
}
}
/**
* Implements hook_preprocess_paragraph__oe_accordion().
*/
function blellow_preprocess_paragraph__oe_accordion(array &$variables): void {
// Massage data to be compliant with ECL accordions 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 = $field_item->entity;
$variables['items'][] = [
'title' => $builder->viewField($paragraph->get('field_oe_text')),
'icon' => $paragraph->get('field_oe_icon')->value,
'body' => $builder->viewField($paragraph->get('field_oe_text_long')),
];
}
}
/**
* Implements hook_preprocess_menu_local_tasks().
*/
function blellow_preprocess_menu_local_tasks(array &$variables): void {
uasort ($variables['primary'], '_blellow_weight_comparator');
foreach ($variables['primary'] as $link) {
/** @var Drupal\Core\Url $url */
$url = $link['#link']['url'];
if ($url->access($variables['user'])) {
$variables['primary_links'][] = [
'href' => $url,
'label' => $link['#link']['title'],
'is_active' => $link['#active'],
];
}
}
uasort ($variables['secondary'], '_blellow_weight_comparator');
foreach ($variables['secondary'] as $link) {
/** @var Drupal\Core\Url $url */
$url = $link['#link']['url'];
if ($url->access($variables['user'])) {
$variables['secondary_links'][] = [
'href' => $url,
'label' => $link['#link']['title'],
'is_active' => $link['#active'],
];
}
}
}
/**
* Implements hook_preprocess_input__radio().
*/
function blellow_preprocess_input__radio(array &$variables): void {
_oe_theme_preprocess_input_label_wrapper($variables);
}
/**
* Implements hook_preprocess_input__checkbox().
*/
function blellow_preprocess_input__checkbox(array &$variables): void {
_oe_theme_preprocess_input_label_wrapper($variables);
}
/**
* 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['has_error'] = !empty($element['#errors']);
if (!in_array($element['#title_display'], ['none', 'invisible'])) {
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',
'checked' => 'checked',
'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_form_element().
*
* Disables displaying of the label for checkbox and radio elements, as the
* label is already rendered in the input template.
*/
function blellow_preprocess_form_element(array &$variables): void {
if (in_array($variables['element']['#type'], ['checkbox', 'radio'])) {
$variables['label_display'] = 'none';
}
}
/**
* Implements hook_preprocess_pager().
*
* @see template_preprocess_pager()
*
* Overwrites the default Drupal pager in order to adapt to the ECL style
* guide pager component requirements.
* Most of the bare-bones are copy-pasted from the Drupal implementation.
*/
function blellow_preprocess_pager(array &$variables): void {
$element = $variables['pager']['#element'];
$parameters = $variables['pager']['#parameters'];
$route_name = $variables['pager']['#route_name'];
$route_parameters = $variables['pager']['#route_parameters'] ?? [];
global $pager_page_array, $pager_total;
// Nothing to do if there is only one page.
if ($pager_total[$element] <= 1) {
return;
}
$quantity = 5;
$pager_middle = ceil($quantity / 2);
$pager_current = $pager_page_array[$element] + 1;
$pager_first = $pager_current - $pager_middle + 1;
$pager_last = $pager_current + $quantity - $pager_middle;
$pager_max = $pager_total[$element];
$i = $pager_first;
// Compared to the core implementation, we don't re-center but we cut out the
// extra elements from the start and end of the query.
if ($pager_last > $pager_max) {
$pager_last = $pager_max;
}
if ($i <= 0) {
$i = 1;
}
// Create the "first" and "previous" links if we are not on the first page.
if ($pager_page_array[$element] > 0) {
$variables['items']['first'] = [];
$options = [
'query' => pager_query_add_page($parameters, $element, 0),
];
$variables['items']['first']['href'] = \Drupal::url($route_name, $route_parameters, $options);
$variables['items']['first']['text'] = '1';
$variables['items']['first']['title'] = t('Go to page 1');
$variables['items']['previous'] = [];
$options = [
'query' => pager_query_add_page($parameters, $element, $pager_page_array[$element] - 1),
];
$variables['items']['previous']['href'] = \Drupal::url($route_name, $route_parameters, $options);
$variables['items']['previous']['text'] = t('� Previous');
$variables['items']['previous']['title'] = t('Go to previous page');
}
if ($i != $pager_max) {
// Generate the actual pager piece.
$variables['items']['pages'] = [];
for (; $i <= $pager_last && $i <= $pager_max; $i++) {
// Don't generate a link to the first page when the "first" link is
// already available.
if ($i == 1 && $pager_current > 1) {
continue;
}
// Don't generate a link to the last page when the "last" link is
// already available.
if ($i == $pager_max && ($pager_current < $pager_max)) {
continue;
}
$options = [
'query' => pager_query_add_page($parameters, $element, $i - 1),
];
$variables['items']['pages'][$i]['href'] = \Drupal::url($route_name, $route_parameters, $options);
if ($i == $pager_current) {
$variables['current'] = $i;
}
$variables['items']['pages'][$i]['text'] = (string) $i;
$variables['items']['pages'][$i]['title'] = t('Go to page @number', ['@number' => $i]);
}
}
// Create the "next" and "last" links if we are not on the last page.
if ($pager_page_array[$element] < ($pager_max - 1)) {
// Setting up variables for the next page link.
$variables['items']['next'] = [];
$options = [
'query' => pager_query_add_page($parameters, $element, $pager_page_array[$element] + 1),
];
$variables['items']['next']['href'] = \Drupal::url($route_name, $route_parameters, $options);
$variables['items']['next']['text'] = t('Next �');
$variables['items']['next']['title'] = t('Go to next page');
// Setting up the variables for the last page link.
$variables['items']['last'] = [];
$options = [
'query' => pager_query_add_page($parameters, $element, $pager_max - 1),
];
$variables['items']['last']['href'] = \Drupal::url($route_name, $route_parameters, $options);
$variables['items']['last']['text'] = (string) $pager_total[$element];
$variables['items']['last']['title'] = t('Go to page @number', ['@number' => $pager_total[$element]]);
}
// Set up additional variables.
$variables['max_page'] = $pager_total[$element];
}
/**
* Implements hook_preprocess_links().
*/
function blellow_preprocess_links__language_block(array &$variables): void {
$current_language = \Drupal::languageManager()
->getCurrentLanguage()
->getId();
$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'],
'lang' => $language_code,
'is_active' => $language_code === $current_language,
];
}
}
/**
* 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 blellow_preprocess_links__oe_multilingual_content_language_block(array &$variables): void {
$entity = \Drupal::service('oe_multilingual.helper')->getEntityFromCurrentRoute();
/** @var \Drupal\Core\Entity\EntityInterface $translation */
$translation = \Drupal::service('oe_multilingual.helper')->getCurrentLanguageEntityTranslation($entity);
$variables['current'] = $translation->language()->getName();
/** @var \Drupal\Core\Language\LanguageInterface[] $languages */
$languages = \Drupal::service('language_manager')->getNativeLanguages();
$current_language = \Drupal::languageManager()->getCurrentLanguage()->getId();
$variables['unavailable'] = $languages[$current_language]->getName();
// Normalize the links to an array of options suitable for the ECL
// "ecl-lang-select-pages" template.
$variables['options'] = [];
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['options'][] = [
'href' => $href,
'hreflang' => $language_code,
'label' => $link['link']['#title'],
'lang' => $language_code,
];
}
}
/**
* Implements hook_preprocess_HOOK().
*/
function blellow_preprocess_pattern_dialog(array &$variables): void {
// Make sure it has default values.
$variables += [
'dialog_id' => Html::getUniqueId('ecl-dialog'),
'title' => [],
'description' => [],
];
// Format the "title" variable.
$variables['title'] = [
'id' => Html::getUniqueId('ecl-dialog-title'),
'value' => $variables['title'],
];
// Format the "description" variable.
$variables['description'] = [
'id' => Html::getUniqueId('ecl-dialog-description'),
'value' => $variables['description'],
];
// Format the "hidden" variable.
$variables['hidden'] = $variables['hidden'] ? 'true' : 'false';
}
/**
* Implements hook_preprocess_HOOK().
*/
function blellow_preprocess_pattern_gallery__preview(array &$variables): void {
foreach ($variables['items'] as &$item) {
$item = GalleryItemValueObject::fromArray($item);
}
}
/**
* Preprocess the $variables before sent them to the page header.
*
* @param array $variables
* The params.
*/
function blellow_preprocess_pattern_page_header(array &$variables): void {
$variables['#attached']['library'][] = 'blellow/group_header';
$title = $variables['title'];
$title = _blellow_title_reduce($title);
$variables['title'] = $title;
}
/**
* TODO: prefix the block eg fut_group_page_header.
*/
function blellow_preprocess_page_header_block(array &$variables): void {
$variables['#attached']['library'][] = 'blellow/group_header';
$title = $variables['title'];
$title = _blellow_title_reduce($title);
$variables['title'] = $title;
$variables['breadcrumb'] = array_map(function ($item) {
return [
'href' => $item->getUrl(),
'label' => $item->getText(),
];
}, $variables['breadcrumb']);
// Null Safe Array.
$variables['group_operations'] = $variables['group_operations'] ? $variables['group_operations'] : [];
array_walk($variables['group_operations'], function (&$item, $key) {
$item['key'] = $key;
});
$variables['operation_links'] = array_map(function ($item) {
return [
'href' => $item['url'],
'label' => $item['title'],
'weight' => $item['weight'],
'key' => $item['key'],
];
}, $variables['group_operations']);
$variables['operation_links'] = array_values($variables['operation_links']);
/* Uncomment to simulate cases with one action only.
$variables['operation_links'] = array_slice($variables['operation_links'],0,1);*/
}
/**
* Implements hook_preprocess_HOOK().
*/
function blellow_preprocess_pattern_accordion(array &$variables): void {
// If no identifier is provided generate a random, unique one.
if (empty($variables['identifier'])) {
$random = new Random();
$variables['identifier'] = $random->string(8, TRUE);
}
// Format items in order to respect ECL expectations.
foreach (array_values($variables['items']) as $key => $item) {
$variables['items'][$key] = [
'id' => $variables['identifier'] . '-' . $key,
'heading' => [
'title' => $item['title'],
'icon' => $item['icon'],
'level' => $key,
],
'body' => $item['body'],
'isExpanded' => $key === 0,
];
}
}
/**
* Implements hook_preprocess_HOOK().
*/
function blellow_preprocess_pattern_dropdown(array &$variables): void {
$defaults = [
'wrapper_id' => 'ecl-button-dropdown',
'expandable_id' => 'expandable-ecl-button-dropdown',
'button_id' => 'button-ecl-button-dropdown',
];
foreach ($defaults as $key => $id) {
if (!empty($variables[$key])) {
continue;
}
$variables[$key] = Html::getUniqueId($id);
}
// @todo: Remove this duplicated code and reuse the link_block from below.
foreach ($variables['links'] as $key => $link) {
$variables['links'][$key]['href'] = $link['url'];
}
}
/**
* Implements hook_preprocess_HOOK().
*/
function blellow_preprocess_pattern_link_block(array &$variables): void {
// Format links in order to respect ECL expectations.
foreach ($variables['links'] as $key => $link) {
$variables['links'][$key]['href'] = $link['url'];
}
}
/**
* Implements hook_preprocess_HOOK().
*/
function _oe_theme_preprocess_pattern_social_media_links(array &$variables): void {
// Format social media links in order to respect ECL expectations.
foreach ($variables['links'] as $key => $link) {
$variables['links'][$key]['variant'] = $link['service'];
$variables['links'][$key]['link'] = [
'label' => $link['label'],
'href' => $link['url'],
];
}
}
/**
* Implements hook_preprocess_HOOK().
*/
function blellow_preprocess_pattern_social_media_links_vertical(array &$variables): void {
_oe_theme_preprocess_pattern_social_media_links($variables);
}
/**
* Implements hook_preprocess_HOOK().
*/
function blellow_preprocess_pattern_social_media_links_horizontal(array &$variables): void {
_oe_theme_preprocess_pattern_social_media_links($variables);
}
/**
* Implements hook_preprocess_html() for html.html.twig.
*
* Add css class for splash page.
*/
function blellow_preprocess_html(array &$variables): void {
if ('language_selection_page' === \Drupal::request()->attributes->get('_route')) {
$variables['attributes']['class'][] = 'page-splash';
}
}
/**
* Implements hook_preprocess_paragraph() for paragraph--oe-list-item.html.twig.
*/
function blellow_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';
$variables['url'] = $paragraph->get('field_oe_link')->first()->getUrl();
$cacheability = CacheableMetadata::createFromRenderArray($variables);
// Extract the image if present.
if (!$paragraph->get('field_oe_image')->isEmpty()) {
/** @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' => file_url_transform_relative(file_create_url($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 blellow_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;
// 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();
$variables['button_url'] = $link_item->getUrl();
$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 blellow_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;
}
// Add default fallback title for inpage naviagtion.
$variables['title'] = t('Page contents');
if (!$paragraph->get('field_oe_title')->isEmpty()) {
$variables['title'] = $paragraph->get('field_oe_title')->value;
}
// Create a shortcut to the render array for the field.
$field_render = &$variables['content']['field_oe_paragraphs'];
$links = [];
foreach ($paragraph->get('field_oe_paragraphs')->referencedEntities() as $delta => $sub_paragraph) {
/** @var \Drupal\paragraphs\Entity\Paragraph $sub_paragraph */
if (!$sub_paragraph->hasField('field_oe_title') || $sub_paragraph->get('field_oe_title')->isEmpty()) {
continue;
}
$unique_id = Html::getUniqueId('ecl-inpage-' . $sub_paragraph->id());
// Wrap the paragraph in a div with a specific id set as anchor.
$field_render[$delta]['#theme_wrappers']['container'] = [
'#attributes' => ['id' => $unique_id],
];
// Get sub-paragraph translation.
$sub_paragraph = \Drupal::service('entity.repository')
->getTranslationFromContext($sub_paragraph, $paragraph->language()->getId());
// Add a link pointing to the paragraph.
$links[] = [
'href' => '#' . $unique_id,
'label' => $sub_paragraph->get('field_oe_title')->first()->value,
];
}
$variables['links'] = $links;
}
/**
* Implements hook_preprocess_pattern().
*/
function blellow_preprocess_pattern_date_block(array &$variables): void {
// This make sure that the variant value will always fallback to
// "default" if an empty or not valid value is passed.
$variants = [
'default',
'ongoing',
'cancelled',
'past',
];
$variables['variant'] = in_array($variables['variant'], $variants, TRUE) ?
$variables['variant'] :
'default';
}
/**
* Implements hook_preprocess_pattern_file__preview().
*/
function blellow_preprocess_pattern_file__preview(array &$variables): void {
$variables['file'] = FileValueObject::fromArray($variables['file']);
}
/**
* Implements hook_preprocess_pattern_file_link__preview().
*/
function blellow_preprocess_pattern_file_link__preview(array &$variables): void {
$variables['files'] = array_map(
function ($file) {
return FileValueObject::fromArray($file);
},
$variables['files']
);
}
/**
* Implements hook_preprocess_pattern_file_translation().
*/
function blellow_preprocess_pattern_file_translation(array &$variables): void {
// This is defined in ui_patterns contrib module in ui_patterns_theme().
if ($variables['translations'] !== NULL) {
return;
}
// Add css ids for expandable javascript behaviour.
$variables['translations_id'] = Html::getUniqueId('translations-expand');
$variables['translations_button_id'] = Html::getUniqueId('translations-expand-button');
}
/**
* Implements hook_preprocess_HOOK().
*/
function blellow_preprocess_pattern_file_translation__preview(array &$variables): void {
$variables['file'] = FileValueObject::fromArray($variables['file']);
foreach ((array) $variables['translations'] as $index => $translation) {
$variables['translations'][$index] = FileValueObject::fromArray($translation);
}
}
/**
* Implements hook_preprocess_HOOK().
*/
function blellow_preprocess_pattern_list_item__variant_date__preview(array &$variables): void {
$variables['date'] = DateValueObject::fromArray($variables['date']);
}
/**
* Implements hook_preprocess_HOOK().
*/
function blellow_preprocess_pattern_date_block__preview(array &$variables): void {
$variables['date'] = DateValueObject::fromArray($variables['date']);
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function blellow_theme_suggestions_form_alter(array &$suggestions, array $variables): void {
$suggestions[] = $variables['theme_hook_original'] . '__' . $variables['element']['#form_id'];
}
/**
* Implements hook_preprocess_input().
*/
function blellow_preprocess_input__search(array &$variables): void {
_oe_theme_preprocess_input_text($variables['element']);
}
/**
* Implements hook_preprocess_form().
*/
function blellow_preprocess_form__oe_search_search_form(array &$variables): void {
_oe_theme_preprocess_input_text($variables['element']['keys']);
$variables['input'] = $variables['element']['keys']['ecl_array'];
$variables['label'] = t('Search');
}
/**
* Helper function to additionally preprocess textfield search elements.
*
* Moves the label element to the input template and creates some helper
* variables to be used with the ECL templates.
*
* @param array $element
* Set of element data.
*/
function _oe_theme_preprocess_input_text(array &$element): void {
// Generate the extra attributes array from the general attributes.
$extra_attributes = !empty($element['#attributes']) ? $element['#attributes'] : [];
$element['ecl_array'] = [
'id' => $element['#attributes']['id'],
'name' => $element['#attributes']['name'],
'disabled' => isset($element['#attributes']['disabled']) ? $element['#attributes']['disabled'] : FALSE,
'type' => $element['#attributes']['type'],
'class' => $element['#attributes']['class'],
];
$extra_attributes = array_diff_key($extra_attributes, $element['ecl_array']);
$element['ecl_array']['extra_attributes'] = [];
foreach ($extra_attributes as $key => $value) {
if (is_string($value)) {
$element['ecl_array']['extra_attributes'][] = [
'name' => $key,
'value' => $value,
];
}
}
// Map rest array elements.
$element['ecl_array']['has_error'] = !empty($element['#errors']);
}
/**
* Provide hook theme suggestions for patterns.
*
* @see ui_patterns_theme_suggestions_alter()
*
* https://ui-patterns.readthedocs.io/en/8.x-1.x/content/developer-documentation.html
*/
function blellow_ui_patterns_suggestions_alter(array &$suggestions, array $variables, $context) {
// kint($variables);
$hook = $variables['theme_hook_original'];
$suggestions[] = $hook . '__futurium_header';
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function blellow_theme_suggestions_oe_corporate_blocks_footer_alter(array &$suggestions, array $variables): void {
$hook = $variables['theme_hook_original'];
$suggestions[] = $hook . '__futurium_footer';
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function blellow_theme_suggestions_cnect_corporate_blocks_footer_alter(array &$suggestions, array $variables): void {
$hook = $variables['theme_hook_original'];
$suggestions[] = $hook . '__futurium_footer';
}
/**
* Implements hook_theme_preprocess_HOOK().
*/
function blellow_preprocess_field(&$variables, $hook) {
$element = $variables['element'];
// Resolve and call the local hook for the particular field_name,
// if available.
if (isset($element['#field_name'])) {
$basename = '_blellow_preprocess_field__';
$postfix = $element['#field_name'];
$fun_name = $basename . $postfix;
// Do not proceed if local hook is not available.
if (!function_exists($fun_name)) {
return;
}
// call_user_func passes parameters by value, we need $variables by ref.
// For now we use the variable for the call.
// - https://stackoverflow.com/questions/3637164/why-does-the-error-expected-to-be-a-reference-value-given-appear
// - https://www.php.net/call_user_func
// call_user_func($fun_name, array(&$variables), $hook);.
$fun_name($variables, $hook);
}
}
/**
* Prepare the varialbles the ecl-style-component needs.
*
* @param array $variables
* Twig variables.
* @param string $hook
* Hook name.
*/
function blellow_preprocess_image(array &$variables, $hook): void {
$capture = ['src', 'alt', 'typeof'];
$attributes = $variables['attributes'];
foreach ($capture as &$key) {
$variables[$key] = _blellow_capture_and_unset($attributes, $key);
}
$namevalue = array_map('_blellow_namevalue_mapper', $attributes, array_keys($attributes));
$variables['extra_attributes'] = $namevalue;
}
/**
* Implements hook_preprocess_menu__main().
*/
function blellow_preprocess_group_navigation(array &$variables): void {
$variables['links'] = array_map(function ($item) {
return [
'href' => $item->url,
'label' => $item->title,
];
}, $variables['links']);
}
/**
* Implements hook_form_comment_form_alter().
*/
function blellow_form_comment_form_alter(&$form, &$form_state) {
// We could also hide($form['subject']);.
$form['subject']['#access'] = FALSE;
}
/**
* Implements hook_preprocess_node().
*/
function blellow_preprocess_node(&$variables) {
/* todo: can be removed as not used now */
$node = $variables['node'];
$variables['avatar'] = user_view($node->getOwner(), 'fut_default_thumbnail');
/* Bring group info to the nodes */
$group_content_entity = GroupContent::loadByEntity($variables['node']);
$node_type = $variables['node']->getType();
$view_mode = $variables['view_mode'];
if (($node_type == 'fut_post' || $node_type == 'fut_event') && (($view_mode == 'fut_teaser') || ($view_mode == 'fut_teaser_lite'))) {
foreach ($group_content_entity as $group_content) {
$variables['group_id'] = $group_content->getGroup()->id();
$variables['group_label'] = $group_content->getGroup()->label();
$variables['group_url'] = $group_content->getGroup()->url();
$group_short_name = $group_content->getGroup()->get('fut_short_name')->getValue();
$variables['group_short_name'] = reset($group_short_name);
$logo = $group_content->getGroup()->get('fut_logo')->getValue();
$variables['group_logo'] = reset($logo);
}
}
}
/**
* Implements hook_preprocess_menu_local_action().
*/
function blellow_preprocess_menu_local_action(&$variables) {
$variables['link']['#options']['attributes']['class'][] = 'ecl-button';
$variables['link']['#options']['attributes']['class'][] = 'ecl-button--default';
}
/**
* Maps an array element into a name/value pair.
*
* @param string $value
* The value of the element.
* @param string $key
* The key of the element (in the array)
*
* @return array
* A name/value pair array
*/
function _blellow_namevalue_mapper($value, $key) {
return ['name' => $key, 'value' => $value];
}
/**
* Captures a value from an array and unsets it.
*
* @param array $array
* The array to capture values from.
* @param string $key
* The key of the element to capture.
*
* @return mixed
* The captured value.
*/