-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathiiif_image_field.module
537 lines (452 loc) · 17 KB
/
iiif_image_field.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
<?php
/**
* Provides a simple field for easily embedding images from IIIF Image Servers (currently supports Image API 2.0 only, with plans to support previous versions)
*
* This module is not intended to replace media - it does not allow for any local storage of images, custom players or anything else
* It simply allows users to embed images from IIIF Image Servers - and provides a hook to allow other modules to provide more providers.
*
* @author shaune
*/
/**
* Implementation of hook_menu
* create out admin page
*/
function iiif_image_field_menu(){
return array(
'admin/config/media/iiif_image_field' => array(
'title' => 'IIIF Images',
'description' => 'Configuration form for the IIIF Image Field, allows one to set viewer options.',
'page callback' => 'drupal_get_form',
'page arguments' => array('iiif_image_field_get_form'),
'access arguments' => array('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
)
);
}
/**
* Page callback: IIIF Image Field Admin settings
*
* @see iiif_image_field_menu()
*/
function iiif_image_field_get_form($form, &$form_state) {
$form['image_server_base'] = array(
'#type' => 'textfield',
'#title' => t('Base URL of IIIF Image Server'),
'#default_value' => variable_get('image_server_base', ''),
'#size' => 255,
'#maxlength' => 512,
'#description' => t('This is the combination of host server and prefix (path) to the service. No trailing slash.'),
'#required' => TRUE,
);
$form['image_api_version'] = array(
'#type' => 'select',
'#options' => array(
'1.1' => t('1.1'),
'2.0' => t('2.0')
),
'#title' => t('Image API Version'),
'#default_value' => variable_get('image_api_version', '1.1'),
'#description' => t('Set this to the highest Image API Version your image server supports.'),
'#required' => TRUE,
);
return system_settings_form($form);
}
/**
* Implementation of hook_field_info
* Define the fields we're creating
*/
function iiif_image_field_field_info(){
return array(
'iiif_image_field' => array(
'label' => 'IIIF Image',
'description' => 'Add Images from IIIF Image Server',
'settings' => array(
//no settings currently - not sure what we need
),
'instance_settings' => array(
'allowed_types' => array('libimages')
),
'default_widget' => 'iiif_image_field_widget',
'default_formatter' => 'iiif_image_field_formatter',
)
);
}
/**
* Implementation of hook_field_widget_info
* Define the widget for inputting
*/
function iiif_image_field_field_widget_info(){
return array(
'iiif_image_field_widget' => array(
'label' => 'IIIF Image Embed',
'description' => 'Provides a IIIF Image embed field',
'field types' => array('iiif_image_field'),
'settings' => array(),
'behaviors' => array(
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
'default value' => FIELD_BEHAVIOR_DEFAULT
)
)
);
}
/**
* implementation of hook_field_widget_form
*/
function iiif_image_field_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element){
//don't need to check the type right now because we're only defining one
$element += array(
// '#type' => 'iiif_image_field_widget'
'#type' => $instance['widget']['type'],
'#default_value' => isset($items[$delta]) ? $items[$delta] : '',
);
$element['iiif_image_id'] = array(
'#type' => 'textfield',
'#title' => 'Image ID',
// Add some js validation/form functions
//'#attached' => array(
// 'css' => array(drupal_get_path('module', 'iiif_image_field') . '/iiif_image_field.form.css'),
// 'js' => array(drupal_get_path('module', 'iiif_image_field') . '/iiif_image_field.form.js'),
// ),
'#default_value' => isset($items[$delta]['iiif_image_id'])?$items[$delta]['iiif_image_id']:''
);
$element['iiif_image_caption'] = array(
'#type' => 'textfield',
'#title' => 'Caption/Label',
// Add some js validation/form functions
//'#attached' => array(
// 'css' => array(drupal_get_path('module', 'iiif_image_field') . '/iiif_image_field.form.css'),
// 'js' => array(drupal_get_path('module', 'iiif_image_field') . '/iiif_image_field.form.js'),
// ),
'#default_value' => isset($items[$delta]['iiif_image_caption'])?$items[$delta]['iiif_image_caption']:''
);
$element['iiif_image_rotation'] = array(
'#type' => 'textfield',
'#title' => 'Rotation',
'#size' => 5,
// Add some js validation/form functions
//'#attached' => array(
// 'css' => array(drupal_get_path('module', 'iiif_image_field') . '/iiif_image_field.form.css'),
// 'js' => array(drupal_get_path('module', 'iiif_image_field') . '/iiif_image_field.form.js'),
// ),
'#default_value' => isset($items[$delta]['iiif_image_rotation'])?$items[$delta]['iiif_image_rotation']:''
);
return $element;
}
function iiif_image_fields_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
foreach ($items as $delta => $item) {
//check that its a valid id
}
}
function iiif_image_field_field_is_empty($item, $field){
return empty($item);
if ($field['type'] == 'iiif_image_field') {
if (empty($item['iiif_image_id']))
{
return TRUE;
}
}
return FALSE;
}
// IIIF Formatter Stuff
function iiif_image_field_field_formatter_info() {
return array(
'iiif_image_field_formatter' => array(
'label' => t('IIIF Image Formatter'),
'field types' => array('iiif_image_field'),
'settings' => array(
'height' => '',
'width' => '300',
'region_x' => '0',
'region_y' => '0',
'region_h' => '',
'region_w' => '300',
'behavior' => 'none',
'display_first_only' => '0',
'display_caption' => '1',
'rotation' => '0',
'quality' => 'native',
'format' => 'jpg'
),
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
)
);
}
/**
* Implements hook_field_formatter_settings_summary
*/
function iiif_image_field_field_formatter_settings_summary($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$region = $settings['region_x'] . "," . $settings['region_y'] . "," . $settings['region_h'] . "," . $settings['region_w'];
$dimensions = $settings['height'] . "," . $settings['width'];
if($settings['display_first_only']) { $first_only = "Only First Image"; } else { $first_only = "All Images"; }
if($settings['display_caption']) { $caption = "Yes"; } else { $caption = "No"; }
$summary = '';
if ($display['type'] == 'iiif_image_field_formatter') {
$summary = t('Behavior: @behavior', array('@behavior' => $settings['behavior']));
$summary .= '<br />' . t('Display: @first_only', array('@first_only' => $first_only));
$summary .= '<br />' . t('Display Caption: @caption', array('@caption' => $caption));
$summary .= '<br />' . t('Region: @region', array('@region' => $region));
$summary .= '<br />' . t('Dimensions: @dimensions', array('@dimensions' => $dimensions));
$summary .= '<br />' . t('Rotation: @rotation', array('@rotation' => $settings['rotation']));
$summary .= '<br />' . t('Quality: @quality', array('@quality' => $settings['quality']));
$summary .= '<br />' . t('Format: @format', array('@format' => $settings['format']));
}
return $summary;
}
/**
* Implements hook_field_formatter_settings_form
*/
function iiif_image_field_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$element = array();
if ($display['type'] == 'iiif_image_field_formatter') {
$element['behavior'] = array(
'#title' => t('Behavior'),
'#type' => 'select',
'#options' => array(
'none' => t('None'),
'link_to_content' => t('Link to Content'),
'zoom' => t('Zoom')
),
'#default_value' => $settings['behavior'],
'#required' => TRUE,
);
$element['display_first_only'] = array(
'#title' => t('Display First Image Only'),
'#type' => 'select',
'#options' => array(
'0' => t('No'),
'1' => t('Yes')
),
'#description' => t('If the field is set to be repeatable, select YES if you would like to only display one image for this View Mode. (Hint: recommended for Teaser-like views.)'),
'#default_value' => $settings['display_first_only'],
'#required' => TRUE,
);
$element['display_caption'] = array(
'#title' => t('Display Caption'),
'#type' => 'select',
'#options' => array(
'0' => t('No'),
'1' => t('Yes')
),
'#description' => t('Choose whether or not to display captions for this view mode.'),
'#default_value' => $settings['display_caption'],
'#required' => TRUE,
);
$element['region_x'] = array(
'#type' => 'textfield',
'#size' => '5',
'#title' => t('Region X'),
'#description' => t('X-coordinate where cropping begins. '),
'#default_value' => $settings['region_x'],
);
$element['region_y'] = array(
'#type' => 'textfield',
'#size' => '5',
'#title' => t('Region Y'),
'#description' => t('Y-coordinate where cropping begins.'),
'#default_value' => $settings['region_y'],
);
$element['region_h'] = array(
'#type' => 'textfield',
'#size' => '5',
'#title' => t('Region Height'),
'#description' => t('Height of the cropped region.'),
'#default_value' => $settings['region_h'],
);
$element['region_w'] = array(
'#type' => 'textfield',
'#size' => '5',
'#title' => t('Region Width'),
'#description' => t('Width of the cropped region.'),
'#default_value' => $settings['region_w'],
);
$element['height'] = array(
'#type' => 'textfield',
'#size' => '5',
'#title' => t('Height'),
'#description' => t('Height of the image (after cropping). You can use percentages, pixels, and bangs.'),
'#default_value' => $settings['height'],
);
$element['width'] = array(
'#type' => 'textfield',
'#size' => '5',
'#title' => t('Width'),
'#description' => t('Width of the image (after cropping). You can use percentages, pixels, and bangs.'),
'#default_value' => $settings['width'],
);
$element['rotation'] = array(
'#type' => 'textfield',
'#size' => '5',
'#title' => t('Rotation'),
'#description' => t('The rotation of the image in degrees. You may enter a number from 0 to 360. A background will be visible behind images where degrees are not multiples of 90. '),
'#default_value' => $settings['rotation'],
);
$element['quality'] = array(
'#type' => 'select',
'#options' => array(
'native' => t('native'),
'color' => t('color'),
'grey' => t('grey'),
'bitonal' => t('bitonal')
),
'#title' => t('Quality'),
'#description' => t('The quality parameter determines the mode of the image - color, greyscale, or black and white.'),
'#default_value' => $settings['quality'],
);
$element['format'] = array(
'#type' => 'select',
'#options' => array(
'jpg' => t('jpg'),
'png' => t('png'),
'gif' => t('gif')
),
'#title' => t('Format'),
'#description' => t('The quality parameter determines the mode of the image - color, greyscale, or black and white.'),
'#default_value' => $settings['format'],
);
}
return $element;
}
// Paint the field to the display
/**
* Implements hook_field_formatter_view
*/
function iiif_image_field_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display){
$element = array();
$delta = 0;
$element[$delta] =
array(
'#markup' => theme($display['type'],
array(
'element' => $items,
'field' => $instance,
'settings' => $display['settings'],
'entity' => $entity
)
)
);
return $element;
}
/**
* Implements hook_theme().
*/
function iiif_image_field_theme() {
return array(
// The theme function that our formatter uses:
'iiif_image_field_formatter' => array(
// Don't forget that all Drupal 7 theme functions have only one argument,
// so you declare what variables get passed within that argument instead.
// See http://drupal.org/node/224333#theme_changes
//'template' => 'iiif_image_field_formatter_iiif_image_field_formatter',
'variables' => array('element' => NULL, 'field' => NULL, 'settings' => NULL, 'entity' => NULL),
),
);
}
/**
* Theme function for 'iiif_image_field_formatter' link field formatter.
*/
function theme_iiif_image_field_formatter($variables) {
$nid = $variables['entity']->nid;
$node_title = $variables['entity']->title;
$node_path = drupal_lookup_path('alias', "node/".$nid);
// assume path_auto, but fall back to node id
if(empty($node_path)){
$node_path = "node/".$nid;
}
// GET THE IIIF SERVER BASE URI AND API VERSION
$image_server_base = variable_get('image_server_base', '');
$image_api_version = variable_get('image_api_version', '1.1');
$elements = $variables['element'];
$el_length = sizeof($elements);
// If Display first only
if($variables['settings']['display_first_only'] && !empty($variables['element'])){
$el_length = 1;
}
// ACCOMMODATE FOR DIFFERENCES IN API VERSIONS
$quality = $variables['settings']['quality'];
if($image_api_version == '2.0'){
if($variables['settings']['quality'] == 'grey'){
$quality = 'gray';
}
else if($variables['settings']['quality'] == 'native'){
$quality = 'default';
}
}
// APPLY INSTANCE OVERRIDES TO VIEW MODE SETTINGS
$rotation = $variables['settings']['rotation'];
// Zoom Behavior
if($variables['settings']['behavior'] == 'zoom'){
$markup = '<div class="viewer"></div>';
$img_sequence = "";
$i = 0;
foreach ($elements as $delta => $el) {
// APPLY INSTANCE OVERRIDES TO VIEW MODE SETTINGS
if(!empty($el['iiif_image_rotation'])){
$rotation = $el['iiif_image_rotation'];
}
if($i < $el_length && !empty($el['iiif_image_id'])){
$caption = '';
if($variables['settings']['display_caption']){
$caption = $el['iiif_image_caption'];
}
$img_sequence .= "{ id: '" . $el['iiif_image_id'] . "', label: '" . $caption . "', rotation: '" . $rotation . "' },";
}
$i++;
}
drupal_add_css(drupal_get_path('module', 'iiif_image_field') .'/css/font-awesome.min.css');
drupal_add_css(drupal_get_path('module', 'iiif_image_field') .'/css/iiifOsdViewer.css');
drupal_add_css('.viewer {
border: 1px solid #ccc;
height: 600px;
width: 800px;
background: #333;
}', 'inline');
drupal_add_js(drupal_get_path('module', 'iiif_image_field') .'/js/jquery-1.11.2.min.js');
drupal_add_js(drupal_get_path('module', 'iiif_image_field') .'/js/openseadragon.min.js');
drupal_add_js(drupal_get_path('module', 'iiif_image_field') .'/js/jquery.iiifOsdViewer.js', array('type' => 'file', 'scope' => 'footer', 'weight' => 2));
drupal_add_js("
(function ($) {
$('.viewer' ).addClass( 'rotate". $rotation ."' );
$('.viewer').iiifOsdViewer({
data: [{
iiifServer: '". $image_server_base ."',
iiifImageAPI: '". $image_api_version ."',
images: [". $img_sequence ."]}
]});
}(jQuery));",
array('type' => 'inline', 'scope' => 'footer', 'weight' => 3));
}else{
// Non-zoom behaviors
$markup = "";
drupal_add_css('figcaption { color: #999999; }', 'inline');
$i = 0;
foreach ($elements as $delta => $el) {
// APPLY INSTANCE OVERRIDES TO VIEW MODE SETTINGS
if(!empty($el['iiif_image_rotation'])){
$rotation = $el['iiif_image_rotation'];
}
if(!empty($el['iiif_image_id']) && $i < $el_length){
$figure = "";
// Basic "no behavior"
$figure = "<img src='" . $image_server_base . "/" . $el['iiif_image_id'] . "/full/200,/" . $rotation . "/" . $quality . "." . $variables['settings']['format'] . "'/>";
// Link to Conent Behavior
$fig_num = $delta +1;
if($variables['settings']['behavior'] == 'link_to_content'){
$figure = '<a href="'. $GLOBALS["base_url"] . '/' . $node_path . '" alt="' . $node_title . '[' . $fig_num .']">' . $figure . '</a>';
}
$figure = "<figure style='display:inline-block'>" . $figure;
// If it has a caption or citation use the figure and figcaption elements
if($variables['settings']['display_caption'] && !empty($el['iiif_image_caption'])){
$figure .= "<figcaption>" . $el['iiif_image_caption'] . "</figcaption>";
}
$figure .= "</figure>";
$markup .= $figure;
}
$i++;
}
}
return $markup;
}