This repository has been archived by the owner on Sep 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
tide_landing_page.module
534 lines (504 loc) · 22.5 KB
/
tide_landing_page.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
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
<?php
/**
* @file
* Tide Landing Page module functionality.
*/
use Drupal\Core\Cache\Cache;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\field\Entity\FieldConfig;
use Drupal\node\NodeInterface;
use Drupal\workflows\Entity\Workflow;
/**
* Implements hook_entity_bundle_create().
*/
function tide_landing_page_entity_bundle_create($entity_type_id, $bundle) {
/** @var \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler */
$moduleHandler = \Drupal::service('module_handler');
if ($entity_type_id == 'node') {
// Support the new content type in the Referenced Content field
// of the Card automated paragraphs.
$supported_paragraph_types = [
'card_navigation_featured_auto',
'card_navigation_auto',
'card_promotion_auto',
];
foreach ($supported_paragraph_types as $supported_paragraph_type) {
$field_config = FieldConfig::loadByName("paragraph", $supported_paragraph_type, "field_paragraph_reference");
if ($field_config) {
$handler_settings = $field_config->getSetting('handler_settings');
$handler_settings['target_bundles'][$bundle] = $bundle;
$field_config->setSetting('handler_settings', $handler_settings);
$field_config->save();
}
}
// Enable Editorial workflow for landing page if workflow module is enabled.
if ($bundle == 'landing_page') {
if ($moduleHandler->moduleExists('workflows')) {
$editorial_workflow = Workflow::load('editorial');
if ($editorial_workflow) {
$editorial_workflow->getTypePlugin()
->addEntityTypeAndBundle('node', 'landing_page');
$editorial_workflow->save();
}
}
}
}
// Add the Embeded webform paragraph to Landing Page component if exists.
if ($entity_type_id == 'paragraph') {
$field_config = FieldConfig::loadByName('node', 'landing_page', 'field_landing_page_component');
if ($field_config) {
$handler_settings = $field_config->getSetting('handler_settings');
$is_embedded_webform = ($bundle == 'embedded_webform' && $moduleHandler->moduleExists('tide_webform'));
if ($is_embedded_webform) {
$handler_settings['target_bundles'][$bundle] = $bundle;
$field_config->setSetting('handler_settings', $handler_settings);
$field_config->save();
}
}
}
}
/**
* Implements hook_form_BASE_FORM_ID_alter().
*/
function tide_landing_page_form_node_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if (in_array($form_id, [
'node_landing_page_form',
'node_landing_page_edit_form',
'node_landing_page_quick_node_clone_form',
])) {
if (isset($form['title']['widget'][0]['value']['#title'])) {
$form['title']['widget'][0]['value']['#title'] = t('Page title');
}
if (isset($form['title']['widget'][0]['value']['#description'])) {
$form['title']['widget'][0]['value']['#description'] = t('Include a short unique title for your page and keywords.');
}
if (isset($form["field_landing_page_key_journeys"]["widget"][0]["subform"]["field_paragraph_title"]["widget"][0]["value"]["#default_value"])) {
$placeholder_value = $form["field_landing_page_key_journeys"]["widget"][0]["subform"]["field_paragraph_title"]["widget"][0]["value"]["#default_value"];
$form["field_landing_page_key_journeys"]["widget"][0]["subform"]["field_paragraph_title"]["widget"][0]["value"]["#placeholder"] = $placeholder_value;
unset($form["field_landing_page_key_journeys"]["widget"][0]["subform"]["field_paragraph_title"]["widget"][0]["value"]["#default_value"]);
}
// Change form layout.
$form['#attached']['library'][] = 'tide_landing_page/landing_page_form';
$form['#attributes']['class'][] = 'node-form-landing-page';
$form['#process'][] = '_tide_landing_page_form_node_form_process';
// Change the styling of paragraph widgets.
$paragraph_fields = [
'field_landing_page_header' => t('Header extra is currently blank.'),
'field_landing_page_component' => t('Page content is currently blank.'),
];
foreach ($paragraph_fields as $paragraph_field => $component_name) {
$form[$paragraph_field]['widget']['#attributes']['class'][] = 'node-landing-page-alternative-paragraph';
if (isset($form[$paragraph_field]['widget']['#max_delta'])) {
$max_delta = $form[$paragraph_field]['widget']['#max_delta'];
if ($max_delta < 0 || !isset($form[$paragraph_field]['widget'][$max_delta])) {
$form[$paragraph_field]['widget']['#description'] = t('<h4><strong>@empty_component</strong></h4>',
['@empty_component' => $component_name]) . $form[$paragraph_field]['widget']['#description'];
}
}
}
// Hide the field Show Acknowledgement to Country if tide_site is missing.
if (!\Drupal::moduleHandler()->moduleExists('tide_site')) {
$form['field_show_ack_of_country']['#attributes']['class'][] = 'hidden';
}
// Move Show Hero Image caption checkbox to Hero Image field group.
if (isset($form['field_show_hero_image_caption']) && isset($form['field_landing_page_hero_image']['widget'])) {
$form['field_landing_page_hero_image']['widget']['field_show_hero_image_caption'] = $form['field_show_hero_image_caption'];
unset($form['field_show_hero_image_caption']);
}
// Move Hero Image theme to Hero Image field group.
if (isset($form['field_landing_page_hero_theme']) && isset($form['field_landing_page_hero_image']['widget'])) {
$form['field_landing_page_hero_image']['widget']['field_landing_page_hero_theme'] = $form['field_landing_page_hero_theme'];
unset($form['field_landing_page_hero_theme']);
}
// @todo the below block needs to be in a proper widget alter.
if (isset($form['field_landing_page_component']['widget'][0])) {
foreach (Element::children($form['field_landing_page_component']['widget']) as $key) {
if (isset($form['field_landing_page_component']['widget'][$key]['subform']['field_paragraph_cta']['widget'])) {
foreach (Element::children($form['field_landing_page_component']['widget'][$key]['subform']['field_paragraph_cta']['widget']) as $delta) {
$form['field_landing_page_component']['widget'][$key]['subform']['field_paragraph_cta']['widget'][$delta]['uri']['#title'] = t("CTA link");
$form['field_landing_page_component']['widget'][$key]['subform']['field_paragraph_cta']['widget'][$delta]['uri']['#description'] = t("A link to a page showing a longer list of public holidays or special dates. Start typing the title of a page in the CMS to select it or paste in a full external URL (eg http://www.example.com).");
$form['field_landing_page_component']['widget'][$key]['subform']['field_paragraph_cta']['widget'][$delta]['title']['#title'] = t("CTA text");
$form['field_landing_page_component']['widget'][$key]['subform']['field_paragraph_cta']['widget'][$delta]['title']['#description'] = t("Type a strong call to action that describes the content of the link destination (eg Find all public holidays).");
}
}
if (isset($form['field_landing_page_component']['widget'][$key]['subform']['field_paragraph_keydates']['widget']['add_more'])) {
$form['field_landing_page_component']['widget'][$key]['subform']['field_paragraph_keydates']['widget']['add_more']['add_more_button_keydates']['#value'] = t("Add another date");
}
if (isset($form['field_landing_page_component']['widget'][$key]['subform']['field_paragraph_accordion']['widget']['add_more'])) {
$form['field_landing_page_component']['widget'][$key]['subform']['field_paragraph_accordion']['widget']['add_more']['add_more_button_accordion_content']['#value'] = t("Add another accordion item");
}
if (isset($form['field_landing_page_component']['widget'][$key]['subform']['field_paragraph_accordion']['widget'])) {
foreach (Element::children($form['field_landing_page_component']['widget'][$key]['subform']['field_paragraph_accordion']['widget']) as $delta) {
$form['field_landing_page_component']['widget'][$key]['subform']['field_paragraph_accordion']['widget'][$delta]['top']['type']['label']['#markup'] = '<span class="paragraph-type-label"></span>';
}
}
if (isset($form['field_landing_page_component']['widget'][$key]['subform']['field_paragraph_keydates']['widget'])) {
foreach (Element::children($form['field_landing_page_component']['widget'][$key]['subform']['field_paragraph_keydates']['widget']) as $delta) {
$form['field_landing_page_component']['widget'][$key]['subform']['field_paragraph_keydates']['widget'][$delta]['top']['type']['label']['#markup'] = '<span class="paragraph-type-label"></span>';
}
}
}
}
}
// Add conditional field for show table of content.
if (isset($form['field_node_display_headings']) && isset($form['field_show_table_of_content'])) {
$form['field_node_display_headings']['#states'] = [
'visible' => [
':input[name="field_show_table_of_content[value]"]' => [
'checked' => TRUE,
],
],
];
if (!isset($form['field_node_display_headings']['widget']['#default_value']) || empty($form['field_node_display_headings']['widget']['#default_value'])) {
$form['field_node_display_headings']['widget']['#default_value'] = 'showH2';
}
}
}
/**
* Implements hook_field_widget_single_element_WIDGET_TYPE_form_alter().
*/
function tide_landing_page_field_widget_single_element_paragraphs_form_alter(&$element, FormStateInterface $form_state, $context) {
/** @var \Drupal\Core\Field\FieldItemListInterface $items */
$items = $context['items'];
$field_definition = $items->getFieldDefinition();
$paragraph_field_name = $field_definition->getName();
$widget_state = WidgetBase::getWidgetState($element['#field_parents'], $paragraph_field_name, $form_state);
$paragraph = $widget_state['paragraphs'][$element['#delta']]['entity'];
$paragraph_type = $paragraph ? $paragraph->bundle() : '';
switch ($paragraph_type) {
case 'embedded_search_form':
$parent_field = $items->getName();
$condition = [
':input[name="' . $parent_field . '[' . $context['delta'] . '][subform][field_paragraph_search_block]"]' => ['value' => 'custom'],
];
if (!empty($element['subform']['field_paragraph_search_url']['widget'][0]['value'])) {
$element['subform']['field_paragraph_search_url']['#states']['visible'] = $condition;
$element['subform']['field_paragraph_search_url']['widget'][0]['value']['#states']['required'] = $condition;
}
if (!empty($element['subform']['field_paragraph_search_ph'])) {
$element['subform']['field_paragraph_search_ph']['#states']['visible'] = $condition;
}
if (!empty($element['subform']['field_paragraph_search_target'])) {
$element['subform']['field_paragraph_search_target']['#states']['visible'] = $condition;
}
break;
// Displaying field_call_to_action_title depends on banner display type.
case 'introduction_banner':
if ($paragraph_field_name === 'field_landing_page_header') {
$selector = sprintf('input[name="%s[%d][subform][%s]"]', $paragraph_field_name, $element['#delta'], 'field_banner_display_type');
if (isset($element['subform']['field_call_to_action_title'])) {
$element['subform']['field_call_to_action_title']['#states'] = [
'visible' => [
$selector => ['value' => 'buttons'],
],
];
}
if (isset($element['subform']['field_paragraph_links']['widget'])) {
foreach ($element['subform']['field_paragraph_links']['widget'] as $key => $links) {
if (is_numeric($key)) {
$element['subform']['field_paragraph_links']['widget'][$key]['title']['#title'] = t('Link text / Button label');
}
}
}
}
break;
}
if ($paragraph_field_name === 'field_landing_page_component') {
$widget_state = WidgetBase::getWidgetState($element['#field_parents'], $paragraph_field_name, $form_state);
$paragraph = $widget_state['paragraphs'][$element['#delta']]['entity'];
$paragraph_type = $paragraph ? $paragraph->bundle() : '';
// Hide table caption field in Data table component.
if ($paragraph_type == 'data_table') {
unset($element['subform']['field_data_table_content']['widget'][0]['caption']);
$element['subform']['field_data_table_content']['widget'][0]['caption'] = [
'#type' => 'textfield',
'#default_value' => NULL,
'#size' => 60,
'#attributes' => ['style' => 'display: none;'],
];
}
// Add custom validation for statistics grid.
if ($paragraph_type == 'statistics_grid') {
if (isset($element['subform']['field_statistic_block']['widget']['#max_delta'])) {
$element['#element_validate'][] = [
'Drupal\tide_landing_page\Validate\StatisticsGrid',
'validate',
];
}
}
// Add states for navigation and promotion card paragraph type fields.
if ($paragraph_type === 'navigation_card' || $paragraph_type === 'promotion_card') {
if (isset($element['subform']['field_paragraph_link'])) {
// Custom description for link field.
$element['subform']['field_paragraph_link']['widget'][0]['uri']['#description'] = t("Start typing the title of a piece of content to select it. You can also enter an internal path such as /node/add or an external URL such as http://example.com.");
// Custom validation based on the link field description.
$element['subform']['field_paragraph_link']['widget'][0]['uri']['#element_validate'][] = [
'Drupal\tide_landing_page\Validate\CardLink',
'validate',
];
}
$dependee_field_name = 'field_paragraph_link';
$selector = sprintf(':input[name^="%s[%d][subform][%s][0][uri]"]', $paragraph_field_name, $element['#delta'], $dependee_field_name);
// Dependent fields.
if (isset($element['subform']['field_paragraph_title'])) {
$element['subform']['field_paragraph_title']['widget'][0]['value']['#states'] = [
'visible' => [
$selector => [
['value' => ['pattern' => '^http']],
['value' => ''],
],
],
'required' => [
$selector => [
['value' => ['pattern' => '^http']],
['value' => ''],
],
],
];
}
if (isset($element['subform']['field_paragraph_summary'])) {
$element['subform']['field_paragraph_summary']['widget'][0]['value']['#states'] = [
'visible' => [
$selector => [
['value' => ['pattern' => '^http']],
['value' => ''],
],
],
];
$element['subform']['summary_message_group'] = [
'#type' => 'fieldset',
'#title' => 'Page title & summary will be automatically drawn from the content you have linked to.',
'#group_name' => 'Page summary message',
'#attributes' => [
'class' => [
'summary-message-group',
],
],
'#states' => [
'invisible' => [
$selector => [
['value' => ['pattern' => '^http']],
['value' => ''],
],
],
],
];
}
if (isset($element['subform']['field_customise'])) {
$element['subform']['customise_field_group'] = [
'#type' => 'fieldset',
'#title' => 'Customise',
'#group_name' => 'Page summary message',
'#attributes' => [
'class' => [
'customise-field-group',
],
],
'#weight' => '20',
'#states' => [
'invisible' => [
$selector => [
['value' => ['pattern' => '^http']],
['value' => ''],
],
],
],
];
$element['subform']['field_customise']['#states'] = [
'invisible' => [
$selector => [
['value' => ['pattern' => '^http']],
['value' => ''],
],
],
];
$element['subform']['customise_field_group']['field_customise'] = $element['subform']['field_customise'];
unset($element['subform']['field_customise']);
}
// Add state for media field.
$media_dependee_field_name = '';
$options = '';
if (isset($element['subform']['field_promo_card_display_style'])) {
$media_dependee_field_name = 'field_promo_card_display_style';
$options = [
['value' => 'thumbnail'],
['value' => 'profile'],
];
}
if (isset($element['subform']['field_nav_card_display_style'])) {
$media_dependee_field_name = 'field_nav_card_display_style';
$options = [
['value' => 'thumbnail'],
['value' => 'featured'],
];
}
if (!empty($media_dependee_field_name) && !empty($options)) {
$media_selector = sprintf(':input[name^="%s[%d][subform][%s]"]', $paragraph_field_name, $element['#delta'], $media_dependee_field_name);
// Dependent field.
if (isset($element['subform']['field_paragraph_media'])) {
$element['subform']['field_paragraph_media']['#states'] = [
'visible' => [
$media_selector => $options,
$selector => [
['value' => ['pattern' => '^http']],
],
],
];
}
}
}
}
}
/**
* Node form #process callback.
*
* @param array $form
* Form that is being processed.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param array $complete_form
* The complete form.
*
* @return array
* The processed form.
*/
function _tide_landing_page_form_node_form_process(array $form, FormStateInterface $form_state, array $complete_form = []) {
/** @var \Drupal\node\NodeInterface $node */
$node = $form_state->getFormObject()->getEntity();
// Add pseudo radio buttons for Header style.
$form['_header_style'] = [
'#type' => 'container',
'#group' => 'group_customised_header',
'#weight' => -100,
];
$form['_header_style']['_header_style_options'] = [
'#title' => t('Header style'),
'#description' => t('The header can be customised to incorporate photography and other design elements.'),
'#type' => 'radios',
'#required' => TRUE,
'#options' => [
'default' => t('Default appearance'),
'fullwidth' => t('Full-width background image'),
'corner' => t('Corner graphics'),
'cta' => t('Call to action banner'),
],
];
$form['field_landing_page_hero_image']['#states']['visible'] = [
':input[name="_header_style_options"]' => [
['value' => 'fullwidth'],
['value' => 'cta'],
],
];
$form['field_graphical_image']['#states']['visible'] = [
':input[name="_header_style_options"]' => ['value' => 'corner'],
];
$form['field_bottom_graphical_image']['#states']['visible'] = [
':input[name="_header_style_options"]' => ['value' => 'corner'],
];
$form['field_landing_page_hero_banner']['#states']['visible'] = [
':input[name="_header_style_options"]' => ['value' => 'cta'],
];
// Determine the initial value of Header style.
$node_hero_image_empty = $node->get('field_landing_page_hero_image')->isEmpty();
$node_hero_banner_empty = $node->get('field_landing_page_hero_banner')->isEmpty();
$node_hero_top_graphical_empty = $node->get('field_graphical_image')->isEmpty();
$node_hero_bottom_graphical_empty = $node->get('field_bottom_graphical_image')->isEmpty();
$header_style = 'default';
if (!$node_hero_banner_empty) {
$header_style = 'cta';
}
elseif (!$node_hero_image_empty) {
$header_style = 'fullwidth';
}
elseif (!$node_hero_top_graphical_empty || !$node_hero_bottom_graphical_empty) {
$header_style = 'corner';
}
$form['_header_style']['_header_style_options']['#default_value'] = $header_style;
$form['#after_build'][] = '_tide_landing_page_form_node_form_after_build';
return $form;
}
/**
* Node form #after_build callback.
*
* @param array $form
* Form that is being processed.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @return array
* The processed form.
*/
function _tide_landing_page_form_node_form_after_build(array $form, FormStateInterface $form_state) {
// Determine the current value of Header style.
$form_hero_image_empty = empty($form_state->getValue([
'field_landing_page_hero_image',
'target_id',
]));
$form_hero_banner_empty = empty($form_state->getValue([
'field_landing_page_hero_banner',
0,
]));
$form_hero_top_graphical_empty = empty($form_state->getValue([
'field_graphical_image',
'target_id',
]));
$form_hero_bottom_graphical_empty = empty($form_state->getValue([
'field_bottom_graphical_image',
'target_id',
]));
$header_style = 'default';
if (!$form_hero_image_empty) {
$header_style = 'cta';
}
elseif (!$form_hero_banner_empty) {
$header_style = 'fullwidth';
}
elseif (!$form_hero_top_graphical_empty || !$form_hero_bottom_graphical_empty) {
$header_style = 'corner';
}
$form['_header_style']['_header_style_options']['#default_value'] = $header_style;
return $form;
}
/**
* Get all referenced nodes for a node in promotion & navigation card paragraph.
*
* @param \Drupal\node\NodeInterface $node
* The node context.
*
* @return null|array
* The node ids.
*/
function _tide_landing_page_get_all_ref_nodes(NodeInterface $node) {
if ($node->id()) {
$query = \Drupal::database()->select('paragraphs_item_field_data', 'p');
$query->fields('p', ['parent_id']);
$query->fields('pl', ['field_paragraph_link_uri']);
$query->leftJoin('paragraph__field_paragraph_link', 'pl', 'pl.entity_id = p.id');
$query->condition('pl.field_paragraph_link_uri', 'entity:node/' . $node->id());
$or = $query->orConditionGroup();
$or->condition('type', 'navigation_card');
$or->condition('type', 'promotion_card');
$node_ids = $query->condition($or)->execute()->fetchCol();
if ($node_ids) {
$cids = [];
foreach ($node_ids as $node_id) {
$cids[] = 'node:' . $node_id;
}
return $cids;
}
}
return NULL;
}
/**
* Implements hook_ENTITY_TYPE_presave().
*/
function tide_landing_page_node_presave(NodeInterface $node) {
if (!$node->isNew()) {
$cids = _tide_landing_page_get_all_ref_nodes($node);
if (!empty($cids)) {
Cache::invalidateTags($cids);
}
}
}