forked from Islandora/openseadragon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
openseadragon.module
149 lines (127 loc) · 4.57 KB
/
openseadragon.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
<?php
/**
* @file
* Main functions and template preprocessor.
*/
use Drupal\file\Entity\File;
use Drupal\Core\Template\Attribute;
use Drupal\Core\Url;
use Drupal\Component\Utility\Html;
/**
* Implements hook_theme().
*/
function openseadragon_theme() {
return [
'openseadragon_formatter' => [
'variables' => [
'item' => NULL,
'entity' => NULL,
'settings' => NULL,
],
],
'openseadragon_iiif_manifest_block' => [
'variables' => [
'iiif_manifest_url' => NULL,
],
],
];
}
/**
* Implements template_preprocess_HOOK().
*/
function template_preprocess_openseadragon_formatter(&$variables) {
$item = $variables['item'];
$entity = $variables['entity'];
// Load the global settings.
$config = \Drupal::service('openseadragon.config');
$fileinfo_service = \Drupal::service('openseadragon.fileinfo');
$classes_array = ['openseadragon-viewer'];
$viewer_settings = $config->getSettings(TRUE);
$iiif_address = $config->getIiifAddress();
if (is_null($iiif_address) || empty($iiif_address)) {
return;
}
// Build the gallery id.
$id = $entity->id();
$openseadragon_viewer_id = 'openseadragon-viewer-' . $id;
$field_name = $item->getParent()->getName();
$tile_sources = [];
$field_value = $entity->get($field_name)->getValue();
foreach ($field_value as $value) {
// Load the image and take file uri.
if (isset($value['target_id'])) {
$fid = $value['target_id'];
$file = File::load($fid);
$resource = $fileinfo_service->getFileData($file);
if (isset($resource['full_path'])) {
$tile_sources[] = rtrim($iiif_address, '/') . '/' . urlencode($resource['full_path']);
}
}
}
if (!empty($tile_sources)) {
$viewer_settings['sequenceMode'] = count($tile_sources) > 1 && !$viewer_settings['collectionMode'];
$variables['#attached']['drupalSettings']['openseadragon'][$openseadragon_viewer_id] = [
'basePath' => Url::fromUri($iiif_address),
'fitToAspectRatio' => $viewer_settings['fit_to_aspect_ratio'],
'options' => [
'id' => $openseadragon_viewer_id,
'prefixUrl' => 'https://cdnjs.cloudflare.com/ajax/libs/openseadragon/2.4.2/images/',
'tileSources' => $tile_sources,
] + $viewer_settings,
];
$variables['attributes'] = new Attribute();
$variables['attributes']['class'] = $classes_array;
$variables['attributes']['id'] = $openseadragon_viewer_id;
}
}
/**
* Implements template_preprocess_HOOK().
*/
function template_preprocess_openseadragon_iiif_manifest_block(&$variables) {
// Load the global settings.
$config = \Drupal::service('openseadragon.config');
// @todo Once Libraries API is finished find a function for this.
$base_library_path = 'sites/all/assets/vendor';
// Build the gallery id.
$openseadragon_viewer_id = Html::getUniqueId('openseadragon-viewer-iiif-manifest-block');
$classes_array = ['openseadragon-viewer'];
$viewer_settings = $config->getSettings(TRUE);
$iiif_address = $config->getIiifAddress();
// Get the tile sources from the manifest.
$parser = \Drupal::service('openseadragon.manifest_parser');
$tile_sources = $parser->getTileSources($variables['iiif_manifest_url']);
if (empty($tile_sources)) {
return;
}
$viewer_settings['sequenceMode'] = count($tile_sources) > 1 && !$viewer_settings['collectionMode'];
// Attach the viewer, using the image urls obtained from the manifest.
if (!is_null($iiif_address) && !empty($iiif_address) && !empty($tile_sources)) {
$variables['#attached']['drupalSettings']['openseadragon'][$openseadragon_viewer_id] = [
'basePath' => Url::fromUri($iiif_address),
'fitToAspectRatio' => $viewer_settings['fit_to_aspect_ratio'],
'options' => [
'id' => $openseadragon_viewer_id,
'prefixUrl' => 'https://cdnjs.cloudflare.com/ajax/libs/openseadragon/2.4.2/images/',
'tileSources' => $tile_sources,
] + $viewer_settings,
];
$variables['attributes'] = new Attribute();
$variables['attributes']['class'] = $classes_array;
$variables['attributes']['id'] = $openseadragon_viewer_id;
}
}
/**
* Implements hook_file_mimetype_mapping_alter().
*/
function islandora_image_file_mimetype_mapping_alter(&$mapping) {
// Add new MIME type 'image/jp2'.
if (!in_array('image/jp2', $mapping['mimetypes'])) {
$mapping['mimetypes']['JP2'] = 'image/jp2';
$mapping['extensions']['jp2'] = 'JP2';
}
// Add Tiff extensions.
if (!in_array('image/tiff', $mapping['mimetypes'])) {
$mapping['mimetypes']['TIFF'] = "image/tiff";
$mapping['extensions']['tiff'] = 'TIFF';
}
}