-
Notifications
You must be signed in to change notification settings - Fork 15
/
openseadragon.module
175 lines (148 loc) · 5.31 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
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
<?php
/**
* @file
* Main functions and template preprocessor.
*/
use Drupal\file\Entity\File;
use Drupal\Core\Cache\CacheableMetadata;
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) {
// Load the global settings.
$config = \Drupal::service('openseadragon.config');
$fileinfo_service = \Drupal::service('openseadragon.fileinfo');
// Initialize our attributes.
$variables['attributes'] = new Attribute();
$cache_meta = CacheableMetadata::createFromRenderArray($variables)
->addCacheableDependency($config);
$classes_array = ['openseadragon-viewer'];
$viewer_settings = $config->getSettings(TRUE);
$iiif_address = $config->getIiifAddress();
if (is_null($iiif_address) || empty($iiif_address)) {
$cache_meta->applyTo($variables);
return;
}
$item = $variables['item'];
$entity = $variables['entity'];
$cache_meta->addCacheableDependency($entity);
// 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);
$access_result = $file->access('view', NULL, TRUE);
$cache_meta->addCacheableDependency($file)
->addCacheableDependency($access_result);
if (!$access_result->isAllowed()) {
continue;
}
$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']['library'] = [
'openseadragon/init',
];
$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']['class'] = $classes_array;
$variables['attributes']['id'] = $openseadragon_viewer_id;
}
$cache_meta->applyTo($variables);
}
/**
* Implements template_preprocess_HOOK().
*/
function template_preprocess_openseadragon_iiif_manifest_block(&$variables) {
$cache_meta = CacheableMetadata::createFromRenderArray($variables);
// 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)) {
$cache_meta->applyTo($variables);
return;
}
// Load the global settings.
$config = \Drupal::service('openseadragon.config');
$cache_meta->addCacheableDependency($config);
// 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();
$viewer_settings['sequenceMode'] = count($tile_sources) > 1 && !$viewer_settings['collectionMode'];
$variables['attributes'] = new Attribute();
// Attach the viewer, using the image urls obtained from the manifest.
if (!is_null($iiif_address) && !empty($iiif_address) && !empty($tile_sources)) {
$variables['#attached']['library'] = [
'openseadragon/init',
];
$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']['class'] = $classes_array;
$variables['attributes']['id'] = $openseadragon_viewer_id;
}
$cache_meta->applyTo($variables);
}
/**
* 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';
}
}