-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlehigh_islandora.module
766 lines (694 loc) · 23.1 KB
/
lehigh_islandora.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
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
<?php
/**
* @file
* Contains lehigh_islandora.module.
*/
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\islandora\Event\StompHeaderEvent;
use Drupal\islandora\Event\StompHeaderEventException;
use Drupal\node\Entity\Node;
use Drupal\taxonomy\Entity\Term;
use Drupal\user\Entity\User;
use Drupal\views\Plugin\views\query\QueryPluginBase;
use Drupal\views\ViewExecutable;
use Drupal\views\Views;
use Stomp\Exception\StompException;
use Stomp\Transport\Message;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Serialization\Json;
use GuzzleHttp\Exception\RequestException;
use Drupal\lehigh_islandora\Event\MediaInsertEvent;
/**
* Implements hook_help().
*/
function lehigh_islandora_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the lehigh_islandora module.
case 'help.page.lehigh_islandora':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('My Awesome Module') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_xmlsitemap_link_alter().
*/
function lehigh_islandora_xmlsitemap_link_alter(array &$link, array $context) {
$uri = $link['loc'];
if (substr($uri, 0, 6) == '/node/') {
$components = explode('/', $uri);
$nid = array_pop($components);
$node = Node::load($nid);
// Do not add pages to the sitemap.
if ($node
&& $node->hasField('field_model')
&& !$node->field_model->isEmpty()
&& $node->field_model->entity->hasField('field_external_uri')
&& !$node->field_model->entity->field_external_uri->isEmpty()
&& $node->field_model->entity->field_external_uri->uri == 'http://id.loc.gov/ontologies/bibframe/part') {
$link['status'] = 0;
$link['access'] = 0;
}
}
}
/**
* Implements hook_pathauto_pattern_alter().
*/
function lehigh_islandora_pathauto_pattern_alter($pattern, array $context) {
if (!empty($context['data']['node'])) {
$node = $context['data']['node'];
// Do not create aliases for pages.
if ($node
&& $node->hasField('field_model')
&& !$node->field_model->isEmpty()
&& $node->field_model->entity->hasField('field_external_uri')
&& !$node->field_model->entity->field_external_uri->isEmpty()
&& $node->field_model->entity->field_external_uri->uri == 'http://id.loc.gov/ontologies/bibframe/part') {
$pattern->setPattern('');
}
}
}
/**
* Implements hook_pathauto_alias_alter().
*/
function lehigh_islandora_pathauto_alias_alter(&$alias, array &$context) {
if (!empty($context['data']['node'])) {
$node = $context['data']['node'];
if (lehigh_site_support_identify_collection($node, TRUE)) {
$context['source'] = '/browse-items/' . $node->id();
}
}
}
/**
* Implements hook_theme_registry_alter().
*/
function lehigh_islandora_theme_registry_alter(array &$theme_registry) {
if (empty($theme_registry['media'])) {
return;
}
$module_path = \Drupal::service('extension.list.module')->getPath('lehigh_islandora');
$theme_registry['media__image__rest_url'] = $theme_registry['media'];
$theme_registry['media__image__rest_url']['template'] = 'media--image--rest-url';
$theme_registry['media__image__rest_url']['path'] = $module_path . '/templates';
$theme_registry['node_edit_form']['template'] = 'node-edit-form';
$theme_registry['node_edit_form']['path'] = $module_path . '/templates';
}
/**
* Check if a node is embargoed.
*/
function lehigh_islandora_node_is_embargoed($node) {
if (!is_object($node) ||
!$node->hasField('field_edtf_date_embargo') ||
$node->field_edtf_date_embargo->isEmpty()) {
return FALSE;
}
foreach ($node->field_edtf_date_embargo as $embargo) {
$d = new DrupalDateTime($embargo->value, 'UTC');
if ($d->getTimestamp() > time()) {
return $d;
}
}
return FALSE;
}
/**
* Implements hook_metatags_attachments_alter().
*/
function lehigh_islandora_metatags_attachments_alter(array &$metatag_attachments) {
// See if the node, or one of the parents in the hierarchy,
// have google scholar metatags set to be hidden.
$node = \Drupal::routeMatch()->getParameter('node');
if ($node && is_object($node) && $node->bundle() === 'islandora_object') {
$depth = 0;
if (lehigh_islandora_node_is_embargoed($node)) {
foreach ($metatag_attachments['#attached']['html_head'] as $id => $attachment) {
if ($attachment[1] == 'citation_pdf_url') {
unset($metatag_attachments['#attached']['html_head'][$id]);
}
}
}
while ($depth < 20 && $node->hasField('field_hide_gscholar_metatags')) {
if (!$node->field_hide_gscholar_metatags->isEmpty()
&& $node->field_hide_gscholar_metatags->value) {
// Metatags should be hidden for google scholar.
foreach ($metatag_attachments['#attached']['html_head'] as $id => $attachment) {
if (substr($attachment[1], 0, 9) == 'citation_') {
unset($metatag_attachments['#attached']['html_head'][$id]);
}
}
break;
}
// Avoid circular references causing infinite loops.
++$depth;
// @todo use recursion for multi-valued field_member_of
// right now that isn't a thing here at Lehigh.
if ($node->hasField('field_member_of') && !$node->field_member_of->isEmpty()) {
$node = $node->field_member_of->entity;
}
else {
break;
}
}
}
}
/**
* See if hOCR should be hidden on display.
*/
function lehigh_islandora_hide_hocr($node) {
$depth = 0;
while ($depth < 20 && $node->hasField('field_hide_hocr')) {
if (!$node->field_hide_hocr->isEmpty()
&& $node->field_hide_hocr->value) {
return TRUE;
}
// Avoid circular references causing infinite loops.
++$depth;
// @todo use recursion for multi-valued field_member_of
// right now that isn't a thing here at Lehigh.
if ($node->hasField('field_member_of') && !$node->field_member_of->isEmpty()) {
$node = $node->field_member_of->entity;
}
else {
break;
}
}
return FALSE;
}
/**
* Implements hook_ENTITY_TYPE_insert().
*/
function lehigh_islandora_media_insert(EntityInterface $media) {
$dispatcher = \Drupal::service('event_dispatcher');
$event = new MediaInsertEvent($media);
$dispatcher->dispatch($event, MediaInsertEvent::NAME);
if ($media->bundle() == 'image' &&
!$media->field_media_use->isEmpty() &&
!$media->field_media_of->isEmpty() &&
!is_null($media->field_media_of->entity) &&
!is_null($media->field_media_of->entity) &&
$media->field_media_use->entity->label() == 'Thumbnail Image') {
$node = Node::load($media->field_media_of->entity->id());
if ($node) {
$node->set('field_thumbnail', $media->id());
$node->save();
}
}
// Invalidate node cache on media CUD.
elseif (isset($media->field_media_of) &&
!$media->field_media_of->isEmpty() &&
!is_null($media->field_media_of->entity)) {
lehigh_islandora_clear_disk_cache($media->field_media_of->entity);
}
}
/**
* Implements hook_ENTITY_TYPE_update().
*/
function lehigh_islandora_media_update(EntityInterface $media) {
if (isset($media->field_media_of) &&
!$media->field_media_of->isEmpty() &&
!is_null($media->field_media_of->entity)) {
lehigh_islandora_clear_disk_cache($media->field_media_of->entity);
}
}
/**
* Implements hook_ENTITY_TYPE_delete().
*/
function lehigh_islandora_media_delete(EntityInterface $media) {
if ($media->bundle() == 'image' &&
!$media->field_media_use->isEmpty() &&
!$media->field_media_of->isEmpty() &&
!is_null($media->field_media_of->entity) &&
!is_null($media->field_media_of->entity) &&
$media->field_media_use->entity->label() == 'Thumbnail Image') {
$node = Node::load($media->field_media_of->entity->id());
if ($node) {
$node->set('field_thumbnail', NULL);
$node->save();
}
}
elseif (isset($media->field_media_of) &&
!$media->field_media_of->isEmpty() &&
!is_null($media->field_media_of->entity)) {
lehigh_islandora_clear_disk_cache($media->field_media_of->entity);
}
}
/**
* Implements hook_ENTITY_TYPE_presave().
*/
function lehigh_islandora_node_presave(EntityInterface $entity) {
// If we're saving a compound object
// see if it needs a thumbnail set.
if (lehigh_islandora_parent_needs_thumbnail($entity)) {
lehigh_islandora_set_parent_thumbnail($entity);
}
// Or check if this node's parent is a compound object
// and check if the parent needs a thumbnail.
elseif ($entity->hasField('field_member_of')) {
foreach ($entity->field_member_of as $parent) {
if (!is_null($parent->entity) && lehigh_islandora_parent_needs_thumbnail($parent->entity)) {
lehigh_islandora_set_parent_thumbnail($parent->entity);
break;
}
}
}
if (lehigh_site_support_identify_collection($entity, TRUE) &&
!is_null($entity->original) &&
!lehigh_site_support_identify_collection($entity->original, TRUE)) {
$alias_storage = \Drupal::service('entity_type.manager')->getStorage('path_alias');
$path = '/node/' . $entity->id();
$aliases = $alias_storage->loadByProperties(['path' => $path]);
foreach ($aliases as $alias) {
$alias->delete();
}
}
if (!lehigh_site_support_identify_collection($entity, TRUE) &&
!is_null($entity->original) &&
lehigh_site_support_identify_collection($entity->original, TRUE)) {
$alias_storage = \Drupal::service('entity_type.manager')->getStorage('path_alias');
$path = '/browse-items/' . $entity->id();
$aliases = $alias_storage->loadByProperties(['path' => $path]);
foreach ($aliases as $alias) {
$alias->delete();
}
}
}
/**
* Implements hook_ENTITY_TYPE_presave().
*/
function lehigh_islandora_media_presave(EntityInterface $media) {
// For microsoft 365 documents
// set the original file to preservation master
// so we can create original file being pdf
// then service file being.
if (lehigh_islandora_media_is_ms_document($media) &&
$media->field_media_use->entity->label() === 'Original File') {
$media->set('field_media_use', lehigh_islandora_get_tid_by_name('Preservation Master File', 'islandora_media_use'));
}
}
/**
* Check if a media entity contains a microsoft document.
*/
function lehigh_islandora_media_is_ms_document($media) {
if ($media->bundle() !== 'file' && $media->bundle() !== 'document') {
return FALSE;
}
if ($media->field_media_use->isEmpty() || is_null($media->field_media_use->entity)) {
return FALSE;
}
$file_field = $media->bundle() === 'document' ? 'field_media_document' : 'field_media_file';
$file = $media->get($file_field);
if ($file->isEmpty() || is_null($file->entity)) {
return FALSE;
}
$uri_components = explode('.', $file->entity->getFileUri());
$extension = array_pop($uri_components);
return in_array($extension, ['doc', 'docx', 'ppt', 'pptx', 'xls', 'xlsx']);
}
/**
*
*/
function lehigh_islandora_parent_needs_thumbnail($entity) : bool {
return $entity->hasField('field_model')
&& $entity->hasField('field_thumbnail')
&& !$entity->field_model->isEmpty()
&& !is_null($entity->field_model->entity)
&& $entity->field_thumbnail->isEmpty()
&& in_array($entity->field_model->entity->label(), ['Paged Content', 'Compound Object']);
}
/**
*
*/
function lehigh_islandora_set_parent_thumbnail($node) {
$view = Views::getView('children');
if (!is_object($view)) {
\Drupal::logger('lehigh_islandora')->error("Missing children view");
return;
}
$view->setDisplay('block_1');
$view->setArguments([$node->id()]);
$view->execute();
// Bail if there are no children.
if (empty($view->result[0]->_entity)) {
return;
}
$child = $view->result[0]->_entity;
// See if the child has a thumbnail set in the respective field
// and use it for this compount object parent's thumbnail.
if ($child->hasField('field_thumbnail') && !$child->field_thumbnail->isEmpty()) {
$node->set('field_thumbnail', $child->field_thumbnail->target_id);
}
// otherwise, see if there's a thumbnail attached
// and use it for this compount object parent's thumbnail.
else {
$mid = \Drupal::database()->query("SELECT o.entity_id FROM media__field_media_of o
INNER JOIN media__field_media_use u ON u.entity_id = o.entity_id
WHERE field_media_use_target_id = :tid AND field_media_of_target_id = :nid", [
":tid" => lehigh_islandora_get_tid_by_name("Thumbnail Image", "islandora_models"),
':nid' => $child->id(),
])->fetchField();
if ($mid) {
$node->set('field_thumbnail', $mid);
}
}
}
/**
* Implements hook_ENTITY_TYPE_insert().
*/
function lehigh_islandora_node_insert(EntityInterface $entity) {
lehigh_islandora_clear_disk_cache($entity);
if ($entity->hasField('field_input_source') &&
$entity->field_input_source->value == 'input_form') {
$webhook_url = getenv('SLACK_WEBHOOK');
if (!$webhook_url) {
return;
}
$payload = [
'msg' => 'New submission: ' . $entity->toUrl('canonical', ['absolute' => TRUE])->toString(),
];
try {
\Drupal::httpClient()->post($webhook_url, [
'headers' => ['Content-Type' => 'application/json'],
'body' => Json::encode($payload),
]);
}
catch (RequestException $e) {
\Drupal::logger('lehigh_islandora')->error('Failed to send message to Slack: @message', ['@message' => $e->getMessage()]);
}
}
}
/**
* Implements hook_ENTITY_TYPE_update().
*/
function lehigh_islandora_node_update(EntityInterface $entity) {
lehigh_islandora_clear_disk_cache($entity);
}
/**
* Implements hook_ENTITY_TYPE_delete().
*/
function lehigh_islandora_node_delete(EntityInterface $entity) {
lehigh_islandora_clear_disk_cache($entity);
}
/**
*
*/
function lehigh_islandora_clear_disk_cache(EntityInterface $node) {
$filesystem = \Drupal::service('file_system');
$request = \Drupal::request();
$current_domain = $request->getHost();
// Remove cached serialized JSON for workbench exports from disk.
$json_cache = 'private://serialized/node/' . $node->id() . '.json';
$f = $filesystem->realpath($json_cache);
if (file_exists($f)) {
unlink($f);
}
// Remove cached IIIF manifests from disk
// for this node and its parent.
$base_dir = $filesystem->realpath("private://iiif/$current_domain");
$nids = [$node->id()];
if ($node->hasField('field_member_of')) {
foreach ($node->field_member_of as $parent) {
if (is_null($parent->entity)) {
continue;
}
$nids[] = $parent->entity->id();
}
}
foreach ($nids as $nid) {
$pattern = $base_dir . '/*/node/' . $nid . '/*.*';
array_map('unlink', glob($pattern));
lehigh_islandora_warm_cache($nid);
}
}
/**
*
*/
function lehigh_islandora_warm_cache($nid) {
$node = Node::load($nid);
if (!$node) {
return;
}
$eventGenerator = \Drupal::service('islandora.eventgenerator');
$stomp = \Drupal::service('islandora.stomp');
$logger = \Drupal::logger('lehigh_islandora');
$eventDispatcher = Drupal::service('event_dispatcher');
try {
$account = User::load(\Drupal::currentUser()->id());
$data = [
'queue' => 'islandora-cache-warmer',
'event' => 'Create',
];
$event = $eventDispatcher->dispatch(
new StompHeaderEvent($node, $account, $data, $data),
StompHeaderEvent::EVENT_NAME
);
$json = $eventGenerator->generateEvent($node, $account, $data);
$eventMessage = json_decode($json);
// Add the special target to crawl this node.
$eventMessage->target = $node->toUrl()->setAbsolute()->toString();
$json = json_encode($eventMessage);
$message = new Message(
$json,
$event->getHeaders()->all()
);
}
catch (StompHeaderEventException $e) {
$logger->error($e->getMessage());
return;
}
catch (StompException $e) {
$logger->error("Unable to connect to JMS Broker: @msg", ["@msg" => $e->getMessage()]);
return;
}
catch (\RuntimeException $e) {
$logger->error('Error generating event: @msg', ['@msg' => $e->getMessage()]);
return;
}
// Send the message.
try {
$stomp->begin();
$stomp->send("islandora-cache-warmer", $message);
$stomp->commit();
}
catch (StompException $e) {
// Log it.
$logger->error(
'Error publishing message: @msg',
['@msg' => $e->getMessage()]
);
}
// Also cache the IIIF manifest.
if ($node->hasField('field_model') && !$node->field_model->isEmpty()
&& !is_null($node->field_model->entity)) {
$model = $node->field_model->entity->label();
$iiif_url = "[node:url:unaliased:absolute]/manifest";
if (in_array($model, ['Publication Issue', 'Paged Content'])) {
$iiif_url = "[node:url:unaliased:absolute]/book-manifest";
}
$eventMessage->target = \Drupal::service('token')->replace($iiif_url, ['node' => $node]);
$json = json_encode($eventMessage);
$message = new Message(
$json,
$event->getHeaders()->all()
);
try {
$stomp->begin();
$stomp->send("islandora-cache-warmer", $message);
$stomp->commit();
}
catch (StompException $e) {
$logger->error(
'Error publishing message: @msg',
['@msg' => $e->getMessage()]
);
}
}
}
/**
* Helper function to fetch a taxonomy term by name/vocab.
*/
function lehigh_islandora_get_tid_by_name($name, $vid, $auto_create = FALSE) {
if ($name == "" || $vid == "") {
return FALSE;
}
static $cache = [];
$cache_key = $vid . ':' . $name;
if (isset($cache[$cache_key])) {
return $cache[$cache_key];
}
$tid = \Drupal::database()->query('SELECT tid FROM {taxonomy_term_field_data}
WHERE name = :name AND vid = :vid', [
':vid' => $vid,
':name' => $name,
])->fetchField();
if (!$tid && $auto_create) {
$term = Term::create([
'vid' => $vid,
'name' => $name,
]);
$term->save();
$tid = $term->id();
}
$cache[$cache_key] = $tid;
return $tid;
}
/**
* Helper function to get the OCFL directory of a fcrepo object ID.
*/
function lehigh_islandora_get_ocfl_dir(string $objectId) : string {
$digest = hash('sha256', $objectId);
$tupleSize = 3;
$numberOfTuples = 3;
$path = "/fcrepo/";
for ($i = 0; $i < $numberOfTuples * $tupleSize; $i += $tupleSize) {
$tuple = substr($digest, $i, $tupleSize);
$path .= $tuple . "/";
}
$path .= $digest;
return $path;
}
/**
* Implements hook_views_query_alter().
*/
function lehigh_islandora_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
if ($view->id() == 'browse') {
if (isset($view->args[0])) {
$arg = (int) $view->args[0];
}
else {
$current_path = \Drupal::service('path.current')->getPath();
$current_path = ltrim($current_path, '/');
$path_parts = explode('/', $current_path);
if (isset($path_parts[1]) && is_numeric($path_parts[1])) {
$arg = (int) $path_parts[1];
}
else {
$arg = -1;
}
}
// Get the term IDs we filter on.
$models = [
'collection' => lehigh_islandora_get_tid_by_name('Collection', 'islandora_models'),
'sub-collection' => lehigh_islandora_get_tid_by_name('Sub-Collection', 'islandora_models'),
'page' => lehigh_islandora_get_tid_by_name('Page', 'islandora_models'),
];
// /browse-items/-1 -> all non-collection items
// browse all items
if ($arg == -1) {
$query->addCondition('field_model', array_values($models), 'NOT IN');
if (empty($_GET['sort_bef_combine'])) {
$query->sort('search_api_relevance', 'DESC');
}
}
// /browse-items/0
// browse all collections
elseif ($arg == 0) {
$query->addCondition('field_model', [
$models['collection'],
$models['sub-collection'],
], 'IN');
$query->addCondition('field_member_of', [1, 2, 185], 'IN');
$query->sort('title');
}
// Browse a specific collection
// /browse-items/([1-9]([0-9]+)?) -> all items in collection.
else {
$query->addCondition('field_model', [$models['page']], 'NOT IN');
// Search all sub collections
// otherwise only show direct descendants.
$field = empty($_GET['search_api_fulltext']) ? 'field_member_of' : 'field_descendant_of';
$query->addCondition($field, [$arg], 'IN');
// See if there's a custom sort on this collection.
$sort = \Drupal::database()->query('SELECT field_sort_by_value FROM {node__field_sort_by} WHERE entity_id = :id', [':id' => $arg])->fetchField();
if ($sort && empty($_GET['sort_bef_combine'])) {
$components = explode('_', $sort);
$direction = array_pop($components);
$sort_by = implode('_', $components);
$query->sort($sort_by, $direction);
}
}
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function lehigh_islandora_form_node_islandora_object_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
$request = \Drupal::request();
$display_name = $request->query->get('display');
if ($display_name !== 'user_submission') {
return;
}
$form['field_abstract']['widget'][0]['attr0']['#default_value'] = 'abstract';
$form['#attached']['library'][] = 'lehigh_islandora/node-form';
}
/**
*
*/
function lehigh_islandora_preprocess_node(&$vars) {
$node = $vars['node'];
if ($vars['view_mode'] !== 'full' || $node->bundle() !== 'islandora_object') {
return;
}
if (lehigh_site_support_identify_collection($node, TRUE)) {
return;
}
$vars['#attached']['library'][] = 'lehigh_islandora/node';
}
/**
* Implements hook_file_download().
*/
function lehigh_islandora_file_download($uri) {
$nodes = \Drupal::database()->query("SELECT field_media_of_target_id, field_edtf_date_embargo_value FROM {file_managed} f
LEFT JOIN {media__field_media_audio_file} a ON field_media_audio_file_target_id = f.fid
LEFT JOIN {media__field_media_document} d ON field_media_document_target_id = f.fid
LEFT JOIN {media__field_media_file} mf ON field_media_file_target_id = f.fid
LEFT JOIN {media__field_media_image} i ON field_media_image_target_id = f.fid
LEFT JOIN {media__field_media_video_file} v ON field_media_video_file_target_id = f.fid
INNER JOIN {media__field_media_of} m ON
m.entity_id = a.entity_id OR
m.entity_id = d.entity_id OR
m.entity_id = mf.entity_id OR
m.entity_id = i.entity_id OR
m.entity_id = v.entity_id
LEFT JOIN {node__field_edtf_date_embargo} e ON e.entity_id = field_media_of_target_id AND field_edtf_date_embargo_value > NOW()
WHERE uri = :uri", [
':uri' => $uri,
])->fetchAllKeyed();
$access = FALSE;
foreach ($nodes as $nid => $embargoed) {
if ($embargoed) {
return -1;
}
$node = Node::load($nid);
if ($node && $node->access()) {
$access = TRUE;
}
}
if ($access) {
$ext = substr($uri, -4);
if ($ext == '.pdf') {
return ['Content-Type' => 'application/pdf'];
}
elseif ($ext == '.jp2') {
return ['Content-Type' => 'image/jp2'];
}
return 1;
}
return NULL;
}
/**
* Implements hook_user_login().
*/
function lehigh_islandora_user_login($account) {
setcookie('DrupalAuth', '1', 0, '/');
}
/**
* Implements hook_user_logout().
*/
function lehigh_islandora_user_logout($account) {
$cookie_name = 'DrupalAuth';
setcookie($cookie_name, '', time() - 3600, '/');
unset($_COOKIE[$cookie_name]);
}