-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebform.module
More file actions
1167 lines (1064 loc) · 41.2 KB
/
webform.module
File metadata and controls
1167 lines (1064 loc) · 41.2 KB
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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* Enables the creation of webforms and questionnaires.
*/
use Drupal\Component\Serialization\Json;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Render\Markup;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
use Drupal\file\FileInterface;
use Drupal\webform\Entity\Webform;
use Drupal\webform\Entity\WebformSubmission;
use Drupal\webform\Plugin\WebformElement\ManagedFile;
use Drupal\webform\Plugin\WebformElementFileDownloadAccessInterface;
use Drupal\webform\Utility\WebformElementHelper;
use Drupal\webform\Utility\WebformMailHelper;
use Drupal\webform\Utility\WebformOptionsHelper;
use Drupal\webform\WebformInterface;
require_once __DIR__ . '/includes/webform.date.inc';
require_once __DIR__ . '/includes/webform.editor.inc';
require_once __DIR__ . '/includes/webform.form_alter.inc';
require_once __DIR__ . '/includes/webform.libraries.inc';
require_once __DIR__ . '/includes/webform.options.inc';
require_once __DIR__ . '/includes/webform.theme.inc';
require_once __DIR__ . '/includes/webform.translation.inc';
require_once __DIR__ . '/includes/webform.query.inc';
/**
* Implements hook_help().
*/
function webform_help($route_name, RouteMatchInterface $route_match) {
if (!$route_match->getRouteObject()) {
return NULL;
}
// Get path from route match.
$path = preg_replace('/^' . preg_quote(base_path(), '/') . '/', '/', Url::fromRouteMatch($route_match)->setAbsolute(FALSE)->toString());
if (!in_array($route_name, ['system.modules_list', 'update.status']) && strpos($route_name, 'webform') === FALSE && strpos($path, '/webform') === FALSE) {
return NULL;
}
/** @var \Drupal\webform\WebformHelpManagerInterface $help_manager */
$help_manager = \Drupal::service('webform.help_manager');
if ($route_name === 'help.page.webform') {
$build = $help_manager->buildIndex();
}
else {
$build = $help_manager->buildHelp($route_name, $route_match);
}
if ($build) {
$renderer = \Drupal::service('renderer');
$config = \Drupal::config('webform.settings');
$renderer->addCacheableDependency($build, $config);
return $build;
}
else {
return NULL;
}
}
/**
* Implements hook_webform_message_custom().
*/
function webform_webform_message_custom($operation, $id) {
if (strpos($id, 'webform_help_notification__') === 0 && $operation === 'close') {
$id = str_replace('webform_help_notification__', '', $id);
/** @var \Drupal\webform\WebformHelpManagerInterface $help_manager */
$help_manager = \Drupal::service('webform.help_manager');
$help_manager->deleteNotification($id);
}
}
/**
* Implements hook_modules_installed().
*/
function webform_modules_installed($modules) {
// Add webform paths when the path.module is being installed.
if (in_array('path', $modules)) {
/** @var \Drupal\webform\WebformInterface[] $webforms */
$webforms = Webform::loadMultiple();
foreach ($webforms as $webform) {
$webform->updatePaths();
}
}
// Check HTML email provider support as modules are installed.
/** @var \Drupal\webform\WebformEmailProviderInterface $email_provider */
$email_provider = \Drupal::service('webform.email_provider');
$email_provider->check();
// Update Webform HTML editor.
if (in_array('ckeditor', $modules)
|| in_array('ckeditor5', $modules)) {
\Drupal::moduleHandler()->loadInclude('webform', 'inc', 'includes/webform.install') .
_webform_update_html_editor();
}
}
/**
* Implements hook_modules_uninstalled().
*/
function webform_modules_uninstalled($modules) {
// Remove uninstalled module's third party settings from admin settings.
$config = \Drupal::configFactory()->getEditable('webform.settings');
$third_party_settings = $config->get('third_party_settings');
$has_third_party_settings = FALSE;
foreach ($modules as $module) {
if (isset($third_party_settings[$module])) {
$has_third_party_settings = TRUE;
unset($third_party_settings[$module]);
}
}
if ($has_third_party_settings) {
$config->set('third_party_settings', $third_party_settings);
$config->save();
}
// Check HTML email provider support as modules are uninstalled.
/** @var \Drupal\webform\WebformEmailProviderInterface $email_provider */
$email_provider = \Drupal::service('webform.email_provider');
$email_provider->check();
// Update Webform HTML editor.
if (in_array('ckeditor', $modules)
|| in_array('ckeditor5', $modules)) {
\Drupal::moduleHandler()->loadInclude('webform', 'inc', 'includes/webform.install') .
_webform_update_html_editor();
}
}
/**
* Implements hook_config_schema_info_alter().
*/
function webform_config_schema_info_alter(&$definitions) {
if (empty($definitions['webform.webform.*']['mapping'])) {
return;
}
$mapping = $definitions['webform.webform.*']['mapping'];
// Copy setting, elements, and handlers to variant override schema.
if (isset($definitions['webform.variant.override'])) {
$definitions['webform.variant.override']['mapping'] += [
'settings' => $mapping['settings'],
'elements' => $mapping['elements'],
'handlers' => $mapping['handlers'],
];
}
// Append settings handler settings schema.
if (isset($definitions['webform.handler.settings'])) {
$definitions['webform.handler.settings']['mapping'] += _webform_config_schema_info_alter_settings_recursive($mapping['settings']['mapping']);
}
}
/**
* Convert most data types to 'string' to support tokens.
*
* @param array $settings
* An associative array of schema settings.
*
* @return array
* An associative array of schema settings with most data types to 'string'
* to support tokens
*/
function _webform_config_schema_info_alter_settings_recursive(array $settings) {
foreach ($settings as $name => $setting) {
if (is_array($setting)) {
$settings[$name] = _webform_config_schema_info_alter_settings_recursive($setting);
}
elseif ($name === 'type' && in_array($setting, ['boolean', 'integer', 'float', 'uri', 'email'])) {
$settings[$name] = 'string';
}
}
return $settings;
}
/**
* Implements hook_user_login().
*/
function webform_user_login($account) {
// Notify the storage of this log in.
\Drupal::entityTypeManager()->getStorage('webform_submission')->userLogin($account);
}
/**
* Implements hook_cron().
*/
function webform_cron() {
$config = \Drupal::config('webform.settings');
\Drupal::entityTypeManager()->getStorage('webform_submission')->purge($config->get('purge.cron_size'));
}
/**
* Implements hook_rebuild().
*/
function webform_rebuild() {
/** @var \Drupal\webform\WebformEmailProviderInterface $email_provider */
$email_provider = \Drupal::service('webform.email_provider');
$email_provider->check();
}
/**
* Implements hook_local_tasks_alter().
*/
function webform_local_tasks_alter(&$local_tasks) {
// Change config translation local task hierarchy.
if (isset($local_tasks['config_translation.local_tasks:entity.webform.config_translation_overview'])) {
$local_tasks['config_translation.local_tasks:entity.webform.config_translation_overview']['base_route'] = 'entity.webform.canonical';
}
if (isset($local_tasks['config_translation.local_tasks:config_translation.item.overview.webform.config'])) {
// Set weight to 110 so that the 'Translate' tab comes after
// the 'Advanced' tab.
// @see webform.links.task.yml
$local_tasks['config_translation.local_tasks:config_translation.item.overview.webform.config']['weight'] = 110;
$local_tasks['config_translation.local_tasks:config_translation.item.overview.webform.config']['parent_id'] = 'webform.config';
}
// Disable 'Contribute' tab if explicitly disabled or the Contribute module
// is installed.
if (\Drupal::config('webform.settings')->get('ui.contribute_disabled') || \Drupal::moduleHandler()->moduleExists('contribute')) {
unset($local_tasks['webform.contribute']);
}
}
/**
* Implements hook_menu_local_tasks_alter().
*/
function webform_menu_local_tasks_alter(&$data, $route_name, RefinableCacheableDependencyInterface $cacheability) {
// Change config entities 'Translate *' tab to be just label 'Translate'.
$webform_entities = [
'webform',
'webform_options',
];
foreach ($webform_entities as $webform_entity) {
if (isset($data['tabs'][0]["config_translation.local_tasks:entity.$webform_entity.config_translation_overview"]['#link']['title'])) {
$data['tabs'][0]["config_translation.local_tasks:entity.$webform_entity.config_translation_overview"]['#link']['title'] = t('Translate');
}
}
// Change simple config 'Translate *' tab to be just label 'Translate'.
if (isset($data['tabs'][1]['config_translation.local_tasks:config_translation.item.overview.webform.config'])) {
$data['tabs'][1]['config_translation.local_tasks:config_translation.item.overview.webform.config']['#link']['title'] = t('Translate');
}
// ISSUE:
// Devel routes do not use 'webform' parameter which throws the below error.
// Some mandatory parameters are missing ("webform") to generate a URL for
// route "entity.webform_submission.canonical"
//
// WORKAROUND:
// Make sure webform parameter is set for all routes.
if (strpos($route_name, 'entity.webform_submission.devel_') === 0 || $route_name === 'entity.webform_submission.token_devel') {
foreach ($data['tabs'] as $tab_level) {
foreach ($tab_level as $tab) {
/** @var \Drupal\Core\Url $url */
$url = $tab['#link']['url'];
$tab_route_name = $url->getRouteName();
$tab_route_parameters = $url->getRouteParameters();
if (strpos($tab_route_name, 'entity.webform_submission.devel_') !== 0) {
$webform_submission = WebformSubmission::load($tab_route_parameters['webform_submission']);
$url->setRouteParameter('webform', $webform_submission->getWebform()->id());
}
}
}
}
// Allow webform query string parameters to be transferred
// from a canonical URL to a test URL.
//
// Please note: This behavior is only applicable when a user can
// test a webform.
$route_names = [
'entity.webform.test_form' => 'entity.webform.canonical',
'entity.node.webform.test_form' => 'entity.node.canonical',
];
if (in_array($route_name, $route_names) || array_key_exists($route_name, $route_names)) {
$query = \Drupal::request()->query->all();
$has_test_tab = FALSE;
foreach ($route_names as $test_route_name => $view_route_name) {
if (isset($data['tabs'][0][$test_route_name])) {
$has_test_tab = TRUE;
if ($query) {
$data['tabs'][0][$test_route_name]['#link']['url']
->setOption('query', $query);
$data['tabs'][0][$view_route_name]['#link']['url']
->setOption('query', $query);
}
}
}
// Query string to cache context webform canonical and test routes.
if ($has_test_tab) {
$cacheability->addCacheContexts(['url.query_args']);
}
}
}
/**
* Implements hook_module_implements_alter().
*/
function webform_module_implements_alter(&$implementations, $hook) {
if ($hook === 'form_alter') {
$implementation = $implementations['webform'];
unset($implementations['webform']);
$implementations['webform'] = $implementation;
}
}
/**
* Implements hook_token_info_alter().
*/
function webform_token_info_alter(&$data) {
\Drupal::moduleHandler()->loadInclude('webform', 'tokens.inc');
// Append learn more about token suffixes to all webform token descriptions.
// @see \Drupal\webform\WebformTokenManager::replace
// @see webform_page_attachments()
$token_suffixes = t('Append the below suffixes to alter the returned value.') .
'<ul>' .
'<li>' . t('<code>:base64encode</code> base64 encodes returned value') . '</li>' .
'<li>' . t('<code>:clear</code> removes the token when it is not replaced.') . '</li>' .
'<li>' . t('<code>:urlencode</code> URL encodes returned value.') . '</li>' .
'<li>' . t('<code>:rawurlencode</code> Raw URL encodes returned value with only hex digits.') . '</li>' .
'<li>' . t('<code>:xmlencode</code> XML encodes returned value.') . '</li>' .
'<li>' . t('<code>:htmldecode</code> decodes HTML entities in returned value.') . '<br/><b>' . t('This suffix has security implications.') . '</b><br/>' . t('Use <code>:htmldecode</code> with <code>:striptags</code>.') . '</li>' .
'<li>' . t('<code>:striptags</code> removes all HTML tags from returned value.') . '</li>' .
'</ul>';
$more = _webform_token_render_more(t('Learn about token suffixes'), $token_suffixes);
foreach ($data['types'] as $type => &$info) {
if (strpos($type, 'webform') === 0) {
if (isset($info['description']) && !empty($info['description'])) {
$description = $info['description'] . $more;
}
else {
$description = $more;
}
$info['description'] = Markup::create($description);
}
}
}
/**
* Implements hook_entity_update().
*/
function webform_entity_update(EntityInterface $entity) {
_webform_clear_webform_submission_list_cache_tag($entity);
}
/**
* Implements hook_entity_delete().
*/
function webform_entity_delete(EntityInterface $entity) {
_webform_clear_webform_submission_list_cache_tag($entity);
/** @var \Drupal\webform\WebformEntityReferenceManagerInterface $entity_reference_manager */
$entity_reference_manager = \Drupal::service('webform.entity_reference_manager');
// Delete saved export settings for a webform or source entity with the
// webform field.
if (($entity instanceof WebformInterface) || $entity_reference_manager->hasField($entity)) {
$name = 'webform.export.' . $entity->getEntityTypeId() . '.' . $entity->id();
\Drupal::state()->delete($name);
}
}
/**
* Invalidate 'webform_submission_list' cache tag when user or role is updated.
*
* Once the below issue is resolved we should rework this approach.
*
* Issue #2811041: Allow views base tables to define additional
* cache tags and max age.
* https://www.drupal.org/project/drupal/issues/2811041
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* An entity.
*
* @see \Drupal\webform\Entity\WebformSubmission
* @see webform_query_webform_submission_access_alter()
*/
function _webform_clear_webform_submission_list_cache_tag(EntityInterface $entity) {
if ($entity->getEntityTypeId() === 'user') {
$original_target_ids = [];
if ($entity->original) {
foreach ($entity->original->roles as $item) {
$original_target_ids[$item->target_id] = $item->target_id;
}
}
$target_ids = [];
foreach ($entity->roles as $item) {
$target_ids[$item->target_id] = $item->target_id;
}
if (array_diff_assoc($original_target_ids, $target_ids)) {
Cache::invalidateTags(['webform_submission_list']);
}
}
elseif ($entity->getEntityTypeId() === 'user_role') {
Cache::invalidateTags(['webform_submission_list']);
}
}
/**
* Implements hook_mail().
*/
function webform_mail($key, &$message, $params) {
// Never send emails when using devel generate to create
// 1000's of submissions.
if (\Drupal::moduleHandler()->moduleExists('devel_generate')) {
/** @var \Drupal\devel_generate\DevelGeneratePluginManager $devel_generate */
$devel_generate = \Drupal::service('plugin.manager.develgenerate');
$definition = $devel_generate->getDefinition('webform_submission', FALSE);
if ($definition) {
$class = $definition['class'];
if ($class::isGeneratingSubmissions()) {
$message['send'] = FALSE;
}
}
}
// Set default parameters.
$params += [
'from_mail' => '',
'from_name' => '',
'cc_mail' => '',
'bcc_mail' => '',
'reply_to' => '',
'return_path' => '',
'sender_mail' => '',
'sender_name' => '',
];
$message['subject'] = $params['subject'];
$message['body'][] = $params['body'];
// Set the header 'From'.
// Using the 'from_mail' so that the webform's email from value is used
// instead of site's email address.
// @see: \Drupal\Core\Mail\MailManager::mail.
if (!empty($params['from_mail'])) {
// 'From name' is only used when the 'From mail' contains a single
// email address.
$from = (!empty($params['from_name']) && strpos($params['from_mail'], ',') === FALSE)
? WebformMailHelper::formatAddress($params['from_mail'], $params['from_name'])
: $params['from_mail'];
$message['from'] = $message['headers']['From'] = $from;
}
// Set header 'Cc'.
if (!empty($params['cc_mail'])) {
$message['headers']['Cc'] = $params['cc_mail'];
}
// Set header 'Bcc'.
if (!empty($params['bcc_mail'])) {
$message['headers']['Bcc'] = $params['bcc_mail'];
}
// Set header 'Reply-to'.
$reply_to = $params['reply_to'] ?: '';
if (empty($reply_to) && !empty($params['from_mail'])) {
$reply_to = $message['from'];
}
if ($reply_to) {
$message['reply-to'] = $message['headers']['Reply-to'] = $reply_to;
}
// Set header 'Return-Path' which only supports a single email address and the
// 'from_mail' may contain multiple comma delimited email addresses.
$return_path = $params['return_path'] ?: $params['from_mail'] ?: '';
if ($return_path) {
$return_path = explode(',', $return_path);
$message['headers']['Sender'] = $message['headers']['Return-Path'] = $return_path[0];
}
// Set header 'Sender'.
$sender_mail = $params['sender_mail'] ?: '';
$sender_name = $params['sender_name'] ?: $params['from_name'] ?: '';
if ($sender_mail) {
$message['headers']['Sender'] = WebformMailHelper::formatAddress($sender_mail, $sender_name);
}
}
/**
* Implements hook_mail_alter().
*/
function webform_mail_alter(&$message) {
// Drupal hardcodes all mail header as 'text/plain' so we need to set the
// header's 'Content-type' to HTML if the EmailWebformHandler's
// 'html' flag has been set.
// @see \Drupal\Core\Mail\MailManager::mail()
// @see \Drupal\webform\Plugin\WebformHandler\EmailWebformHandler::getMessage().
if (strpos($message['id'], 'webform') === 0) {
if (isset($message['params']['html']) && $message['params']['html']) {
$message['headers']['Content-Type'] = 'text/html; charset=UTF-8; format=flowed';
}
}
}
/**
* Implements hook_toolbar_alter().
*/
function webform_toolbar_alter(&$items) {
if (\Drupal::config('webform.settings')->get('ui.toolbar_item')) {
$items['administration']['#attached']['library'][] = 'webform/webform.admin.toolbar';
}
}
/**
* Implements hook_menu_links_discovered_alter().
*/
function webform_menu_links_discovered_alter(&$links) {
// Display Webforms as a top-level administration menu item in the toolbar.
if (\Drupal::config('webform.settings')->get('ui.toolbar_item')) {
$links['entity.webform.collection']['parent'] = 'system.admin';
$links['entity.webform.collection']['weight'] = -9;
}
// Add webform local tasks as admin menu toolbar menu items.
if (\Drupal::moduleHandler()->moduleExists('admin_toolbar_tools')) {
// Get local task definitions.
/** @var \Drupal\Core\Menu\LocalTaskManager $local_task_manager */
$local_task_manager = \Drupal::service('plugin.manager.menu.local_task');
$definitions = $local_task_manager->getDefinitions();
// Set default definition.
$default_definition = [
'provider' => 'webform',
'menu_name' => 'admin',
];
// Get keys to be copied.
$keys = ['title', 'route_name', 'weight'];
$keys_to_copy = array_combine($keys, $keys);
$menu_links = [];
foreach ($definitions as $task_name => $definition) {
if (isset($definition['base_route']) && $definition['base_route'] === 'entity.webform.collection') {
$menu_links[$task_name . '.item'] = $default_definition
+ array_intersect_key($definition, $keys_to_copy)
+ ['parent' => 'entity.webform.collection'];
}
}
foreach ($menu_links as $sub_link_task_name => $sub_link) {
foreach ($definitions as $task_name => $definition) {
if (isset($definition['parent_id']) && $definition['parent_id'] === preg_replace('/\.item$/', '', $sub_link_task_name)) {
$menu_links[$task_name . '.item'] = $default_definition
+ array_intersect_key($definition, $keys_to_copy)
+ ['parent' => $sub_link_task_name];
}
}
}
// Make sure weight are integers and not floats which throw fatal errors
// for PostgreSQL.
// @see https://www.drupal.org/project/webform/issues/3247861
// @see https://www.drupal.org/project/drupal/issues/3248199
foreach ($menu_links as &$menu_link) {
$menu_link['weight'] = (int) $menu_link['weight'];
}
$links += $menu_links;
}
}
/**
* Implements hook_page_attachments().
*/
function webform_page_attachments(array &$attachments) {
$route_name = \Drupal::routeMatch()->getRouteName();
// Attach global libraries only to webform specific pages and module list.
if (preg_match('/^(webform\.|^entity\.([^.]+\.)?webform)/', $route_name)
|| $route_name === 'system.modules_list') {
_webform_page_attachments($attachments);
}
// Attach codemirror and select2 library to block admin to ensure that the
// library is loaded by the webform block is placed using Ajax.
if (strpos($route_name, 'block.admin_display') === 0) {
$attachments['#attached']['library'][] = 'webform/webform.block';
}
// Attach webform dialog library and options to every page.
if (\Drupal::config('webform.settings')->get('settings.dialog')) {
$attachments['#attached']['library'][] = 'webform/webform.dialog';
$attachments['#attached']['drupalSettings']['webform']['dialog']['options'] = \Drupal::config('webform.settings')->get('settings.dialog_options');
/** @var \Drupal\webform\WebformRequestInterface $request_handler */
$request_handler = \Drupal::service('webform.request');
if ($source_entity = $request_handler->getCurrentSourceEntity()) {
$attachments['#attached']['drupalSettings']['webform']['dialog']['entity_type'] = $source_entity->getEntityTypeId();
$attachments['#attached']['drupalSettings']['webform']['dialog']['entity_id'] = $source_entity->id();
}
}
// Attach webform more element to token token help.
// @see webform_token_info_alter()
if ($route_name === 'help.page' && \Drupal::routeMatch()->getRawParameter('name') === 'token') {
$attachments['#attached']['library'][] = 'webform/webform.token';
}
// Attach meta tag robots noindex directive to all webform confirmation pages
// , if the metatag module is not installed.
// @see webform_metatags_alter()
if (!\Drupal::moduleHandler()->moduleExists('metatag')
&& !empty(\Drupal::config('webform.settings')->get('settings.default_confirmation_noindex'))
&& preg_match('/^(entity\.webform\.confirmation|entity\.[a-z-_]+\.webform\.confirmation)$/', $route_name)) {
$attachments['#attached']['html_head'][] = [
[
'#tag' => 'meta',
'#attributes' => ['name' => 'robots', 'content' => 'noindex'],
],
'webform_confirmation_noindex',
];
}
}
/**
* Implements hook_metatags_alter().
*/
function webform_metatags_alter(array &$metatags, array &$context) {
$route_name = \Drupal::routeMatch()->getRouteName();
if (!empty(\Drupal::config('webform.settings')->get('settings.default_confirmation_noindex'))
&& $route_name
&& preg_match('/^(entity\.webform\.confirmation|entity\.[a-z-_]+\.webform\.confirmation)$/', $route_name)) {
$robots = (!empty($metatags['robots']))
? preg_split('/\s*,\s*/', $metatags['robots'])
: [];
$robots[] = 'noindex';
$metatags['robots'] = implode(', ', array_unique($robots));
}
}
/**
* Add webform libraries to page attachments.
*
* @param array $attachments
* An array of page attachments.
*/
function _webform_page_attachments(array &$attachments) {
// Attach webform theme specific libraries.
/** @var \Drupal\webform\WebformThemeManagerInterface $theme_manager */
$theme_manager = \Drupal::service('webform.theme_manager');
$active_theme_names = $theme_manager->getActiveThemeNames();
foreach ($active_theme_names as $active_theme_name) {
if (file_exists(__DIR__ . "/css/webform.theme.$active_theme_name.css")) {
$attachments['#attached']['library'][] = "webform/webform.theme.$active_theme_name";
}
}
// Attach webform contextual link helper.
if (\Drupal::currentUser()->hasPermission('access contextual links')) {
$attachments['#attached']['library'][] = 'webform/webform.contextual';
}
// Attach details element save open/close library.
// This ensures pages without a webform will still be able to save the
// details element state.
if (\Drupal::config('webform.settings')->get('ui.details_save')) {
$attachments['#attached']['library'][] = 'webform/webform.element.details.save';
}
// Add 'info' message style to all webform pages.
$attachments['#attached']['library'][] = 'webform/webform.element.message';
// Get current webform, if it does not exist exit.
/** @var \Drupal\webform\WebformRequestInterface $request_handler */
$request_handler = \Drupal::service('webform.request');
$webform = $request_handler->getCurrentWebform();
if (!$webform) {
return;
}
// Assets: Add custom shared and webform specific CSS and JS.
// @see webform_library_info_build()
$assets = $webform->getAssets();
foreach ($assets as $type => $value) {
if ($value) {
$attachments['#attached']['library'][] = 'webform/webform.' . $type . '.' . $webform->id();
}
}
// Attach variant randomization JavaScript.
$route_name = \Drupal::routeMatch()->getRouteName();
$route_names = [
'entity.webform.canonical',
'entity.webform.test_form',
'entity.node.canonical',
'entity.node.webform.test_form',
// Webform Share module routes.
'entity.webform.share_page',
'entity.webform.share_page.javascript',
];
if (in_array($route_name, $route_names)) {
$variants = [];
$element_keys = $webform->getElementsVariant();
foreach ($element_keys as $element_key) {
$element = $webform->getElement($element_key);
if (!empty($element['#prepopulate']) && !empty($element['#randomize'])) {
$variant_plugins = $webform->getVariants(NULL, TRUE, $element_key);
if ($variant_plugins->count()) {
$variants[$element_key] = array_values($variant_plugins->getInstanceIds());
}
else {
$attachments['#attached']['html_head'][] = [
[
'#type' => 'html_tag',
'#tag' => 'script',
'#value' => Markup::create("
(function(){
try {
if (window.sessionStorage) {
var key = 'Drupal.webform.{$webform->id()}.variant.{$element_key}';
window.sessionStorage.removeItem(key);
}
}
catch(e) {}
})();
"),
'#weight' => 1000,
],
'webform_variant_' . $element_key . '_clear',
];
}
}
}
if ($variants) {
// Using JavaScript for redirection allows pages to be cached
// by URL with querystring parameters.
$json_variants = Json::encode($variants);
$attachments['#attached']['html_head'][] = [
[
'#type' => 'html_tag',
'#tag' => 'script',
'#value' => Markup::create("
(function(){
var hasSessionStorage = (function () {
try {
sessionStorage.setItem('webform', 'webform');
sessionStorage.removeItem('webform');
return true;
}
catch (e) {
return false;
}
}());
function getSessionVariantID(variant_key) {
if (hasSessionStorage) {
var key = 'Drupal.webform.{$webform->id()}.variant.' + variant_key;
return window.sessionStorage.getItem(key);
}
return null;
}
function setSessionVariantID(variant_key, variant_id) {
if (hasSessionStorage) {
var key = 'Drupal.webform.{$webform->id()}.variant.' + variant_key;
window.sessionStorage.setItem(key, variant_id);
}
}
var variants = $json_variants;
var search = location.search;
var element_key, variant_ids, variant_id;
for (element_key in variants) {
if (variants.hasOwnProperty(element_key)
&& !search.match(new RegExp('[?&]' + element_key + '='))) {
variant_ids = variants[element_key];
variant_id = getSessionVariantID(element_key);
if (!variant_ids.includes(variant_id)) {
variant_id = variant_ids[Math.floor(Math.random() * variant_ids.length)];
setSessionVariantID(element_key, variant_id);
}
search += (search ? '&' : '?') + element_key + '=' + variant_id;
}
}
if (search !== location.search) {
location.replace(location.pathname + search);
}
})();
"),
'#weight' => 1000,
],
'webform_variant_randomize',
];
}
}
}
/**
* Implements hook_file_access().
*
* @see file_file_download()
* @see webform_preprocess_file_link()
*/
function webform_file_access(FileInterface $file, $operation, AccountInterface $account) {
$is_webform_download = ($operation === 'download' && strpos($file->getFileUri(), 'private://webform/') === 0);
// Block access to temporary anonymous private file uploads
// only when an anonymous user is attempting to download the file.
// Links to anonymous file uploads are automatically suppressed.
// @see webform_preprocess_file_link()
// @see webform_file_download()
if ($is_webform_download
&& $file->isTemporary()
&& $file->getOwner() && $file->getOwner()->isAnonymous()
&& \Drupal::routeMatch()->getRouteName() === 'system.files') {
return AccessResult::forbidden();
}
// Allow access to files associated with a webform submission.
// This prevent uploaded webform files from being lost when another user
// edits a submission with multiple file uploads.
// @see \Drupal\file\Element\ManagedFile::valueCallback
if ($is_webform_download && ManagedFile::accessFile($file, $account)) {
return AccessResult::allowed();
}
return AccessResult::neutral();
}
/**
* Implements hook_file_download().
*/
function webform_file_download($uri) {
/** @var \Drupal\webform\Plugin\WebformElementManagerInterface $webform_element_manager */
$webform_element_manager = \Drupal::service('plugin.manager.webform.element');
$webform_elements = $webform_element_manager->getInstances();
foreach ($webform_elements as $webform_element) {
if ($webform_element->isEnabled() && $webform_element instanceof WebformElementFileDownloadAccessInterface) {
$result = $webform_element::accessFileDownload($uri);
if ($result !== NULL) {
return $result;
}
}
}
return NULL;
}
/**
* Checks for files with names longer than can be stored in the database.
*
* @param \Drupal\file\FileInterface $file
* A file entity.
*
* @return array
* An empty array if the file name length is smaller than the limit or an
* array containing an error message if it's not or is empty.
*
* @see file_validate_name_length()
*/
function webform_file_validate_name_length(FileInterface $file) {
$errors = [];
// Don't display error is the file_validate_name_length() has already
// displayed a warning because the files length is over 240.
if (strlen($file->getFilename()) > 240) {
return $errors;
}
if (strlen($file->getFilename()) > 150) {
$errors[] = t("The file's name exceeds the Webform module's 150 characters limit. Please rename the file and try again.");
}
return $errors;
}
/**
* Implements hook_contextual_links_view_alter().
*
* Add .webform-contextual class to all webform context links.
*
* @see webform.links.contextual.yml
* @see js/webform.contextual.js
*/
function webform_contextual_links_view_alter(&$element, $items) {
$links = [
'entitywebformtest-form',
'entitywebformresults-submissions',
'entitywebformedit-form',
'entitywebformsettings',
];
foreach ($links as $link) {
if (isset($element['#links'][$link])) {
$element['#links'][$link]['attributes']['class'][] = 'webform-contextual';
}
}
}
/**
* Implements hook_webform_access_rules().
*/
function webform_webform_access_rules() {
return [
'create' => [
'title' => t('Create submissions'),
'roles' => [
'anonymous',
'authenticated',
],
],
'view_any' => [
'title' => t('View any submissions'),
],
'update_any' => [
'title' => t('Update any submissions'),
],
'delete_any' => [
'title' => t('Delete any submissions'),
],
'purge_any' => [
'title' => t('Purge any submissions'),
],
'view_own' => [
'title' => t('View own submissions'),
],
'update_own' => [
'title' => t('Update own submissions'),
],
'delete_own' => [
'title' => t('Delete own submissions'),
],
'administer' => [
'title' => t('Administer webform & submissions'),
'description' => [
'#type' => 'webform_message',
'#message_type' => 'warning',
'#message_message' => t('<strong>Warning</strong>: The below settings give users, permissions, and roles full access to this webform and its submissions.'),
],
],
'test' => [
'title' => t('Test webform'),
],
'configuration' => [
'title' => t('Access webform configuration'),
'description' => [
'#type' => 'webform_message',
'#message_type' => 'warning',
'#message_message' => t("<strong>Warning</strong>: The below settings give users, permissions, and roles full access to this webform's configuration via API requests."),
],
],
];
}
/**
* Implements hook_field_info_alter().
*/
function webform_field_info_alter(&$info) {
// @todo Remove once Drupal 10.2.x is only supported.
if (floatval(\Drupal::VERSION) < 10.2) {
$info['webform']['category'] = new TranslatableMarkup("Reference");
}
}
/* ************************************************************************** */
// Devel generate info hooks.
/* ************************************************************************** */
/**
* Implements hook_devel_generate_info_alter().
*/
function webform_devel_generate_info_alter(array &$generators) {
if (!isset($generators['webform_submission'])) {
return;
}
// Use deprecated generator because the devel_generate.module changed the
// DevelGenerateBaseInterface.
//
// @see \Drupal\webform\Plugin\DevelGenerate\WebformSubmissionDevelGenerateDeprecated
// @see https://www.drupal.org/project/webform/issues/3155654
// @see https://gitlab.com/drupalspoons/devel/-/issues/324
$info = \Drupal::service('extension.list.module')->getExtensionInfo('devel_generate');
if (!empty($info['version']) && strpos($info['version'], '8.x-') === 0) {
$generators['webform_submission']['class'] = 'Drupal\webform\Plugin\DevelGenerate\WebformSubmissionDevelGenerateDeprecated';
}
}
/* ************************************************************************** */
// Element info hooks.
/* ************************************************************************** */
/**
* Implements hook_element_info_alter().
*/
function webform_element_info_alter(array &$info) {
$info['checkboxes']['#process'][] = 'webform_process_options';
$info['radios']['#process'][] = 'webform_process_options';