‪TYPO3CMS  9.5
DistributionImageViewHelper.php
Go to the documentation of this file.
1 <?php
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  */
18 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
19 
25 class ‪DistributionImageViewHelper extends AbstractTagBasedViewHelper
26 {
30  protected ‪$tagName = 'img';
31 
35  public function ‪initializeArguments()
36  {
37  parent::initializeArguments();
38  $this->registerArgument('extensionkey', 'string', '', true);
39  $this->registerArgument('width', 'int', 'width of the image');
40  $this->registerArgument('height', 'int', 'height of the image');
41  $this->registerUniversalTagAttributes();
42  $this->registerTagAttribute('alt', 'string', 'Specifies an alternate text for an image', false);
43  }
44 
50  public function ‪render()
51  {
52  $extensionKey = $this->arguments['extensionkey'];
53  $width = $this->arguments['width'];
54  $height = $this->arguments['height'];
55  $content = '';
56 
57  $src = $this->‪findImage($extensionKey);
58  $uri = $this->‪getImageUri($src);
59 
60  if (!empty($uri)) {
61  if ($width) {
62  $this->tag->addAttribute('width', (int)$width);
63  }
64  if ($height) {
65  $this->tag->addAttribute('height', (int)$height);
66  }
67  $this->tag->addAttribute('src', $uri);
68  $content = $this->tag->render();
69  }
70 
71  return $content;
72  }
73 
80  protected function ‪findImage($extensionKey)
81  {
82  $paths = [];
83  $paths[] = 'EXT:' . $extensionKey . '/Resources/Public/Images/Distribution.svg';
84  $paths[] = 'EXT:' . $extensionKey . '/Resources/Public/Images/Distribution.png';
85  $paths[] = 'EXT:extensionmanager/Resources/Public/Images/Distribution.svg';
86 
87  foreach ($paths as $path) {
88  $absFileName = GeneralUtility::getFileAbsFileName($path);
89  if (file_exists($absFileName)) {
90  return $absFileName;
91  }
92  }
93 
94  return '';
95  }
96 
103  protected function ‪getImageUri($src)
104  {
105  $uri = GeneralUtility::getFileAbsFileName($src);
106  if ($uri !== '' && !file_exists($uri)) {
107  $uri = '';
108  }
109  if ($uri !== '') {
110  $uri = '../' . ‪PathUtility::stripPathSitePrefix($uri);
111  }
112  return $uri;
113  }
114 }
‪TYPO3\CMS\Extensionmanager\ViewHelpers\DistributionImageViewHelper\findImage
‪string findImage($extensionKey)
Definition: DistributionImageViewHelper.php:79
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:23
‪TYPO3\CMS\Core\Utility\PathUtility\stripPathSitePrefix
‪static string stripPathSitePrefix($path)
Definition: PathUtility.php:371
‪TYPO3\CMS\Extensionmanager\ViewHelpers\DistributionImageViewHelper\initializeArguments
‪initializeArguments()
Definition: DistributionImageViewHelper.php:34
‪TYPO3\CMS\Extensionmanager\ViewHelpers
‪TYPO3\CMS\Extensionmanager\ViewHelpers\DistributionImageViewHelper
Definition: DistributionImageViewHelper.php:26
‪TYPO3\CMS\Extensionmanager\ViewHelpers\DistributionImageViewHelper\$tagName
‪string $tagName
Definition: DistributionImageViewHelper.php:29
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Extensionmanager\ViewHelpers\DistributionImageViewHelper\getImageUri
‪string getImageUri($src)
Definition: DistributionImageViewHelper.php:102
‪TYPO3\CMS\Extensionmanager\ViewHelpers\DistributionImageViewHelper\render
‪string render()
Definition: DistributionImageViewHelper.php:49