forked from City-of-Helsinki/drupal-module-helfi-tpr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelfi_tpr.module
270 lines (237 loc) · 8.46 KB
/
helfi_tpr.module
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
<?php
/**
* @file
* Contains helfi_tpr.
*/
declare(strict_types = 1);
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Session\AccountInterface;
/**
* Implements hook_theme().
*/
function helfi_tpr_theme() {
return [
'tpr_accessibility_sentences' => [
'variables' => ['name' => NULL, 'items' => []],
],
'tpr_school_details' => [
'variables' => ['clarification' => NULL, 'schoolyear' => NULL],
'template' => 'tpr-school-details',
],
'tpr_unit' => [
'render element' => 'elements',
'template' => 'tpr-unit',
],
'tpr_service' => [
'render element' => 'elements',
'template' => 'tpr-service',
],
'tpr_unit_form' => [
'render element' => 'form',
],
'tpr_service_form' => [
'render element' => 'form',
],
'tpr_service_channel' => [
'render element' => 'elements',
'template' => 'tpr-service-channel',
],
'tpr_errand_service' => [
'render element' => 'elements',
'template' => 'tpr-errand-service',
],
'tpr_ontology_word_details' => [
'render element' => 'elements',
'template' => 'tpr-ontology-word-details',
],
'tpr_ontology_word_details_form' => [
'render element' => 'form',
],
];
}
/**
* Implements hook_theme_suggestions_HOOK().
*/
function helfi_tpr_theme_suggestions_tpr_unit(array $variables) {
$suggestions = [];
$sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
$suggestions[] = 'tpr_unit__' . $sanitized_view_mode;
return $suggestions;
}
/**
* Implements hook_theme_suggestions_HOOK().
*/
function helfi_tpr_theme_suggestions_tpr_service(array $variables) {
$suggestions = [];
$sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
$suggestions[] = 'tpr_service__' . $sanitized_view_mode;
return $suggestions;
}
/**
* Implements hook_theme_suggestions_HOOK().
*/
function helfi_tpr_theme_suggestions_tpr_service_channel(array $variables) {
$suggestions = [];
$sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
$suggestions[] = 'tpr_service_channel__' . $sanitized_view_mode;
return $suggestions;
}
/**
* Implements hook_theme_suggestions_HOOK().
*/
function helfi_tpr_theme_suggestions_tpr_errand_service(array $variables) {
$suggestions = [];
$sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
$suggestions[] = 'tpr_errand_service__' . $sanitized_view_mode;
return $suggestions;
}
/**
* Prepares variables for tpr_unit templates.
*
* Default template: tpr-unit.html.twig.
*
* @param array $variables
* An associative array containing:
* - elements: An array of elements to display in view mode.
*/
function template_preprocess_tpr_unit(array &$variables) {
$variables['view_mode'] = $variables['elements']['#view_mode'];
// Helpful $content variable for templates.
foreach (Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
if (isset($variables['elements']['#tpr_unit'])) {
$variables['entity'] = $variables['elements']['#tpr_unit'];
$variables['content']['description_summary'] = $variables['elements']['#tpr_unit']->get('description')->summary;
// Get provided languages for the template.
$provided_languages = $variables['entity']->get('provided_languages')->getValue();
foreach ($provided_languages as $provided_language) {
$variables['provided_languages'][] = $provided_language['value'];
}
// Get 'hide_description' field value and pass it to the template.
$hide_description = $variables['entity']->get('hide_description')->value;
$variables['hide_description'] = boolval($hide_description);
// Get 'show_www' field value and pass it to the template.
$show_www = $variables['entity']->get('show_www')->value;
$variables['show_www'] = boolval($show_www);
}
}
/**
* Prepares variables for tpr_service templates.
*
* Default template: tpr-service.html.twig.
*
* @param array $variables
* An associative array containing:
* - elements: An array of elements to display in view mode.
*/
function template_preprocess_tpr_service(array &$variables) {
$variables['view_mode'] = $variables['elements']['#view_mode'];
// Helpful $content variable for templates.
foreach (Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
if (isset($variables['elements']['#tpr_service'])) {
$variables['entity'] = $variables['elements']['#tpr_service'];
$variables['content']['description_summary'] = $variables['elements']['#tpr_service']->get('description')->summary;
}
}
/**
* Prepares variables for tpr_service_channel templates.
*
* Default template: tpr-service-channel.html.twig.
*
* @param array $variables
* An associative array containing:
* - elements: An array of elements to display in view mode.
*/
function template_preprocess_tpr_service_channel(array &$variables) {
$variables['view_mode'] = $variables['elements']['#view_mode'];
// Helpful $content variable for templates.
foreach (Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
if (isset($variables['elements']['#tpr_service_channel'])) {
$variables['entity'] = $variables['elements']['#tpr_service_channel'];
}
}
/**
* Prepares variables for tpr_errand_service templates.
*
* Default template: tpr-errand-service.html.twig.
*
* @param array $variables
* An associative array containing:
* - elements: An array of elements to display in view mode.
*/
function template_preprocess_tpr_errand_service(array &$variables) {
$variables['view_mode'] = $variables['elements']['#view_mode'];
// Helpful $content variable for templates.
foreach (Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
if (isset($variables['elements']['#tpr_errand_service'])) {
$variables['entity'] = $variables['elements']['#tpr_errand_service'];
}
}
/**
* Prepares variables for tpr_ontology_word_details templates.
*
* Default template: tpr-ontology-word-details.html.twig.
*
* @param array $variables
* An associative array containing:
* - elements: An array of elements to display in view mode.
*/
function template_preprocess_tpr_ontology_word_details(array &$variables) {
$variables['view_mode'] = $variables['elements']['#view_mode'];
// Helpful $content variable for templates.
foreach (Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
if (isset($variables['elements']['#tpr_ontology_word_details'])) {
$variables['entity'] = $variables['elements']['#tpr_ontology_word_details'];
}
}
/**
* Implements hook_ENTITY_TYPE_access().
*/
function helfi_tpr_tpr_unit_access(EntityInterface $entity, $operation, AccountInterface $account) {
// Allow user to view unpublished Units based on permission.
if ($operation === 'view' && !$entity->isPublished()) {
return AccessResult::allowedIfHasPermission($account, 'view unpublished tpr_unit');
}
return AccessResult::neutral();
}
/**
* Implements hook_ENTITY_TYPE_access().
*/
function helfi_tpr_tpr_service_access(EntityInterface $entity, $operation, AccountInterface $account) {
// Allow user to view unpublished Services based on permission.
if ($operation === 'view' && !$entity->isPublished()) {
return AccessResult::allowedIfHasPermission($account, 'view unpublished tpr_service');
}
return AccessResult::neutral();
}
/**
* Implements hook_ENTITY_TYPE_access().
*/
function helfi_tpr_tpr_service_channel_access(EntityInterface $entity, $operation, AccountInterface $account) {
// Allow user to view unpublished Service channels based on permission.
if ($operation === 'view' && !$entity->isPublished()) {
return AccessResult::allowedIfHasPermission($account, 'view unpublished tpr_service_channel');
}
return AccessResult::neutral();
}
/**
* Implements hook_ENTITY_TYPE_access().
*/
function helfi_tpr_tpr_errand_service_access(EntityInterface $entity, $operation, AccountInterface $account) {
// Allow user to view unpublished Errand services based on permission.
if ($operation === 'view' && !$entity->isPublished()) {
return AccessResult::allowedIfHasPermission($account, 'view unpublished tpr_errand_service');
}
return AccessResult::neutral();
}