/
imagezoom.module
62 lines (55 loc) · 1.77 KB
/
imagezoom.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
<?php
use Drupal\image\Entity\ImageStyle;
/**
* @file
* Provides an Image Zoom field formatter for Image fields.
*
* This module provides a field formatter that allows users to specify an image
* style to display, and another image style to use as the zoomed version of the
* image. Hovering the mouse over the image will display the zoomed version,
* which shifts with mouse movement.
*/
/**
* Implements hook_theme().
*/
function imagezoom_theme($existing, $type, $theme, $path)
{
return [
'imagezoom_image' => [
'variables' => [
'item' => NULL,
'display_style' => NULL,
'zoom_style' => NULL,
'settings' => NULL,
],
'template' => 'imagezoom-image',
],
];
}
/**
* Preprocess function for imagezoom_image.
*/
function template_preprocess_imagezoom_image(&$variables)
{
$item = $variables['item'];
if ($variables['display_style']) {
$variables['image'] = ImageStyle::load($variables['display_style'])->buildUrl($item->entity->getFileUri());
$image = Drupal::service('image.factory')->get($variables['image']);
$info['width'] = $image->getWidth();
$info['height'] = $image->getHeight();
} else {
$variables['image'] = file_create_url($item->entity->getFileUri());
$image = Drupal::service('image.factory')->get($variables['zoom']);
$info['width'] = $image->getWidth();
$info['height'] = $image->getHeight();
}
$variables['width'] = $info['width'];
$variables['height'] = $info['height'];
$variables['alt'] = $item->alt;
$variables['title'] = $item->title;
if ($variables['zoom_style']) {
$variables['zoom'] = ImageStyle::load($variables['zoom_style'])->buildUrl($item->entity->getFileUri());
} else {
$variables['zoom'] = file_create_url($item->entity->getFileUri());
}
}