forked from Islandora/openseadragon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
openseadragon.module
123 lines (109 loc) · 3.52 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
<?php
/**
* @file
* Main functions and template preprocessor.
*/
use Drupal\file\Entity\File;
use Drupal\Core\Template\Attribute;
use Drupal\Core\Url;
/**
* Implements hook_libraries_info().
*/
function openseadragon_libraries_info() {
$libraries['openseadragon'] = [
'name' => 'OpenSeadragon plugin',
'vendor url' => 'https://openseadragon.github.io/',
'download url' => 'https://github.com/openseadragon/openseadragon/master/zipball',
'version arguments' => [
'file' => 'openseadragon.js',
// //! openseadragon 2.2.1.
'pattern' => '@openseadragon ([0-9\.-]+)@',
'lines' => 1,
'columns' => 50,
],
'versions' => [
'2.2.1' => [
'files' => [
'js' => [
'openseadragon.js',
],
],
],
],
];
return $libraries;
}
/**
* Implements hook_theme().
*/
function openseadragon_theme() {
return [
'openseadragon_formatter' => [
'variables' => [
'item' => NULL,
'entity' => NULL,
'settings' => 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');
// TODO: Once Libraries API is finished find a function for this.
$base_library_path = 'sites/all/assets/vendor';
// Build the gallery id.
$id = $entity->id();
$openseadragon_viewer_id = 'openseadragon-viewer-' . $id;
$field_name = $item->getParent()->getName();
$field_value = $entity->get($field_name)->getValue();
if (isset($field_value['0']['target_id'])) {
// Load the image and take file uri.
$fid = $field_value[0]['target_id'];
$file = File::load($fid);
$resource = $fileinfo_service->getFileData($file);
}
$classes_array = ['openseadragon-viewer'];
$viewer_settings = $config->getSettings(TRUE);
$iiif_address = $config->getIiifAddress();
if (!is_null($iiif_address) && !empty($iiif_address) && array_key_exists('full_path', $resource)) {
// Get the path to the file by stripping off the site's base url.
$base = Url::fromRoute('<front>', [], ['absolute' => TRUE])->toString();
$path = str_replace($base, "", $resource['full_path']);
// Construct the tile source url.
$tile_source = rtrim($iiif_address, '/') . '/' . urlencode($path);
$variables['#attached']['drupalSettings']['openseadragon'] = [
'basePath' => Url::fromUri($iiif_address),
'fitToAspectRatio' => $viewer_settings['fit_to_aspect_ratio'],
'options' => [
'id' => $openseadragon_viewer_id,
'prefixUrl' => file_create_url("{$base_library_path}/openseadragon/images/"),
'tileSources' => $tile_source,
] + $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';
}
}