ExtensionIconViewHelper.php 2.29 KB
Newer Older
1
<?php
2 3
namespace T3o\TerFe2\ViewHelpers;

4 5
/*
 * This file is part of the TYPO3 CMS project.
6
 *
7 8 9
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
10
 *
11 12
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
13
 *
14 15
 * The TYPO3 project - inspiring people to share!
 */
16

17 18
use T3o\TerFe2\Domain\Model\Version;

19 20 21
/**
 * Extension icon view helper
 */
22
class ExtensionIconViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper
23
{
24

25 26 27 28
    /**
     * @var string
     */
    protected $tagName = 'img';
29

30
    /**
31
     * @var \T3o\TerFe2\Provider\ProviderManager
32
     * @inject
33 34
     */
    protected $providerManager;
35

36 37 38 39 40 41 42 43 44
    /**
     * @var array
     */
    protected $fileIconOrder = [
        'svg',
        'png',
        'gif'
    ];

45 46 47 48 49 50 51
    /**
     * Initialize arguments
     */
    public function initializeArguments()
    {
        parent::initializeArguments();
        $this->registerUniversalTagAttributes();
Markus Sommer's avatar
Markus Sommer committed
52
        $this->registerTagAttribute('alt', 'string', 'Specifies an alternate text for an image', true);
53
        $this->registerArgument('version', Version::class, 'Version of extension', true);
54
    }
55

56 57 58 59 60
    /**
     * Renders an extension icon for given version object
     *
     * @return string Rendered image tag
     */
61
    public function render()
62
    {
63
        $version = $this->arguments['version'];
64

65 66 67
        $imageUrl = '';
        $provider = $version->getExtensionProvider();
        if (!empty($provider)) {
68 69
            foreach ($this->fileIconOrder as $fileType) {
                $imageUrl = $this->providerManager->getProvider($provider)->getIconUrl($version, $fileType);
70
                if (!empty($imageUrl) && file_exists(\T3o\TerFe2\Utility\FileUtility::getAbsolutePathFromUrl($imageUrl))) {
71 72
                    break;
                }
73 74
            }
        }
75

76
        if (empty($imageUrl) || !file_exists(\T3o\TerFe2\Utility\FileUtility::getAbsolutePathFromUrl($imageUrl))) {
77
            return '';
78
        }
79

80
        $this->tag->addAttribute('src', $imageUrl);
81 82
        $this->tag->addAttribute('height', 32);
        $this->tag->addAttribute('width', 32);
83 84 85
        return $this->tag->render();
    }
}