forked from openeuropa/oe_theme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oe_theme.theme
161 lines (146 loc) · 4.75 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
<?php
/**
* @file
* Functions to support theming.
*/
use Drupal\Core\Render\Element;
/**
* Implements hook_preprocess_breadcrumb().
*/
function oe_theme_preprocess_breadcrumb(&$variables) {
$variables['segments'] = array_map(function ($item) {
return [
'target' => $item['url'],
'title' => $item['text'],
];
}, $variables['breadcrumb']);
}
/**
* 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 [
'href' => $item['url'],
'label' => $item['title'],
'is_active' => $item['in_active_trail'],
];
}, $variables['items'][$name]['below']);
}
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function oe_theme_theme_suggestions_field_alter(array &$suggestions, array $variables) {
// Do not output field labels and wrapping markup for paragraph fields.
if (isset($variables['element']['#entity_type']) && $variables['element']['#entity_type'] === 'paragraph') {
$suggestions[] = 'field__bare';
}
}
/**
* 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'],
'href' => $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['panels'] = [];
/** @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['panels'][] = [
'id' => $paragraph->id(),
'heading' => [
'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) {
$element = &$variables['element'];
$variables['has_error'] = !empty($element['#errors']);
if (isset($element['#title']) && $element['#title'] !== '') {
$variables['title'] = ['#markup' => $element['#title']];
}
}
/**
* 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';
}
}