-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoit.module
More file actions
1075 lines (980 loc) · 39.6 KB
/
oit.module
File metadata and controls
1075 lines (980 loc) · 39.6 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
* Module containing custom oit code.
*/
use Drupal\block\Entity\Block;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\node\NodeInterface;
use Drupal\oit\Plugin\OitImageStyled;
use Drupal\user\Entity\User;
use Symfony\Component\HttpFoundation\RedirectResponse;
/**
* Implements hook_preprocess_HOOK().
*/
function oit_preprocess_html(&$variables) {
$theme = \Drupal::service('theme.manager')->getActiveTheme()->getName();
if ($theme == 'gin') {
$title[0] = $variables['head_title']['name'] ?? '';
$title[1] = '';
if (isset($variables['head_title']['title'])) {
$title[1] = is_string($variables['head_title']['title'])?$variables['head_title']['title']:$variables['head_title']['title']->__toString();
}
$getEnv = \Drupal::service('oit.environment.icon');
$variables['head_title'] = $getEnv->getEnv() . ' ' . $title[1] . ' | ' . $title[0];
}
$user = \Drupal::currentUser();
$roles = $user->getRoles();
if (in_array('administrator', $roles)) {
$env = getenv('PANTHEON_ENVIRONMENT');
$variables['attributes']['class'][] = "env-$env";
$variables['#attached']['library'][] = 'oit/oit_env';
}
}
/**
* Implements hook_webform_access_rules_alter().
*/
function oit_webform_access_rules_alter(array &$access_rules) {
if (isset($access_rules['create'])) {
$access_rules['create']['roles'] = ['authenticated'];
}
}
/**
* Implements oit_webform_access_rules_alter().
*/
function oit_entity_create(EntityInterface $entity) {
if ($entity->getEntityType()->getBundleEntityType() == 'node_type' && $entity->getType() == 'webform') {
// Set access control set to Authenticated.
$entity->set('field_access_control_2', '475');
}
}
/**
* hook_preprocess().
*/
function oit_preprocess(&$variables, $hook) {
$theme = \Drupal::service('theme.manager')->getActiveTheme()->getName();
if ($theme == 'gin') {
$form_id = $variables['form']['#id'] ?? '';
$current_path = \Drupal::service('path.current')->getPath();
if ($current_path == '/node/add') {
$variables['#attached']['library'][] = 'oit/gingerbread';
}
if ($form_id == 'node-tutorial-form' || $form_id == 'node-tutorial-edit-form') {
$variables['#attached']['library'][] = 'oit/gin_select';
}
}
}
/**
* Implements hook_form_alter().
*/
function oit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if (array_key_exists('#webform_id', $form)) {
// Get node id current webform is on.
$node = \Drupal::routeMatch()->getParameter('node');
if ($node != NULL) {
$node_id = is_string($node) ? 0 : $node->id();
$current_user = \Drupal::currentUser();
$roles = $current_user->getRoles();
if ($node_id && in_array('administrator', $roles)) {
$webform_id = $form['#webform_id'];
$config = \Drupal::config('webform.webform.' . $webform_id);
$webform_roles = $config->get('access.create.roles');
if (!is_array($roles)) {
$roles = [];
}
$anonymous_set = FALSE;
foreach ($webform_roles as $webform_role) {
if ($webform_role == 'anonymous') {
$anonymous_set = TRUE;
}
}
if ($anonymous_set) {
$captcha_config = \Drupal::config('captcha.captcha_point.webform_add_' . $node_id)->isNew();
if ($captcha_config) {
$button_text = t('Add captcha to form');
$form['#prefix'] = Xss::filter("<ul class='no-list-style'><li class='red'><a href='/admin/config/people/captcha/captcha-points/add?destination=node/$node_id&webform_id=$node_id' class='button icon'>⬇️ $button_text</a></li></ul>");
}
else {
$button_text = t('Remove captcha from form');
$form['#prefix'] = Xss::filter("<ul class='no-list-style'><li class='red'><a href='/admin/config/people/captcha/captcha-points/webform_add_$node_id/delete?destination=node/$node_id' class='button icon'>🔥 $button_text</a></li></ul>");
}
}
}
}
}
// Show a deployment window warning on webform nodes and entities.
_oit_security_window_warning($form, $form_id);
// admin/config/people/captcha/captcha-points/add
// Set defaults if 'webform_id' is in query string.
if ($form_id == 'captcha_point_add_form') {
// Get 'webform_id' from query string.
$request = \Drupal::request();
$webform_id = $request->get('webform_id');
$webform_id = is_numeric($webform_id) ? intval($webform_id) : 0;
if ($webform_id == 0) {
return;
}
$form['label']['#default_value'] = 'webform_' . $webform_id;
$form['formId']['#default_value'] = 'webform_add_' . $webform_id;
}
if ($form_id == "taxonomy_term_service_dashboard_category_form") {
$form['actions']['submit']['#submit'][] = 'oit_servicealert_dashboard_category_add';
$form['actions']['overview']['#submit'][] = 'oit_servicealert_dashboard_category_add';
}
if ($form_id == "taxonomy_term_service_dashboard_category_delete_form") {
$form['actions']['submit']['#submit'][] = 'oit_servicealert_dashboard_category_delete';
}
if ($form_id == 'node_news_form' || $form_id == 'node_news_edit_form' ||
$form_id == 'node_service_alert_form' || $form_id == 'node_service_alert_edit_form'
) {
$form['actions']['submit']['#suffix'] = '<div class="loading-message">' . t('The page is loading, do not submit. Reload if necessary.') . '</div>';
$form['#attached']['library'][] = 'oit/loading';
}
// GP blank body for editing news, tutorial, or page.
if ($form_id == 'node_news_edit_form' || $form_id == 'node_tutorial_edit_form' || $form_id == 'node_page_edit_form') {
// Add validation function.
$form['#validate'][] = 'oit_news_gp_check';
}
// Set to oit domain on oit site when viewing the content.
$domain = \Drupal::service('oit.domain')->getDomain();
if ($domain == 'oit') {
oit_set_domain_defaults($form, 'oit', 'oit_colorado_edu');
}
if ($form_id == "views_form_content_page_1") {
if ($domain == 'oit') {
// Get user roles.
$roles = \Drupal::currentUser()->getRoles();
if (in_array('oda_super_editors', $roles) || in_array('oda_editors', $roles)) {
$message = t('You are logged into the OIT site and cannot edit content. Please log into the Data site to edit content.');
\Drupal::messenger()->addMessage($message, 'warning');
$form['#access'] = FALSE;
}
// Get query string.
$query = \Drupal::request()->query->all();
$domain_set = FALSE;
foreach ($query as $key => $value) {
if ($key == 'field_domain_access_target_id') {
$domain_set = TRUE;
}
}
if (!$domain_set) {
// Redirect to query string "field_domain_access_target_id=oit_colorado_edu".
$query['field_domain_access_target_id'] = 'oit_colorado_edu';
$query = http_build_query($query);
$response = new RedirectResponse('/admin/content?' . $query);
$response->send();
}
}
}
// Add spacemonkey to search.
if ($form_id == 'search_form') {
// Get query string.
$query = \Drupal::request()->query->all();
$search_key = isset($query['keys']) ? Xss::filter(strtolower($query['keys'])) : 0;
$space = [
'space monkey',
'space+monkey',
'spacemonkey',
];
if ($search_key == 'space monkey' || $search_key == 'space+monkey' || $search_key == 'spacemonkey') {
$form['#attached']['library'][] = 'oit/spacemonkey';
}
}
switch ($form_id) {
case "node_service_alert_form":
case "node_service_alert_edit_form":
$config = \Drupal::config('sympa.settings');
$prod_email = $config->get('sympa_email_prod');
if ($prod_email) {
$form['#attached']['library'][] = 'oit/gin_sa';
}
// Group for page sub-type extra fields.
$form['oit_sa_extras'] = [
'#title' => t('Service Alert Extras'),
'#type' => 'details',
'#group' => 'advanced',
'#open' => 1,
'#weight' => 100,
];
$form['field_access_control_2']['#group'] = 'oit_sa_extras';
$form['field_sympa_send']['#group'] = 'options';
// Death to comments.
$form['comment_node_service_alert']['#access'] = FALSE;
// Fill in empty body with template.
if ($form['body']['widget'][0]['#default_value'] == NULL) {
$form['body']['widget'][0]['#default_value'] = '<h2>Impact</h2><p></p>
<h2>Scope</h2><p></p>
<h2>Affected Services</h2><p></p>
<h2>Affected Buildings</h2><p></p>
<h2>For More Information</h2><p></p>
<h2>Additional Information from Vendor</h2><p></p>
<h2>Additional Information from UIS</h2><p></p>';
}
break;
case "search_block_form":
// Set search placeholder on oit.
$domain = \Drupal::service('oit.domain')->getDomain();
if ($domain == 'oit') {
$form['keys']['#attributes']['placeholder'] = t('Search OIT');
}
$form['keys']['#attributes']['autocomplete'] = 'off';
break;
case "node_page_form":
case "node_page_edit_form":
// Group for page sub-type extra fields.
$form['page_extras'] = [
'#title' => t('Page Extras'),
'#type' => 'details',
'#group' => 'advanced',
'#open' => 1,
'#weight' => 100,
];
$form['field_oit_category']['#group'] = 'page_extras';
$form['field_access_control_2']['#group'] = 'page_extras';
$form['field_show_child_links']['#group'] = 'page_extras';
$form['upload']['#group'] = 'page_extras';
// Private files.
$form['protected_downloads'] = [
'#title' => t('Protected Downloads'),
'#type' => 'details',
'#group' => 'advanced',
'#open' => 0,
'#weight' => 101,
];
$form['field_dl_facstaff']['#group'] = 'protected_downloads';
$form['field_dl_student']['#group'] = 'protected_downloads';
$form['field_dl_authenticated']['#group'] = 'protected_downloads';
// Group for service type.
$form['type_service'] = [
'#title' => t('Service'),
'#type' => 'details',
'#group' => 'advanced',
'#open' => 0,
'#weight' => 102,
];
$form['taxonomy_vocabulary_11']['#group'] = 'type_service';
$form['field_service_main_page']['#group'] = 'type_service';
$form['field_services_related']['#group'] = 'type_service';
$form['field_tut_comp_type_d7']['#group'] = 'type_service';
$form['field_software_download_link']['#group'] = 'type_service';
$request = \Drupal::request();
$request_type = $request->get("type");
if (isset($request_type)) {
// Show/hide fields that apply/don't apply to the service type.
if ($request_type == "service") {
$form['field_faq']['#access'] = FALSE;
$form['field_faq_section_title']['#access'] = FALSE;
$form['type_service']['#open'] = 1;
$form['body']['widget'][0]['#default_value'] = '<h2>Features</h2><p>Features here.</p><h2>Related Policies</h2><p>Policies here</p><h2>Benefits</h2><p>Benefits here</p><h2>Cost</h2><p>Cost here</p><h2>Who can get it</h2><p>Who can get it here</p><h2>How to get it</h2><p>how to get it here</p><h2>Related Projects</h2><p>related projects here</p>';
$form['field_oit_category']['widget']['#default_value'][] = 1039;
$form['taxonomy_vocabulary_11']['widget']['#required'] = TRUE;
$form['#title'] = t('Create Service Page');
}
if ($request_type == "accessibility") {
$form['field_oit_category']['widget']['#default_value'][] = 847;
$form['type_service']['#access'] = FALSE;
}
}
$form['#attached']['library'][] = 'oit/oit_clipboard';
$form['#attached']['library'][] = 'webform/webform.element.select2';
$form['#attached']['library'][] = 'oit/oit_node_page_form';
$advanced = sprintf(
'<details><summary>%s</summary>
<ul><li><a href="%s" class="edit-button use-ajax" data-dialog-type="dialog" data-dialog-renderer="off_canvas" data-dialog-options="{"width":400}">%s</a></li><li><a href="%s" target="_blank">%s</a></li><li><a href="%s" target="_blank">%s</a></li></ul>
<h3>%s</h3>
<p>%s</p>
<h3>%s</h3>
<p>%s</p>
<p>%s</p>
<h3>%s</h3>
<p>%s</p>
<h3>%s</h3>
<p>%s</p>
</details>',
t('OIT advanced html'),
'/admin/config/content/shortcode_svg/svg_list',
t('Icons shortcode panel'),
'https://curly-umbrella-61336c58.pages.github.io',
t('Style Guide'),
'/admin/config/development/asset-injector/js',
t('Custom js/css (admin only)'),
t('Flexbox Codez'),
'<button type="button" class="copy-icon" data-clipboard="flexBox">Copy flexbox</button>',
t('Details'),
'<button type="button" class="copy-icon" data-clipboard="details">Copy details element</button>',
'<button type="button" class="copy-icon" data-clipboard="details-no-deets">Copy details element with class to hide show/hide links</button>',
t('Columns'),
'<button type="button" class="copy-icon" data-clipboard="text-cols--3">Copy Columns class</button>',
t('Shortcode Block embed'),
'<button type="button" class="copy-icon" data-clipboard="shortcode-block">Copy block shortcode embed</button>',
);
$form['oit_advanced'] = [
'#markup' => $advanced,
'#weight' => 100,
'#allowed_tags' => [
'button',
'details',
'summary',
'ul',
'li',
'a',
'p',
'h3',
],
];
break;
case "node_news_form":
case "node_news_edit_form":
$config = \Drupal::config('sympa.settings');
$prod_email = $config->get('sympa_email_prod');
if ($prod_email) {
$form['#attached']['library'][] = 'oit/gin_sa';
}
// Group for page sub-type extra fields.
$form['oit_news_extras'] = [
'#title' => t('News Extras'),
'#type' => 'details',
'#group' => 'advanced',
'#open' => 1,
'#weight' => 100,
];
$form['field_oit_category']['#group'] = 'oit_news_extras';
$form['field_access_control_2']['#group'] = 'oit_news_extras';
$form['taxonomy_vocabulary_11']['#group'] = 'oit_news_extras';
$form['field_sympa_send']['#group'] = 'options';
$form['field_oit_page_file_attatchment']['#group'] = 'oit_news_extras';
$form['field_oit_news_front_image']['#group'] = 'oit_news_extras';
$form['field_oit_page_related_content']['#group'] = 'oit_news_extras';
$form['#attached']['library'][] = 'webform/webform.element.select2';
$form['#attached']['library'][] = 'oit/oit_node_news_form';
$form['#validate'][] = 'oit_news_types_categories';
// Add submit handler.
$form['actions']['submit']['#submit'][] = 'news_node_form_submit';
break;
case "user_login_form":
// Remove any messages so they don't show up after login.
\Drupal::messenger()->deleteAll();
$config = \Drupal::config('oit.settings');
$show_login_form = $config->get('show_login_form');
$form['login_words']['#markup'] = '<div class="login_text"></div>';
$form['login_words']['#weight'] = -10;
if (!$show_login_form) {
// Cache is breaking the redirect, so kill it.
\Drupal::service('page_cache_kill_switch')->trigger();
$dest_get = \Drupal::request()->get('dest') != null ? Xss::filter(\Drupal::request()->get('dest')) : '';
$destination_get = \Drupal::request()->get('destination') != null ? Xss::filter(\Drupal::request()->get('destination')) : '';
$destination = "";
if (!empty($dest_get)) {
$destination = '?destination=' . preg_replace('/https:\/\/[^\/]+/', '', $dest_get);
}
if (!empty($destination_get)) {
$destination = '?destination=' . preg_replace('/https:\/\/[^\/]+/', '', $destination_get);
}
// Drupal 10 add log message with $destination.
\Drupal::logger('oit')->notice('User login form redirecting to saml_login with destination: @destination', ['@destination' => $destination]);
$response = new RedirectResponse('/saml_login' . $destination, 301);
$response->send();
unset($form['name']);
unset($form['pass']);
unset($form['actions']);
$login_text = t('Click below to login');
$form['login_words']['#markup'] = "<div class='login_text'><p>$login_text</p></div>";
$form['simplesamlphp_auth_login_link']['#attributes']['class'][] = 'button';
$form['simplesamlphp_auth_login_link']['#attributes']['class'][] = 'ext';
$form['#attached']['library'][] = 'oit/gsap';
return;
}
break;
// Imported from zap_initialize.
case 'node_webform_form':
case 'node_webform_edit_form':
$form['oit_webform_extras'] = [
'#title' => t('Webform Extras'),
'#type' => 'details',
'#group' => 'advanced',
'#open' => 1,
'#weight' => 100,
];
$form['field_access_control_2']['#group'] = 'oit_webform_extras';
$form['field_oit_category']['#group'] = 'oit_webform_extras';
break;
case 'node_tutorial_form':
case 'node_tutorial_edit_form':
$form['oit_page_extras'] = [
'#title' => t('Tutorial Extras'),
'#type' => 'details',
'#group' => 'advanced',
'#open' => 1,
'#weight' => 100,
];
$form['field_access_control_2']['#group'] = 'oit_page_extras';
$form['field_oit_category']['#group'] = 'oit_page_extras';
$form['taxonomy_vocabulary_11']['#group'] = 'oit_page_extras';
$form['field_tut_comp_type_d7']['#group'] = 'oit_page_extras';
$form['upload']['#group'] = 'oit_page_extras';
break;
}
}
/**
* Set domain on content to match site domain
*/
function _oit_set_oit_domain(&$form, FormStateInterface $form_state) {
_oit_set_domain_submit($form, $form_state, 'oit', 'oit_colorado_edu');
}
/**
* Shared: Set domain defaults on form for the given domain.
*
* Used by oit and oda modules to auto-fill domain access/source fields.
*
* @param array $form
* The form array.
* @param string $domain_name
* The domain name (e.g. 'oit', 'oda').
* @param string $domain_id
* The domain ID (e.g. 'oit_colorado_edu', 'oda_colorado_edu').
*/
function oit_set_domain_defaults(array &$form, $domain_name, $domain_id) {
if (isset($form['field_domain_access'])) {
$env = getenv('PANTHEON_ENVIRONMENT');
// Add on dev, test, and live.
if ($env != 'local' && strpos($env, 'md-') !== 0) {
// Auto fill for admins as they are the only ones that can see the field
// to set it.
if (in_array('administrator', \Drupal::currentUser()->getRoles())) {
$form['field_domain_access']['widget']['#default_value'][] = $domain_id;
$form['field_domain_source']['widget']['#default_value'][] = $domain_id;
}
// Force access and source if not set.
array_unshift($form['actions']['submit']['#submit'], '_' . $domain_name . '_set_' . $domain_name . '_domain');
} else {
// Disable source locally.
$form['field_domain_source']['widget']['#default_value'] = [];
$form['field_domain_source']['widget']['#default_value'][] = '_none';
}
}
}
/**
* Shared: Submit handler to force domain access/source values.
*
* @param array $form
* The form array.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
* @param string $domain_name
* The domain name (e.g. 'oit', 'oda').
* @param string $domain_id
* The domain ID (e.g. 'oit_colorado_edu', 'oda_colorado_edu').
*/
function _oit_set_domain_submit(array &$form, FormStateInterface $form_state, $domain_name, $domain_id) {
$domain = \Drupal::service('oit.domain')->getDomain();
if ($domain == $domain_name && (!$form_state->getValues()['field_domain_access'] || !$form_state->getValues()['field_domain_source'])) {
$form_state->setValue('field_domain_access', ['target_id' => $domain_id]);
$form_state->setValue('field_domain_source', ['target_id' => $domain_id]);
}
}
/**
* News node form submit handler to un-promote other news nodes when a news
* node is promoted.
*/
function news_node_form_submit(&$form, FormStateInterface $form_state) {
if ($form_state->getValues()['promote']['value']) {
$node = $form_state->getFormObject()->getEntity();
$node_id = $node->id();
$node_storage = \Drupal::entityTypeManager()->getStorage('node');
$query = $node_storage->getQuery();
$query->condition('type', 'news');
$query->condition('promote', 1);
$query->condition('nid', $node_id, '!=');
$query->accessCheck(TRUE);
$query->sort('created', 'DESC');
$nids = $query->execute();
foreach ($nids as $nid) {
$node = $node_storage->load($nid);
$node->set('promote', 0);
$node->set('field_sympa_send', 0);
$node->save();
}
}
}
/**
* Service alert dashboard category add handler.
*/
function oit_servicealert_dashboard_category_add($form, FormStateInterface $form_state) {
$name = Xss::filter($form_state->getValue('name')[0]['value']);
$dashboard_state = \Drupal::state()->get('oit_dashboard_add');
if ($dashboard_state) {
$dashboard_state = json_decode($dashboard_state, TRUE);
}
$dashboard_state[]= $name;
// Add term_name to a drupal state for future GH action processing.
\Drupal::state()->set('oit_dashboard_add', json_encode($dashboard_state));
}
/**
* Service alert dashboard category delete handler.
*/
function oit_servicealert_dashboard_category_delete($form, FormStateInterface $form_state) {
$term = $form_state->getFormObject()->getEntity();
$term_name = $term->getName();
$dashboard_state = \Drupal::state()->get('oit_dashboard_delete');
if ($dashboard_state) {
$dashboard_state = json_decode($dashboard_state, TRUE);
}
$dashboard_state[]= $term_name;
// Add term_name to a drupal state for future GH action processing.
\Drupal::state()->set('oit_dashboard_delete', json_encode($dashboard_state));
}
/**
* Node form validate for GP blank body.
*/
function oit_news_gp_check(&$form, FormStateInterface $form_state) {
$body = $form_state->getValue('body');
$body_value = $body[0]['value'];
$node = $form_state->getFormObject()->getEntity();
if ($node->isNew()) {
return;
}
else {
$old_body = $node->get('body')->value;
}
if (trim($body_value) == '' && !empty(trim($old_body))) {
$form_state->setErrorByName('body', t('You went from having body content to none. As a hall monitor, I will not allow such tomfoolery.'));
}
}
/**
* News form validate.
*/
function oit_news_types_categories(&$form, FormStateInterface $form_state) {
$categories = $form_state->getValue('field_oit_category');
$parent_cat = [];
if (isset($categories)) {
foreach ($categories as $category) {
$term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($category['target_id']);
$parent_term_id = $term->parent->target_id;
$parent_cat[] = $parent_term_id;
}
}
$cat_type = '';
if (!in_array('885', $parent_cat)) {
$cat_type = 'Service';
}
if (!in_array('889', $parent_cat)) {
if ($cat_type == 'Service') {
$cat_type .= ' & ';
}
$cat_type .= 'News';
}
if (!empty($cat_type)) {
$form_state->setErrorByName('field_oit_category', t('Please select a @type category', ['@type' => $cat_type]));
}
}
/**
* Implements hook_token_info().
*/
function oit_token_info() {
$type = [
'name' => t('OIT'),
'description' => t('Custom OIT tokens.'),
];
$node['tweet_pic'] = [
'name' => t("Twitter Picture"),
'description' => t('Sets the twitter picture for news/service alerts.'),
];
$node['og_image'] = [
'name' => t("OG Image"),
'description' => t('Sets the Open Graph image for news/service alerts at 1200x630 for LinkedIn and other platforms.'),
];
$node['sa_status'] = [
'name' => t("Service Alert Status"),
'description' => t('Set status of the service alert node type.'),
];
$node['sa_title'] = [
'name' => t("Service Alert Title"),
'description' => t('Display the service alert title field.'),
];
$node['who_i_is'] = [
'name' => t("Who I Is"),
'description' => t('Displays users name.'),
];
return [
'types' => ['oittoken' => $type],
'tokens' => ['oittoken' => $node],
];
}
/**
* Implements hook_tokens().
*/
function oit_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
$replacements = [];
if ($type == 'oittoken') {
foreach ($tokens as $name => $original) {
// Find the desired token by name.
switch ($name) {
case 'who_i_is':
$user = User::load(\Drupal::currentUser()->id());
$user_name = '';
if (!$user->isAnonymous()) {
if ($user->get('field_user_name') != NULL) {
$user_name = $user->get('field_user_name')->getValue();
if (array_key_exists(0, $user_name)) {
$user_name = $user_name[0]['value'];
}
}
}
$replacements[$original] = $user_name;
break;
case 'tweet_pic':
if (!empty($data['node'])) {
$replacements[$original] = '';
if ($data['node']->getType() == 'news') {
$sa_image = oit_get_hero_image_fid($data['node']);
if ($sa_image) {
$sa_image_style = new OitImageStyled($sa_image, 'large', '', \Drupal::entityTypeManager());
$sa_image_styled = $sa_image_style->getImageUrl();
$replacements[$original] = $sa_image_styled;
}
}
if ($data['node']->getType() == 'service_alert' && count($data['node']->get('field_service_alert_dash_cat')->getValue()) !== 0) {
$set_dashboard = $data['node']->get('field_service_alert_dash_cat')->getValue()[0]['value'];
$taxonomy = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($set_dashboard);
$tax_image = $taxonomy->get('field_twitter_image');
$image_id = $tax_image->getValue()[0]['target_id'] ?? NULL;
$image_url = '';
if ($image_id) {
$image_style = new OitImageStyled($image_id, 'large', '', \Drupal::entityTypeManager());
$image_url = $image_style->getImageUrl();
}
$host = \Drupal::request()->getSchemeAndHttpHost();
if ($image_url) {
$twitter_image = $image_url;
}
else {
$twitter_image = "$host/sites/default/files/sa_images/sa_other.png";
}
$replacements[$original] = $twitter_image;
}
}
break;
case 'og_image':
if (!empty($data['node'])) {
$replacements[$original] = '';
if ($data['node']->getType() == 'news') {
$og_image_id = oit_get_hero_image_fid($data['node']);
$og_image_url = '';
if ($og_image_id) {
$og_image_style = new OitImageStyled($og_image_id, 'social_share', '', \Drupal::entityTypeManager());
$og_image_url = $og_image_style->getImageUrl();
}
$replacements[$original] = $og_image_url;
}
if ($data['node']->getType() == 'service_alert' && count($data['node']->get('field_service_alert_dash_cat')->getValue()) !== 0) {
$set_dashboard = $data['node']->get('field_service_alert_dash_cat')->getValue()[0]['target_id'];
$taxonomy = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($set_dashboard);
$image_id = NULL;
if ($taxonomy !== NULL) {
$tax_image = $taxonomy->get('field_twitter_image');
$tax_image_values = $tax_image->getValue();
$image_id = !empty($tax_image_values) ? ($tax_image_values[0]['target_id'] ?? NULL) : NULL;
}
$image_url = '';
if ($image_id) {
$image_style = new OitImageStyled($image_id, 'social_share', '', \Drupal::entityTypeManager());
$image_url = $image_style->getImageUrl();
}
$host = \Drupal::request()->getSchemeAndHttpHost();
$replacements[$original] = $image_url ?: "$host/sites/default/files/sa_images/sa_other.png";
}
}
break;
case 'sa_status':
if (!empty($data['node'])) {
$sa_status = $data['node']->get('field_service_alert_status')->getValue() !== NULL ? $data['node']->get('field_service_alert_status')->getValue() : '';
$replacements[$original] = $sa_status[0]['value'];
}
break;
case 'sa_title':
if (!empty($data['node'])) {
$sa_title = $data['node']->get('field_service_alert_title')->getValue() !== NULL ? $data['node']->get('field_service_alert_title')->getValue() : '';
$replacements[$original] = $sa_title[0]['value'];
}
break;
}
}
}
return $replacements;
}
/**
* Implements hook_page_attachments_alter().
*/
function oit_page_attachments_alter(array &$attachments) {
$node = \Drupal::routeMatch()->getParameter('node');
if (!is_null($node)) {
$id = is_string($node) ? $node : $node->id();
if ($id == 262) {
$attachments['#attached']['library'][] = 'oit/listjs';
$attachments['#attached']['library'][] = 'oit/downloads_search';
}
if (!is_string($node)) {
if ($node->getType() == 'tutorial') {
$attachments['#attached']['library'][] = 'dingo/tutorial';
}
}
}
$domain = \Drupal::service('oit.domain')->getDomain();
if ($domain == 'oit') {
$google_site_verify = [
'#tag' => 'meta',
'#attributes' => [
'name' => 'google-site-verification',
'content' => '7W5qMq8L0e0Ar6WDHCTrcC5IDXMBDj1Cm1DL0evR32o',
],
];
$attachments['#attached']['html_head'][] = [$google_site_verify, 'google-site-verification'];
}
}
/**
* Implements hook_entity_view().
*/
function oit_entity_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
if ($entity->getEntityType()->getBundleEntityType() == 'node_type' && $entity->getType() == 'webform') {
if ($webform_id = $entity->get('webform')->getValue()) {
$webform_id = $webform_id[0]['target_id'];
$webform = \Drupal::entityTypeManager()->getStorage('webform')->load($webform_id);
// Report error if webform node is pointing to a deleted webform.
if ($webform == NULL && $webform_id != 0) {
\Drupal::service('oit.teamsalert')->sendMessage('Webform no longer exists but is set on node: ' . $entity->id(), ['live']);
\Drupal::logger('oit')->error('Webform no longer exists but is set on node: ' . $entity->id());
}
}
}
}
/**
* Alter the off-canvas menu tree.
*
* @param array $rendered_tree
* The built menu tree to be altered. This is provided as a render array.
*/
function oit_responsive_menu_off_canvas_tree_alter(array &$rendered_tree) {
$feedback = "<a href='mailto:[email protected]' title='email feedback' class='button'>Feedback</a>";
$close = "<a href='#mm-0' class='button'>close menu</a>";
$twitter = oit_social_links(t('twitter'), 'http://www.x.com/CUBoulderOIT');
$facebook = oit_social_links(t('facebook'), 'https://www.facebook.com/CUBoulderOIT');
$youtube = oit_social_links(t('youtube'), 'https://www.youtube.com/CUBoulderOIT');
$instagram = oit_social_links(t('instagram'), 'https://www.instagram.com/cuboulderoit');
$domain = \Drupal::service('oit.domain')->getDomain();
if ($domain == 'oit') {
$rendered_tree['#suffix'] = "<div class='feedback-social'><ul class='feedback-social-inner'><li>$facebook</li><li>$twitter</li><li>$youtube</li><li>$instagram</li><li>$feedback</li><li>$close</li></ul></div>";
}
}
/**
* Create social link.
*/
function oit_social_links($title, $url) {
return "<a class='social-icon $title' title='$title' href='$url'><img alt='' src='/themes/custom/dingo/images/icons/$title.svg'></a>";
}
/**
* Implements hook_entity_presave().
*/
function oit_entity_presave(EntityInterface $entity) {
if (method_exists($entity, 'getType')) {
if ($entity->getType() == 'news') {
oit_news_update($entity);
}
if ($entity->getType() == 'service_alert') {
oit_servicealert_update($entity);
}
if ($entity->getEntityType()->id() == 'node') {
// Allows for clearing of blocks based on node type.
$tags = ['node_type:' . $entity->getType()];
Cache::invalidateTags($tags);
}
}
// Set image alt.
if (method_exists($entity, 'bundle')) {
if ($entity->bundle() == 'pictures') {
$entity->field_finished_picture->alt = $entity->get('title')->value;
}
}
}
/**
* Implements hook_entity_delete().
*/
function oit_entity_delete(EntityInterface $entity) {
if (method_exists($entity, 'getType') && $entity->getEntityTypeId() == 'node') {
// Allows for clearing of blocks based on node type.
$tags = ['node_type:' . $entity->getType()];
Cache::invalidateTags($tags);
}
}
/**
* Set twitter meta image and set unpublish date.
*/
function oit_news_update($entity) {
$meta_info = json_decode($entity->get('field_meta_tags')->getValue()[0]['value'], TRUE) ?? [];
$is_temp_image = (isset($meta_info['og_image']) && strpos($meta_info['og_image'], '/system/temporary') !== FALSE)
|| (isset($meta_info['twitter_cards_image']) && strpos($meta_info['twitter_cards_image'], '/system/temporary') !== FALSE);
$og_using_large = isset($meta_info['og_image']) && strpos($meta_info['og_image'], '/styles/large/') !== FALSE;
if (!isset($meta_info['twitter_cards_image']) || !isset($meta_info['og_image']) || $is_temp_image || $og_using_large) {
$image_id = oit_get_hero_image_fid($entity);
if (!empty($image_id)) {
$twitter_style = new OitImageStyled($image_id, 'large', '', \Drupal::entityTypeManager());
$twitter_styled = $twitter_style->getImageUrl();
if ($twitter_styled && (!isset($meta_info['twitter_cards_image']) || $meta_info['twitter_cards_image'] != NULL)) {
$meta_info['twitter_cards_image'] = $twitter_styled;
}
$og_style = new OitImageStyled($image_id, 'social_share', '', \Drupal::entityTypeManager());
$og_styled = $og_style->getImageUrl();
if ($og_styled && (!isset($meta_info['og_image']) || $meta_info['og_image'] != NULL)) {
$meta_info['og_image'] = $og_styled;
}
$meta_serial = json_encode($meta_info);
$entity->set('field_meta_tags', $meta_serial);
}
}
if ($entity->getType() == 'news' && $entity->get('field_news_archive')->getValue()[0]['value'] == 2) {
$entity->set('unpublish_on', strtotime("100 days"));
}
}
/**
* Get the file entity ID from the news hero media field.
*/
function oit_get_hero_image_fid($node) {
if ($node->get('field_media_hero_image')->isEmpty()) {
return NULL;
}
$media_id = $node->get('field_media_hero_image')->getValue()[0]['target_id'];
$media = \Drupal::entityTypeManager()->getStorage('media')->load($media_id);
if (!$media || $media->get('field_media_image_2')->isEmpty()) {
return NULL;
}
return $media->get('field_media_image_2')->getValue()[0]['target_id'];
}
/**
* Set twitter image and dashboard ordering.
*/
function oit_servicealert_update($entity) {
$meta_info = json_decode($entity->get('field_meta_tags')->getValue()[0]['value']) ?? [];
if ((!isset($meta_info->twitter_cards_image) || !isset($meta_info->og_image)) && $entity->get('field_service_alert_dash_cat')) {
if (is_object($meta_info)) {
$meta_info->twitter_cards_type = "summary";
}
else {
$meta_info['twitter_cards_type'] = "summary";
}
$set_dashboard = $entity->get('field_service_alert_dash_cat')->getValue()[0]['target_id'];
$taxonomy = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($set_dashboard);
$tax_image = $taxonomy->get('field_twitter_image');
$image_id = $tax_image->getValue()[0]['target_id'] ?? NULL;
$image_url = '';
if ($image_id) {
$image_style = new OitImageStyled($image_id, 'large', '', \Drupal::entityTypeManager());
$image_url = $image_style->getImageUrl();
}
$host = \Drupal::request()->getSchemeAndHttpHost();
if ($image_url) {
$social_image = $image_url;
}
else {
$social_image = "$host/sites/default/files/sa_images/sa_other.png";
}
$meta = [];
if (!isset($meta_info->twitter_cards_image) || $meta_info->twitter_cards_image != NULL) {
$meta['twitter_cards_image'] = $social_image;
}
else {
$meta['twitter_cards_image'] = $meta_info->twitter_cards_image;
}
if (!isset($meta_info->og_image) || $meta_info->og_image != NULL) {
$meta['og_image'] = $social_image;
}
else {
$meta['og_image'] = $meta_info->og_image;
}
$meta_serial = serialize($meta);
$entity->set('field_meta_tags', $meta_serial);
}
}
/**
* Implements hook_menu().
*/
function oit_menu() {
$items['front'] = [
'title' => 'Office of Information Technology',
'access arguments' => ['access content'],
];