Repository navigation
Expand file tree
/
Copy pathoit.module
More file actions
379 lines (345 loc) · 13.1 KB
/
Copy pathoit.module
File metadata and controls
379 lines (345 loc) · 13.1 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
<?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\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\node\NodeInterface;
use Drupal\oit\Plugin\OitImageStyled;
use Drupal\webform\WebformInterface;
use Drupal\webform\WebformSubmissionStorageInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
/**
* 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');
}
/**
* Set domain on view filter on content pages.
*/
function _oit_form_set_domain($page, $field_domain) {
// 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 with field_domain_access_target_id.
$query['field_domain_access_target_id'] = $field_domain;
$query = http_build_query($query);
$response = new RedirectResponse('/admin/' . $page . '?' . $query);
// @todo (C1): Calling send() inside hook_form_alter() bypasses the Symfony
// kernel response pipeline. The proper fix is to convert this to a
// #submit handler or KernelEvents::RESPONSE subscriber. Until then,
// return immediately after send() so no further form-build code runs.
$response->send();
return;
}
}
/**
* 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]);
}
}
/**
* 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_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 = \Drupal::service('oit.hero_image')->getFileId($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"));
}
}
/**
* 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_local_tasks_alter().
*/
function oit_menu_local_tasks_alter(&$data, $route_name) {
if ($route_name == 'user.login') {
$data['tabs'][0]['user.pass']['#access'] = FALSE;
}
}
/**
* Implements hook_block_access().
*/
function oit_block_access(Block $block, $operation, AccountInterface $account) {
// Forbid access to unpublished block on published nodes.
if ($block->getPluginId() == 'block_content:b64c8b8f-40f5-492b-9cd9-4261baee0cfd') {
$node = \Drupal::routeMatch()->getParameter('node');
if ($node instanceof NodeInterface) {
if ($node->isPublished()) {
return AccessResult::forbidden();
}
}
}
if ($block->id() == 'mainnavigation') {
$node = \Drupal::routeMatch()->getParameter('node');
if (!is_string($node) && isset($node)) {
if ($node->bundle() == 'page') {
$child = $node->get('field_show_child_links')->getValue();
if (!$child[0]['value']) {
return AccessResult::forbiddenIf(TRUE)->addCacheableDependency($block);
}
}
}
}
}
/**
* Implements hook_mail_alter().
*
* Forces the From address on every outgoing message to the OIT service account
* (oitsrvcom@colorado.edu). Office 365 rejects mail whose From does not match
* an authorized sender (SendAsDenied), so this reproduces the From-rewriting
* the drupal/smtp module performed before the migration to Symfony Mailer Lite.
* Symfony Mailer Lite has no transport-level From override, so the header is
* forced here, after hook_mail() has run and before the message is sent.
*
* Reply-To defaults to oithelp@colorado.edu so replies still reach the help
* desk, but any Reply-To a module set explicitly is preserved.
*/
function oit_mail_alter(&$message) {
$from = 'Office of Information Technology <oitsrvcom@colorado.edu>';
$message['from'] = 'oitsrvcom@colorado.edu';
$message['headers']['From'] = $from;
$message['headers']['Sender'] = 'oitsrvcom@colorado.edu';
$message['headers']['Return-Path'] = 'oitsrvcom@colorado.edu';
if (empty($message['headers']['Reply-To'])) {
$message['headers']['Reply-To'] = 'oithelp@colorado.edu';
}
}
/**
* Implements hook_ENTITY_TYPE_create() for webform entities.
*/
function oit_webform_create(WebformInterface $webform) {
// Default new webforms to purge all submissions after 30 days.
if ($webform->getSetting('purge') === WebformSubmissionStorageInterface::PURGE_NONE) {
$webform->setSetting('purge', WebformSubmissionStorageInterface::PURGE_ALL);
$webform->setSetting('purge_days', 30);
}
}