‪TYPO3CMS  10.4
DistributionImageViewHelper.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
20 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
21 
27 class ‪DistributionImageViewHelper extends AbstractTagBasedViewHelper
28 {
32  protected ‪$tagName = 'img';
33 
37  public function ‪initializeArguments()
38  {
39  parent::initializeArguments();
40  $this->registerArgument('extensionkey', 'string', '', true);
41  $this->registerArgument('width', 'int', 'width of the image');
42  $this->registerArgument('height', 'int', 'height of the image');
43  $this->registerUniversalTagAttributes();
44  $this->registerTagAttribute('alt', 'string', 'Specifies an alternate text for an image', false);
45  }
46 
52  public function ‪render()
53  {
54  $extensionKey = $this->arguments['extensionkey'];
55  $width = $this->arguments['width'];
56  $height = $this->arguments['height'];
57  $content = '';
58 
59  $src = $this->‪findImage($extensionKey);
60  $uri = $this->‪getImageUri($src);
61 
62  if (!empty($uri)) {
63  if ($width) {
64  $this->tag->addAttribute('width', (int)$width);
65  }
66  if ($height) {
67  $this->tag->addAttribute('height', (int)$height);
68  }
69  $this->tag->addAttribute('src', $uri);
70  $content = $this->tag->render();
71  }
72 
73  return $content;
74  }
75 
82  protected function ‪findImage($extensionKey)
83  {
84  $paths = [];
85  $paths[] = 'EXT:' . $extensionKey . '/Resources/Public/Images/Distribution.svg';
86  $paths[] = 'EXT:' . $extensionKey . '/Resources/Public/Images/Distribution.png';
87  $paths[] = 'EXT:extensionmanager/Resources/Public/Images/Distribution.svg';
88 
89  foreach ($paths as $path) {
90  $absFileName = GeneralUtility::getFileAbsFileName($path);
91  if (file_exists($absFileName)) {
92  return $absFileName;
93  }
94  }
95 
96  return '';
97  }
98 
105  protected function ‪getImageUri($src)
106  {
107  $uri = GeneralUtility::getFileAbsFileName($src);
108  if ($uri !== '' && !file_exists($uri)) {
109  $uri = '';
110  }
111  if ($uri !== '') {
112  $uri = '../' . ‪PathUtility::stripPathSitePrefix($uri);
113  }
114  return $uri;
115  }
116 }
‪TYPO3\CMS\Extensionmanager\ViewHelpers\DistributionImageViewHelper\findImage
‪string findImage($extensionKey)
Definition: DistributionImageViewHelper.php:81
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:24
‪TYPO3\CMS\Core\Utility\PathUtility\stripPathSitePrefix
‪static string stripPathSitePrefix($path)
Definition: PathUtility.php:372
‪TYPO3\CMS\Extensionmanager\ViewHelpers\DistributionImageViewHelper\initializeArguments
‪initializeArguments()
Definition: DistributionImageViewHelper.php:36
‪TYPO3\CMS\Extensionmanager\ViewHelpers
‪TYPO3\CMS\Extensionmanager\ViewHelpers\DistributionImageViewHelper
Definition: DistributionImageViewHelper.php:28
‪TYPO3\CMS\Extensionmanager\ViewHelpers\DistributionImageViewHelper\$tagName
‪string $tagName
Definition: DistributionImageViewHelper.php:31
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Extensionmanager\ViewHelpers\DistributionImageViewHelper\getImageUri
‪string getImageUri($src)
Definition: DistributionImageViewHelper.php:104
‪TYPO3\CMS\Extensionmanager\ViewHelpers\DistributionImageViewHelper\render
‪string render()
Definition: DistributionImageViewHelper.php:51