-
Notifications
You must be signed in to change notification settings - Fork 0
/
os2web_pagebuilder.module
334 lines (286 loc) · 10.6 KB
/
os2web_pagebuilder.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
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
<?php
/**
* @file
* Page builder module.
*/
use Drupal\block\Entity\Block;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\node\NodeInterface;
use Drupal\os2web_pagebuilder\Form\SettingsForm;
use Symfony\Component\Yaml\Yaml;
/**
* Update/creates entity from yml.
*
* Some config like form display needs to be updated via config manager.
*
* @param string $ymlFileName
* Yml file name.
* @param string $entityType
* The entity type for this storage.
* @param mixed $id
* The ID of the entity to load.
* @param array $setComponents
* Array of components you want to add.
* - The key will be what we are setting.
* - The value is the key that will be used from the new config file
* (Can have in string for array).
*/
function os2web_pagebuilder_update_entity_from_yml($module_name, $ymlFileName, $entityType, $id, array $setComponents) {
$yml = Yaml::parse(file_get_contents(drupal_get_path('module', $module_name) . '/config/optional/' . $ymlFileName . '.yml'));
$entity = \Drupal::entityTypeManager()
->getStorage($entityType)
->load($id);
if (empty($entity)) {
os2web_pagebuilder_read_in_new_config($ymlFileName, $module_name);
return;
}
foreach ($setComponents as $key => $value) {
$parts = explode('.', $value);
if (count($parts) == 1) {
$entity->setComponent($key, $yml[$value]);
}
else {
$value = NestedArray::getValue($yml, $parts);
if (empty($value)) {
\Drupal::service('messenger')->addWarning('Component ' . $key . ' has empty configuration');
continue;
}
$entity->setComponent($key, $value);
}
}
$entity->save();
}
/**
* Implements hook_form_alter().
*/
function os2web_pagebuilder_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Hiding os2web_contact related fields, if setting tells us to.
if ($form_id == 'node_os2web_page_form' || $form_id == 'node_os2web_page_edit_form') {
// Hiding tab - Sidebygger.
if (!\Drupal::currentUser()->hasPermission('os2web_page access sidebygger tab')) {
$form['field_os2web_page_paragraph_wide']['#access'] = FALSE;
}
// Hiding tab - Media.
if (!\Drupal::currentUser()->hasPermission('os2web_page access media tab')) {
$form['field_os2web_page_paragraph_bann']['#access'] = FALSE;
$form['field_os2web_page_primaryimage']['#access'] = FALSE;
}
// Hiding field - Primary image.
if (!\Drupal::currentUser()->hasPermission('os2web_page access primaryimage field')) {
$form['field_os2web_page_primaryimage']['#access'] = FALSE;
}
if (!empty($form['field_os2web_page_primaryicon'])) {
if (!\Drupal::currentUser()->hasPermission('os2web_page access primaryicon field')) {
$form['field_os2web_page_primaryicon']['#access'] = FALSE;
}
}
if (!empty($form['field_os2web_icon'])) {
if (!\Drupal::currentUser()->hasPermission('os2web_page access icon field')) {
$form['field_os2web_icon']['#access'] = FALSE;
}
}
// Hiding tab - Sideafsnit.
if (!\Drupal::currentUser()->hasPermission('os2web_page access sideafsnit tab')) {
$form['field_os2web_page_paragraph_righ']['#access'] = FALSE;
$form['field_os2web_page_paragraph_left']['#access'] = FALSE;
}
// Hiding tab - Instillinger.
if (!\Drupal::currentUser()->hasPermission('os2web_page access instillinger tab')) {
$form['field_os2web_page_contact']['#access'] = FALSE;
$form['field_os2web_page_contact_hide']['#access'] = FALSE;
$form['field_os2web_page_related_hide']['#access'] = FALSE;
}
if (\Drupal::config(SettingsForm::$configName)->get('hide_os2web_contact_fields')) {
$form['field_os2web_page_contact']['#access'] = FALSE;
$form['field_os2web_page_contact_hide']['#access'] = FALSE;
}
if (\Drupal::config(SettingsForm::$configName)->get('hide_os2web_paragraph_banner_field')) {
$form['field_os2web_page_paragraph_bann']['#access'] = FALSE;
}
if (\Drupal::config(SettingsForm::$configName)->get('hide_os2web_content_page_section_tab')) {
$form['field_os2web_page_paragraph_righ']['#access'] = FALSE;
$form['field_os2web_page_paragraph_left']['#access'] = FALSE;
}
if (
!\Drupal::currentUser()->hasPermission('create os2web_borgerdk content') &&
!\Drupal::currentUser()->hasPermission('edit own os2web_borgerdk content')
) {
if (!empty($form['field_selfbetjening_ref'])) {
$form['field_selfbetjening_ref']['#access'] = FALSE;
}
}
}
// Making field optional only for Section pages.
if ($form_id == 'node_os2web_page_edit_form') {
/** @var NodeInterface $node */
$node = $form_state->getFormObject()->getEntity();
$config = \Drupal::config(SettingsForm::$configName);
if ($config->get('os2web_section_pages')) {
$sectionPageIds = array_column($config->get('os2web_section_pages'), 'target_id');
if (in_array($node->id(), $sectionPageIds)) {
$form['field_os2web_page_intro']['widget'][0]['value']['#required'] = FALSE;
$form['field_os2web_page_description']['widget'][0]['#required'] = FALSE;
$form['#validate'][] = 'os2web_pagebuilder_section_page_node_form_validate';
}
}
}
// Changing media file field name.
if ($form_id == 'media_library_add_form_upload') {
$form['media'][0]['fields']['name']['widget'][0]['value']['#title'] = 'Linktekst';
}
if (preg_match('/media_.*_(add|edit)_form/', $form_id)) {
$form['name']['widget'][0]['value']['#title'] = 'Linktekst';
}
}
/**
* Custom validate function for section pages.
*
* Removes the error related with fields that are optional for section page.
*
* @param $form
* @param FormStateInterface $form_state
*/
function os2web_pagebuilder_section_page_node_form_validate($form, FormStateInterface $form_state) {
// Clearing the errors and saving the for future.
$errors = $form_state->getErrors();
$form_state->clearErrors();
// Adding errors back, skipping those related with fields we made non-mandatory.
foreach ($errors as $element => $error) {
if ($element != 'field_os2web_page_description' && $element != 'field_os2web_page_intro') {
$form_state->setErrorByName($element, $error);
}
}
}
/**
* Implements hook_entity_presave().
*
* On a new node save copies header from title if header value is empty.
*/
function os2web_pagebuilder_node_presave(NodeInterface $node) {
if ($node->isNew() && $node->getType() == 'os2web_page') {
// If header is empty, use the title.
if (empty($node->field_os2web_page_heading->value)) {
$node->field_os2web_page_heading->value = $node->label();
}
}
}
/**
* Implements hook_block_access().
*/
function os2web_pagebuilder_block_access(Block $block, $operation, AccountInterface $account) {
// Hiding os2web_contact, if setting tells us to.
if ($operation == 'view') {
if ($block->getPluginId() == 'os2web_contact') {
if (\Drupal::config(SettingsForm::$configName)->get('hide_os2web_contact_fields')) {
return AccessResult::forbidden();
}
}
}
return AccessResult::neutral();
}
/**
* Implements hook_field_widget_WIDGET_TYPE_form_alter().
*/
function os2web_pagebuilder_field_widget_paragraphs_form_alter(&$element, &$form_state, $context) {
if (\Drupal::config(SettingsForm::$configName)
->get('hide_os2web_paragraph_title_field')) {
switch ($element['#paragraph_type']) {
case 'os2web_spotbox_reference':
$element['subform']['field_os2web_spotbox_reference_h']['#access'] = FALSE;
break;
case 'os2web_accordion':
$element['subform']['field_os2web_accordion_heading']['#access'] = FALSE;
break;
case 'os2web_banner_paragraph':
$element['subform']['field_os2web_banner_heading']['#access'] = FALSE;
break;
case 'os2web_box_paragraph':
$element['subform']['field_os2web_box_heading']['#access'] = FALSE;
break;
case 'os2web_files_paragraph':
$element['subform']['field_os2web_files_heading']['#access'] = FALSE;
break;
case 'os2web_content_reference':
$element['subform']['field_os2web_content_reference_h']['#access'] = FALSE;
break;
case 'os2web_simple_text_paragraph':
$element['subform']['field_os2web_simple_text_heading']['#access'] = FALSE;
break;
case 'os2web_cnt_paragraph':
$element['subform']['field_os2web_cnt_heading']['#access'] = FALSE;
break;
}
}
if (\Drupal::config(SettingsForm::$configName)
->get('hide_os2web_paragraph_subtitle_field')) {
switch ($element['#paragraph_type']) {
case 'os2web_spotbox_reference':
$element['subform']['field_os2web_spotbox_ref_subhead']['#access'] = FALSE;
break;
case 'os2web_accordion':
$element['subform']['field_os2web_accordion_subheadin']['#access'] = FALSE;
break;
case 'os2web_banner_paragraph':
$element['subform']['field_os2web_banner_subheading']['#access'] = FALSE;
break;
case 'os2web_content_reference':
$element['subform']['field_os2web_content_ref_subhead']['#access'] = FALSE;
break;
case 'os2web_simple_text_paragraph':
$element['subform']['field_os2web_simple_text_subhead']['#access'] = FALSE;
break;
case 'os2web_cnt_paragraph':
$element['subform']['field_os2web_cnt_subheading']['#access'] = FALSE;
break;
}
}
}
/**
* Get all pages that are using this section and are promoted.
*
* @param int $section_term_id
* Term ID of the section term.
*
* @return array
* Array of Node Ids.
*/
function getSectionPromotedNodes($section_term_id) {
$query = \Drupal::entityQuery('node');
$query->condition('status', 1);
$query->condition('type', 'os2web_page');
$query->condition('promote', 1);
$query->condition('field_os2web_page_section', $section_term_id);
return $query->execute();
}
/**
* Implements hook_transliteration_overrides_alter().
*/
function os2web_pagebuilder_transliteration_overrides_alter(&$overrides, $langcode) {
if ($langcode == 'da') {
// List of char to be overriden, see SettingsForms.php.
$chars = [
// Char Ø.
'0xd8',
// Char ø.
'0xf8',
// Char Å.
'0xc5',
// Char å.
'0xe5',
// Char Æ.
'0xc6',
// Char æ.
'0xe6',
];
$config = \Drupal::config(SettingsForm::$configName);
foreach ($chars as $char) {
// Add only those that were set.
if ($override_value = $config->get('os2web_pagebuilder_translit_alter_' . $char)) {
$overrides[hexdec($char)] = $override_value;
}
}
}
}