-
-
Notifications
You must be signed in to change notification settings - Fork 200
/
LightboxViewHelper.php
111 lines (97 loc) · 5.99 KB
/
LightboxViewHelper.php
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
<?php declare(strict_types=1);
/*
* This file is part of the package bk2k/bootstrap-package.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/
namespace BK2K\BootstrapPackage\ViewHelpers\Link;
use TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection;
use TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Service\ImageService;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
class LightboxViewHelper extends AbstractTagBasedViewHelper
{
/**
* @var string
*/
protected $tagName = 'a';
/**
* @return void
*/
public function initializeArguments()
{
parent::initializeArguments();
$this->registerUniversalTagAttributes();
$this->registerTagAttribute('rel', 'string', 'Specifies the relationship between the current document and the linked document', false);
$this->registerArgument('caption', 'string', 'Specifies an caption text', false);
$this->registerArgument('lightbox-title', 'string', 'Specifies an title text', false);
$this->registerArgument('lightbox-alt', 'string', 'Specifies an alt text', false);
$this->registerArgument('lightbox-caption', 'string', 'Specifies an caption text', false);
$this->registerArgument('image', 'object', 'image', true);
$this->registerArgument('crop', 'string|bool', 'overrule cropping of image (setting to FALSE disables the cropping set in FileReference)');
$this->registerArgument('cropVariant', 'string', 'select a cropping variant, in case multiple croppings have been specified or stored in FileReference', false, 'default');
$this->registerArgument('width', 'string', 'width of the image. This can be a numeric value representing the fixed width of the image in pixels. But you can also perform simple calculations by adding "m" or "c" to the value. See imgResource.width for possible options.');
$this->registerArgument('height', 'string', 'height of the image. This can be a numeric value representing the fixed height of the image in pixels. But you can also perform simple calculations by adding "m" or "c" to the value. See imgResource.width for possible options.');
$this->registerArgument('minWidth', 'int', 'minimum width of the image');
$this->registerArgument('minHeight', 'int', 'minimum height of the image');
$this->registerArgument('maxWidth', 'int', 'maximum width of the image');
$this->registerArgument('maxHeight', 'int', 'maximum height of the image');
$this->registerArgument('absolute', 'bool', 'Force absolute URL', false, false);
}
public function render(): string
{
try {
$imageService = $this->getImageService();
$image = $imageService->getImage('', $this->arguments['image'], false);
$cropString = $this->arguments['crop'];
if ($cropString === null && $image->hasProperty('crop') && $image->getProperty('crop') !== '') {
$cropString = $image->getProperty('crop');
}
$cropVariantCollection = CropVariantCollection::create((string)$cropString);
$cropVariant = $this->arguments['cropVariant'] ?? 'default';
$cropArea = $cropVariantCollection->getCropArea($cropVariant);
$processingInstructions = [
'width' => $this->arguments['width'],
'height' => $this->arguments['height'],
'minWidth' => $this->arguments['minWidth'],
'minHeight' => $this->arguments['minHeight'],
'maxWidth' => $this->arguments['maxWidth'],
'maxHeight' => $this->arguments['maxHeight'],
'crop' => $cropArea->isEmpty() ? null : $cropArea->makeAbsoluteBasedOnFile($image),
];
$processedImage = $imageService->applyProcessingInstructions($image, $processingInstructions);
$imageUri = $imageService->getImageUri($processedImage, isset($this->arguments['absolute']) ? $this->arguments['absolute'] : false);
$this->tag->addAttribute('href', $imageUri);
$this->tag->addAttribute('data-lightbox-width', $processedImage->getProperty('width') !== null ? strval($processedImage->getProperty('width')) : null);
$this->tag->addAttribute('data-lightbox-height', $processedImage->getProperty('height') !== null ? strval($processedImage->getProperty('height')) : null);
$this->tag->addAttribute('data-lightbox-title', isset($this->arguments['lightbox-title']) ? $this->arguments['lightbox-title'] : null);
$this->tag->addAttribute('data-lightbox-alt', isset($this->arguments['lightbox-alt']) ? $this->arguments['lightbox-alt'] : null);
$this->tag->addAttribute('data-lightbox-caption', isset($this->arguments['lightbox-caption']) ? $this->arguments['lightbox-caption'] : null);
$this->tag->setContent(strval($this->renderChildren()));
$this->tag->forceClosingTag(true);
} catch (ResourceDoesNotExistException $e) {
// thrown if file does not exist
throw new Exception($e->getMessage(), 1622197950, $e);
} catch (\UnexpectedValueException $e) {
// thrown if a file has been replaced with a folder
throw new Exception($e->getMessage(), 1622197951, $e);
} catch (\RuntimeException $e) {
// RuntimeException thrown if a file is outside of a storage
throw new Exception($e->getMessage(), 1622197952, $e);
} catch (\InvalidArgumentException $e) {
// thrown if file storage does not exist
throw new Exception($e->getMessage(), 1622197953, $e);
}
return $this->tag->render();
}
/**
* @return ImageService
*/
protected function getImageService(): ImageService
{
return GeneralUtility::makeInstance(ImageService::class);
}
}