Completed
Branch master (d17104)
by Christian
21:20
created

ClickEnlargeViewHelper   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 56
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getTypoScriptService() 0 3 1
A renderStatic() 0 11 2
A initializeArguments() 0 8 1
A getContentObjectRenderer() 0 3 1
1
<?php
2
namespace TYPO3\CMS\FluidStyledContent\ViewHelpers\Link;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use TYPO3\CMS\Core\Resource\FileInterface;
18
use TYPO3\CMS\Core\Resource\FileReference;
19
use TYPO3\CMS\Core\TypoScript\TypoScriptService;
20
use TYPO3\CMS\Core\Utility\GeneralUtility;
21
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
22
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
23
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
24
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
25
26
/**
27
 * A view helper for creating a link for an image popup.
28
 *
29
 * = Example =
30
 *
31
 * <code title="enlarge image on click">
32
 * <ce:link.clickEnlarge image="{image}" configuration="{settings.images.popup}"><img src=""></ce:link.clickEnlarge>
33
 * </code>
34
 *
35
 * <output>
36
 * <a href="url" onclick="javascript" target="thePicture"><img src=""></a>
37
 * </output>
38
 */
39
class ClickEnlargeViewHelper extends AbstractViewHelper
40
{
41
    use CompileWithRenderStatic;
42
43
    /**
44
     * @var bool
45
     */
46
    protected $escapeOutput = false;
47
48
    /**
49
     * Initialize ViewHelper arguments
50
     */
51
    public function initializeArguments()
52
    {
53
        $this->registerArgument('image', FileReference::class, 'The original image file', true);
54
        $this->registerArgument(
55
            'configuration',
56
            'mixed',
57
            'String, \TYPO3\CMS\Core\Resource\File or \TYPO3\CMS\Core\Resource\FileReference with link configuration',
58
            true
59
        );
60
    }
61
62
    /**
63
     * @param array $arguments
64
     * @param \Closure $renderChildrenClosure
65
     * @param RenderingContextInterface $renderingContext
66
     * @return string
67
     */
68
    public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
69
    {
70
        $image = $arguments['image'];
71
        if ($image instanceof FileInterface) {
72
            self::getContentObjectRenderer()->setCurrentFile($image);
73
        }
74
        $configuration = self::getTypoScriptService()->convertPlainArrayToTypoScriptArray($arguments['configuration']);
75
        $content = $renderChildrenClosure();
76
        $configuration['enable'] = true;
77
78
        return self::getContentObjectRenderer()->imageLinkWrap($content, $image, $configuration);
79
    }
80
81
    /**
82
     * @return ContentObjectRenderer
83
     */
84
    protected static function getContentObjectRenderer()
85
    {
86
        return $GLOBALS['TSFE']->cObj;
87
    }
88
89
    /**
90
     * @return TypoScriptService
91
     */
92
    protected static function getTypoScriptService(): TypoScriptService
93
    {
94
        return GeneralUtility::makeInstance(TypoScriptService::class);
95
    }
96
}
97