From 537d74cf03ab8185acc32d4a80ba4814c39d8ae7 Mon Sep 17 00:00:00 2001
From: Georg Ringer <georg.ringer@gmail.com>
Date: Fri, 16 Feb 2018 19:19:06 +0100
Subject: [PATCH] [FEATURE] Provide VH to render icons for resources

Provide a ViewHelper to render the markup to show
an icon for files and folders.

Resolves: #83942
Releases: master
Change-Id: I44a99f3e06dc35b98b3e29f509432a8575c29807
Reviewed-on: https://review.typo3.org/55759
Reviewed-by: Frank Naegler <frank.naegler@typo3.org>
Tested-by: Frank Naegler <frank.naegler@typo3.org>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: TYPO3com <no-reply@typo3.com>
---
 .../ViewHelpers/IconForResourceViewHelper.php | 66 +++++++++++++++++++
 ...tion-83942-DeprecatedFileFacadegetIcon.rst | 33 ++++++++++
 ...videViewHelperToRenderIconForResources.rst | 20 ++++++
 typo3/sysext/filelist/Classes/FileFacade.php  | 34 +++++-----
 .../Private/Templates/FileList/Search.html    |  2 +-
 .../Php/MethodCallMatcher.php                 |  7 ++
 6 files changed, 146 insertions(+), 16 deletions(-)
 create mode 100644 typo3/sysext/core/Classes/ViewHelpers/IconForResourceViewHelper.php
 create mode 100644 typo3/sysext/core/Documentation/Changelog/master/Deprecation-83942-DeprecatedFileFacadegetIcon.rst
 create mode 100644 typo3/sysext/core/Documentation/Changelog/master/Feature-83942-ProvideViewHelperToRenderIconForResources.rst

diff --git a/typo3/sysext/core/Classes/ViewHelpers/IconForResourceViewHelper.php b/typo3/sysext/core/Classes/ViewHelpers/IconForResourceViewHelper.php
new file mode 100644
index 000000000000..6881d5abf964
--- /dev/null
+++ b/typo3/sysext/core/Classes/ViewHelpers/IconForResourceViewHelper.php
@@ -0,0 +1,66 @@
+<?php
+declare(strict_types = 1);
+namespace TYPO3\CMS\Core\ViewHelpers;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * 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.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use TYPO3\CMS\Core\Imaging\Icon;
+use TYPO3\CMS\Core\Imaging\IconFactory;
+use TYPO3\CMS\Core\Resource\ResourceInterface;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
+use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
+use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
+
+/**
+ * Displays icon for a FAL resource (file or folder means a TYPO3\CMS\Core\Resource\ResourceInterface)
+ */
+class IconForResourceViewHelper extends AbstractViewHelper
+{
+    use CompileWithRenderStatic;
+
+    /**
+     * View helper returns HTML, thus we need to disable output escaping
+     *
+     * @var bool
+     */
+    protected $escapeOutput = false;
+
+    /**
+     * Initializes the arguments
+     */
+    public function initializeArguments()
+    {
+        $this->registerArgument('resource', ResourceInterface::class, 'Resource', true);
+        $this->registerArgument('size', 'string', 'The icon size', false, Icon::SIZE_SMALL);
+        $this->registerArgument('overlay', 'string', 'Overlay identifier', false, null);
+        $this->registerArgument('options', 'array', 'An associative array with additional options', false, []);
+        $this->registerArgument('alternativeMarkupIdentifier', 'string', 'Alternative markup identifier', false, null);
+    }
+
+    /**
+     * @param array $arguments
+     * @param \Closure $renderChildrenClosure
+     * @param RenderingContextInterface $renderingContext
+     * @return string
+     */
+    public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext): string
+    {
+        $resource = $arguments['resource'];
+        $size = $arguments['size'];
+        $overlay = $arguments['overlay'];
+        $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
+        return $iconFactory->getIconForResource($resource, $size, $overlay, $arguments['options'])->render($arguments['alternativeMarkupIdentifier']);
+    }
+}
diff --git a/typo3/sysext/core/Documentation/Changelog/master/Deprecation-83942-DeprecatedFileFacadegetIcon.rst b/typo3/sysext/core/Documentation/Changelog/master/Deprecation-83942-DeprecatedFileFacadegetIcon.rst
new file mode 100644
index 000000000000..480f3744c522
--- /dev/null
+++ b/typo3/sysext/core/Documentation/Changelog/master/Deprecation-83942-DeprecatedFileFacadegetIcon.rst
@@ -0,0 +1,33 @@
+.. include:: ../../Includes.txt
+
+====================================================
+Deprecation: #83942 - Deprecated FileFacade::getIcon
+====================================================
+
+See :issue:`83942`
+
+Description
+===========
+
+The method :php:`\TYPO3\CMS\Filelist\FileFacade::getIcon` has been marked as deprecated.
+
+
+Impact
+======
+
+Calling the method :php:`\TYPO3\CMS\Filelist\FileFacade::getIcon` will trigger a deprecation warning.
+
+
+Affected Installations
+======================
+
+Instances with extensions using the method :php:`\TYPO3\CMS\Filelist\FileFacade::getIcon`
+
+
+Migration
+=========
+
+Either use the ViewHelper :html:`<core:iconForResource resource="{file}" />` or
+:php:`GeneralUtility::makeInstance(IconFactory::class)->getIconForResource($resource)` to render a resource-based icon.
+
+.. index:: FAL, FullyScanned
diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-83942-ProvideViewHelperToRenderIconForResources.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-83942-ProvideViewHelperToRenderIconForResources.rst
new file mode 100644
index 000000000000..a374c255b091
--- /dev/null
+++ b/typo3/sysext/core/Documentation/Changelog/master/Feature-83942-ProvideViewHelperToRenderIconForResources.rst
@@ -0,0 +1,20 @@
+.. include:: ../../Includes.txt
+
+=================================================================
+Feature: #83942 - Provide ViewHelper to render icon for resources
+=================================================================
+
+See :issue:`83942`
+
+Description
+===========
+
+A new ViewHelper to render the icon markup based on a FAL resource has been introduced.
+
+Example:
+
+.. code-block:: html
+
+   <core:iconForResource resource="{file}" />
+
+.. index:: Backend, Fluid, ext:core
diff --git a/typo3/sysext/filelist/Classes/FileFacade.php b/typo3/sysext/filelist/Classes/FileFacade.php
index 56dd4586a980..51a018d9351e 100644
--- a/typo3/sysext/filelist/Classes/FileFacade.php
+++ b/typo3/sysext/filelist/Classes/FileFacade.php
@@ -15,9 +15,11 @@ namespace TYPO3\CMS\Filelist;
  */
 
 use TYPO3\CMS\Backend\Utility\BackendUtility;
+use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
 use TYPO3\CMS\Core\Database\ConnectionPool;
 use TYPO3\CMS\Core\Imaging\Icon;
 use TYPO3\CMS\Core\Imaging\IconFactory;
+use TYPO3\CMS\Core\Localization\LanguageService;
 use TYPO3\CMS\Core\Resource\FileInterface;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 
@@ -60,9 +62,11 @@ class FileFacade
 
     /**
      * @return string
+     * @deprecated
      */
-    public function getIcon()
+    public function getIcon(): string
     {
+        trigger_error('This method has been deprecated in v9 will be removed in TYPO3 v10, use ViewHelper <core:iconForResource /> instead', E_USER_DEPRECATED);
         $title = htmlspecialchars($this->resource->getName() . ' [' . (int)$this->resource->getProperty('uid') . ']');
         return '<span title="' . $title . '">' . $this->iconFactory->getIconForResource($this->resource, Icon::SIZE_SMALL) . '</span>';
     }
@@ -70,7 +74,7 @@ class FileFacade
     /**
      * @return \TYPO3\CMS\Core\Resource\FileInterface
      */
-    public function getResource()
+    public function getResource(): FileInterface
     {
         return $this->resource;
     }
@@ -78,7 +82,7 @@ class FileFacade
     /**
      * @return bool
      */
-    public function getIsEditable()
+    public function getIsEditable(): bool
     {
         return $this->getIsWritable()
             && GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'], $this->resource->getExtension());
@@ -87,7 +91,7 @@ class FileFacade
     /**
      * @return bool
      */
-    public function getIsMetadataEditable()
+    public function getIsMetadataEditable(): bool
     {
         return $this->resource->isIndexed() && $this->getIsWritable() && $this->getBackendUser()->check('tables_modify', 'sys_file_metadata');
     }
@@ -95,7 +99,7 @@ class FileFacade
     /**
      * @return int
      */
-    public function getMetadataUid()
+    public function getMetadataUid(): int
     {
         $uid = 0;
         $method = '_getMetadata';
@@ -113,7 +117,7 @@ class FileFacade
     /**
      * @return string
      */
-    public function getName()
+    public function getName(): string
     {
         return $this->resource->getName();
     }
@@ -121,7 +125,7 @@ class FileFacade
     /**
      * @return string
      */
-    public function getPath()
+    public function getPath(): string
     {
         $method = 'getReadablePath';
         if (is_callable([$this->resource->getParentFolder(), $method])) {
@@ -134,7 +138,7 @@ class FileFacade
     /**
      * @return string
      */
-    public function getPublicUrl()
+    public function getPublicUrl(): string
     {
         return $this->resource->getPublicUrl(true);
     }
@@ -142,7 +146,7 @@ class FileFacade
     /**
      * @return string
      */
-    public function getExtension()
+    public function getExtension(): string
     {
         return strtoupper($this->resource->getExtension());
     }
@@ -150,7 +154,7 @@ class FileFacade
     /**
      * @return string
      */
-    public function getIdentifier()
+    public function getIdentifier(): string
     {
         return $this->resource->getStorage()->getUid() . ':' . $this->resource->getIdentifier();
     }
@@ -158,7 +162,7 @@ class FileFacade
     /**
      * @return string
      */
-    public function getLastModified()
+    public function getLastModified(): string
     {
         return BackendUtility::date($this->resource->getModificationTime());
     }
@@ -166,7 +170,7 @@ class FileFacade
     /**
      * @return string
      */
-    public function getSize()
+    public function getSize(): string
     {
         return GeneralUtility::formatSize($this->resource->getSize(), htmlspecialchars($this->getLanguageService()->getLL('byteSizeUnits')));
     }
@@ -249,7 +253,7 @@ class FileFacade
      *
      * @return int
      */
-    public function getReferenceCount()
+    public function getReferenceCount(): int
     {
         $uid = (int)$this->resource->getProperty('uid');
 
@@ -303,7 +307,7 @@ class FileFacade
     /**
      * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
      */
-    protected function getBackendUser()
+    protected function getBackendUser(): BackendUserAuthentication
     {
         return $GLOBALS['BE_USER'];
     }
@@ -311,7 +315,7 @@ class FileFacade
     /**
      * @return \TYPO3\CMS\Core\Localization\LanguageService
      */
-    protected function getLanguageService()
+    protected function getLanguageService(): LanguageService
     {
         return $GLOBALS['LANG'];
     }
diff --git a/typo3/sysext/filelist/Resources/Private/Templates/FileList/Search.html b/typo3/sysext/filelist/Resources/Private/Templates/FileList/Search.html
index 69954f78933f..13bf8b46ecc1 100644
--- a/typo3/sysext/filelist/Resources/Private/Templates/FileList/Search.html
+++ b/typo3/sysext/filelist/Resources/Private/Templates/FileList/Search.html
@@ -32,7 +32,7 @@
 					<tr data-uid="0" data-l10nparent="0">
 						<td class="col-icon nowrap">
 							<a class="t3js-contextmenutrigger" data-uid="{file.identifier}" data-table="sys_file" href="#">
-								{file.icon->f:format.raw()}
+								<core:iconForResource resource="{file.resource}" />
 							</a>
 						</td>
 						<td class="col-path nowrap">
diff --git a/typo3/sysext/install/Configuration/ExtensionScanner/Php/MethodCallMatcher.php b/typo3/sysext/install/Configuration/ExtensionScanner/Php/MethodCallMatcher.php
index 03544d75b986..b0af35b261ef 100644
--- a/typo3/sysext/install/Configuration/ExtensionScanner/Php/MethodCallMatcher.php
+++ b/typo3/sysext/install/Configuration/ExtensionScanner/Php/MethodCallMatcher.php
@@ -1605,4 +1605,11 @@ return [
             'Deprecation-83904-ArrayHandlingInAbstractTreeView.rst',
         ],
     ],
+    'TYPO3\CMS\Filelist\FileFacade->getIcon' => [
+        'numberOfMandatoryArguments' => 0,
+        'maximumNumberOfArguments' => 0,
+        'restFiles' => [
+            'Deprecation-83942-DeprecatedFileFacadegetIcon.rst',
+        ],
+    ],
 ];
-- 
GitLab