forked from openeuropa/oe_theme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoe_theme.theme
580 lines (514 loc) · 19.4 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
<?php
/**
* @file
* Functions to support theming.
*/
declare(strict_types = 1);
use Drupal\Component\Utility\Html;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Render\Element;
use Drupal\Component\Utility\Random;
/**
* Implements hook_preprocess_breadcrumb().
*/
function oe_theme_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 oe_theme_preprocess_menu__main(&$variables) {
// 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 oe_theme_theme_suggestions_field_alter(&$suggestions, $variables) {
// 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');
}
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function oe_theme_theme_suggestions_paragraph_alter(&$suggestions, $variables) {
/** @var \Drupal\paragraphs\ParagraphInterface $paragraph */
$paragraph = $variables['elements']['#paragraph'];
if ($paragraph->bundle() === 'oe_content_row') {
$variant = $paragraph->get('field_oe_content_row_variant')->first()->value;
$suggestions[] = 'paragraph__' . $paragraph->bundle() . '__variant_' . $variant;
}
}
/**
* Implements hook_preprocess_paragraph().
*/
function oe_theme_preprocess_paragraph__oe_links_block(&$variables) {
// 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 oe_theme_preprocess_paragraph__oe_accordion(&$variables) {
// 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 oe_theme_preprocess_menu_local_tasks(&$variables) {
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'],
];
}
}
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 oe_theme_preprocess_input__radio(&$variables) {
_oe_theme_preprocess_input_label_wrapper($variables);
}
/**
* Implements hook_preprocess_input__checkbox().
*/
function oe_theme_preprocess_input__checkbox(&$variables) {
_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 (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 oe_theme_preprocess_form_element(&$variables) {
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 oe_theme_preprocess_pager(&$variables) {
$element = $variables['pager']['#element'];
$parameters = $variables['pager']['#parameters'];
$route_name = $variables['pager']['#route_name'];
$route_parameters = isset($variables['pager']['#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 oe_theme_preprocess_links__language_block(&$variables) {
$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 oe_theme_preprocess_links__oe_multilingual_content_language_block(&$variables) {
$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();
$language_names = \Drupal::service('oe_multilingual.helper')->getLanguageNameList();
$current_language = \Drupal::languageManager()->getCurrentLanguage()->getId();
$variables['unavailable'] = $language_names[$current_language];
// 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 oe_theme_preprocess_pattern_accordion(&$variables) {
// 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 oe_theme_preprocess_pattern_link_block(&$variables) {
// 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(&$variables) {
// 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 oe_theme_preprocess_pattern_social_media_links_vertical(&$variables) {
_oe_theme_preprocess_pattern_social_media_links($variables);
}
/**
* Implements hook_preprocess_HOOK().
*/
function oe_theme_preprocess_pattern_social_media_links_horizontal(&$variables) {
_oe_theme_preprocess_pattern_social_media_links($variables);
}
/**
* Implements hook_preprocess_html() for html.html.twig.
*
* Add css class for splash page.
*/
function oe_theme_preprocess_html(&$variables) {
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 oe_theme_preprocess_paragraph__oe_list_item(&$variables) {
/** @var \Drupal\paragraphs\Entity\Paragraph $paragraph */
$paragraph = $variables['paragraph'];
$variables['variant'] = $paragraph->get('field_oe_list_item_variant')->first()->value;
$variables['url'] = $paragraph->get('field_oe_link')->first()->getUrl()->toString();
$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()) {
/** @var \Drupal\Core\Datetime\DrupalDateTime $date */
$date = $paragraph->get('field_oe_date')->first()->get('date')->getValue();
$timestamp = $date->getTimestamp();
$date_formatter = \Drupal::service('date.formatter');
$variables['week_day'] = $date_formatter->format($timestamp, 'custom', 'D');
$variables['day'] = $date_formatter->format($timestamp, 'custom', 'd');
$variables['month'] = $date_formatter->format($timestamp, 'custom', 'M');
// 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(&$variables) {
/** @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_variant')->first()->value;
// Prepare the button variables if a link has been specified.
if (!$paragraph->get('field_oe_link')->isEmpty()) {
/** @var \Drupal\link\Plugin\Field\FieldType\LinkItem $link_item */
$link_item = $paragraph->get('field_oe_link')->first();
$variables['button_url'] = $link_item->getUrl()->toString();
$variables['button_label'] = $link_item->get('title')->getValue();
}
}
/**
* Implements hook_preprocess_paragraph() for paragraph--oe-content-row--variant-inpage.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(&$variables) {
/** @var \Drupal\paragraphs\Entity\Paragraph $paragraph */
$paragraph = $variables['paragraph'];
if ($paragraph->get('field_oe_paragraphs')->isEmpty()) {
return;
}
// 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;
}