[FEATURE] Make it possible to register own icons for file extensions 60/43460/7
authorFrank Nägler <frank.naegler@typo3.org>
Mon, 21 Sep 2015 17:42:28 +0000 (19:42 +0200)
committerFrank Nägler <frank.naegler@typo3.org>
Wed, 23 Sep 2015 12:05:33 +0000 (14:05 +0200)
Resolves: #70002
Releases: master
Change-Id: Ia2df7f2fd73a71488027bd8b589abfe5fca96821
Reviewed-on: http://review.typo3.org/43460
Reviewed-by: Andreas Fernandez <typo3@scripting-base.de>
Tested-by: Andreas Fernandez <typo3@scripting-base.de>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Frank Nägler <frank.naegler@typo3.org>
Tested-by: Frank Nägler <frank.naegler@typo3.org>
typo3/sysext/core/Classes/Imaging/IconFactory.php
typo3/sysext/core/Classes/Imaging/IconRegistry.php
typo3/sysext/core/Documentation/Changelog/master/Feature-70002-MakeItPossibleToRegisterOwnIconsForFileExtensions.rst [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/Imaging/IconRegistryTest.php

index a16dbbe..4a96eeb 100644 (file)
@@ -35,83 +35,6 @@ class IconFactory {
        protected $iconRegistry;
 
        /**
-        * Mapping of file extensions to mimetypes
-        *
-        * @var string[]
-        */
-       protected $fileExtensionMapping = array(
-               'htm' => 'mimetypes-text-html',
-               'html' => 'mimetypes-text-html',
-               'css' => 'mimetypes-text-css',
-               'js' => 'mimetypes-text-js',
-               'csv' => 'mimetypes-text-csv',
-               'php' => 'mimetypes-text-php',
-               'php6' => 'mimetypes-text-php',
-               'php5' => 'mimetypes-text-php',
-               'php4' => 'mimetypes-text-php',
-               'php3' => 'mimetypes-text-php',
-               'inc' => 'mimetypes-text-php',
-               'ts' => 'mimetypes-text-ts',
-               'txt' => 'mimetypes-text-text',
-               'class' => 'mimetypes-text-text',
-               'tmpl' => 'mimetypes-text-text',
-               'jpg' => 'mimetypes-media-image',
-               'jpeg' => 'mimetypes-media-image',
-               'gif' => 'mimetypes-media-image',
-               'png' => 'mimetypes-media-image',
-               'bmp' => 'mimetypes-media-image',
-               'tif' => 'mimetypes-media-image',
-               'tiff' => 'mimetypes-media-image',
-               'tga' => 'mimetypes-media-image',
-               'psd' => 'mimetypes-media-image',
-               'eps' => 'mimetypes-media-image',
-               'ai' => 'mimetypes-media-image',
-               'svg' => 'mimetypes-media-image',
-               'pcx' => 'mimetypes-media-image',
-               'avi' => 'mimetypes-media-video',
-               'mpg' => 'mimetypes-media-video',
-               'mpeg' => 'mimetypes-media-video',
-               'mov' => 'mimetypes-media-video',
-               'wav' => 'mimetypes-media-audio',
-               'mp3' => 'mimetypes-media-audio',
-               'mid' => 'mimetypes-media-audio',
-               'swf' => 'mimetypes-media-flash',
-               'swa' => 'mimetypes-media-flash',
-               'exe' => 'mimetypes-application',
-               'com' => 'mimetypes-application',
-               't3x' => 'mimetypes-compressed',
-               't3d' => 'mimetypes-compressed',
-               'zip' => 'mimetypes-compressed',
-               'tgz' => 'mimetypes-compressed',
-               'gz' => 'mimetypes-compressed',
-               'pdf' => 'mimetypes-pdf',
-               'doc' => 'mimetypes-word',
-               'dot' => 'mimetypes-word',
-               'docm' => 'mimetypes-word',
-               'docx' => 'mimetypes-word',
-               'dotm' => 'mimetypes-word',
-               'dotx' => 'mimetypes-word',
-               'sxw' => 'mimetypes-word',
-               'rtf' => 'mimetypes-word',
-               'xls' => 'mimetypes-excel',
-               'xlsm' => 'mimetypes-excel',
-               'xlsx' => 'mimetypes-excel',
-               'xltm' => 'mimetypes-excel',
-               'xltx' => 'mimetypes-excel',
-               'sxc' => 'mimetypes-excel',
-               'pps' => 'mimetypes-powerpoint',
-               'ppsx' => 'mimetypes-powerpoint',
-               'ppt' => 'mimetypes-powerpoint',
-               'pptm' => 'mimetypes-powerpoint',
-               'pptx' => 'mimetypes-powerpoint',
-               'potm' => 'mimetypes-powerpoint',
-               'potx' => 'mimetypes-powerpoint',
-               'mount' => 'apps-filetree-mount',
-               'folder' => 'apps-filetree-folder-default',
-               'default' => 'mimetypes-other-other',
-       );
-
-       /**
         * Mapping of record status to overlays
         *
         * @var string[]
@@ -372,24 +295,12 @@ class IconFactory {
         * @return Icon
         */
        public function getIconForFileExtension($fileExtension, $size = Icon::SIZE_DEFAULT, $overlayIdentifier = NULL) {
-               $iconName = $this->getIconIdentifierForFileExtension($fileExtension);
+               $iconRegistry = GeneralUtility::makeInstance(IconRegistry::class);
+               $iconName = $iconRegistry->getIconIdentifierForFileExtension($fileExtension);
                return $this->getIcon($iconName, $size, $overlayIdentifier);
        }
 
        /**
-        * @param string $fileExtension
-        *
-        * @return string
-        */
-       protected function getIconIdentifierForFileExtension($fileExtension) {
-               // If the file extension is not valid use the default one
-               if (!isset($this->fileExtensionMapping[$fileExtension])) {
-                       $fileExtension = 'default';
-               }
-               return $this->fileExtensionMapping[$fileExtension];
-       }
-
-       /**
         * This method is used throughout the TYPO3 Backend to show icons for files and folders
         *
         * The method takes care of the translation of file extension to proper icon and for folders
@@ -462,7 +373,8 @@ class IconFactory {
                        if ($resource instanceof File && $resource->isMissing()) {
                                $overlayIdentifier = 'overlay-missing';
                        }
-                       $iconIdentifier = $this->getIconIdentifierForFileExtension($resource->getExtension());
+                       $iconRegistry = GeneralUtility::makeInstance(IconRegistry::class);
+                       $iconIdentifier = $iconRegistry->getIconIdentifierForFileExtension($resource->getExtension());
                }
 
                unset($options['mount-root']);
index 51d1a1a..7602912 100644 (file)
@@ -1633,6 +1633,83 @@ class IconRegistry implements \TYPO3\CMS\Core\SingletonInterface {
        );
 
        /**
+        * Mapping of file extensions to mimetypes
+        *
+        * @var string[]
+        */
+       protected $fileExtensionMapping = array(
+               'htm' => 'mimetypes-text-html',
+               'html' => 'mimetypes-text-html',
+               'css' => 'mimetypes-text-css',
+               'js' => 'mimetypes-text-js',
+               'csv' => 'mimetypes-text-csv',
+               'php' => 'mimetypes-text-php',
+               'php6' => 'mimetypes-text-php',
+               'php5' => 'mimetypes-text-php',
+               'php4' => 'mimetypes-text-php',
+               'php3' => 'mimetypes-text-php',
+               'inc' => 'mimetypes-text-php',
+               'ts' => 'mimetypes-text-ts',
+               'txt' => 'mimetypes-text-text',
+               'class' => 'mimetypes-text-text',
+               'tmpl' => 'mimetypes-text-text',
+               'jpg' => 'mimetypes-media-image',
+               'jpeg' => 'mimetypes-media-image',
+               'gif' => 'mimetypes-media-image',
+               'png' => 'mimetypes-media-image',
+               'bmp' => 'mimetypes-media-image',
+               'tif' => 'mimetypes-media-image',
+               'tiff' => 'mimetypes-media-image',
+               'tga' => 'mimetypes-media-image',
+               'psd' => 'mimetypes-media-image',
+               'eps' => 'mimetypes-media-image',
+               'ai' => 'mimetypes-media-image',
+               'svg' => 'mimetypes-media-image',
+               'pcx' => 'mimetypes-media-image',
+               'avi' => 'mimetypes-media-video',
+               'mpg' => 'mimetypes-media-video',
+               'mpeg' => 'mimetypes-media-video',
+               'mov' => 'mimetypes-media-video',
+               'wav' => 'mimetypes-media-audio',
+               'mp3' => 'mimetypes-media-audio',
+               'mid' => 'mimetypes-media-audio',
+               'swf' => 'mimetypes-media-flash',
+               'swa' => 'mimetypes-media-flash',
+               'exe' => 'mimetypes-application',
+               'com' => 'mimetypes-application',
+               't3x' => 'mimetypes-compressed',
+               't3d' => 'mimetypes-compressed',
+               'zip' => 'mimetypes-compressed',
+               'tgz' => 'mimetypes-compressed',
+               'gz' => 'mimetypes-compressed',
+               'pdf' => 'mimetypes-pdf',
+               'doc' => 'mimetypes-word',
+               'dot' => 'mimetypes-word',
+               'docm' => 'mimetypes-word',
+               'docx' => 'mimetypes-word',
+               'dotm' => 'mimetypes-word',
+               'dotx' => 'mimetypes-word',
+               'sxw' => 'mimetypes-word',
+               'rtf' => 'mimetypes-word',
+               'xls' => 'mimetypes-excel',
+               'xlsm' => 'mimetypes-excel',
+               'xlsx' => 'mimetypes-excel',
+               'xltm' => 'mimetypes-excel',
+               'xltx' => 'mimetypes-excel',
+               'sxc' => 'mimetypes-excel',
+               'pps' => 'mimetypes-powerpoint',
+               'ppsx' => 'mimetypes-powerpoint',
+               'ppt' => 'mimetypes-powerpoint',
+               'pptm' => 'mimetypes-powerpoint',
+               'pptx' => 'mimetypes-powerpoint',
+               'potm' => 'mimetypes-powerpoint',
+               'potx' => 'mimetypes-powerpoint',
+               'mount' => 'apps-filetree-mount',
+               'folder' => 'apps-filetree-folder-default',
+               'default' => 'mimetypes-other-other',
+       );
+
+       /**
         * Array of deprecated icons, add deprecated icons to this array and remove it from registry
         * - Index of this array contains the deprecated icon
         * - Value of each entry must contain the deprecation message and can contain an identifier which replaces the old identifier
@@ -1711,6 +1788,16 @@ class IconRegistry implements \TYPO3\CMS\Core\SingletonInterface {
        }
 
        /**
+        * Registers an icon for a file extension.
+        *
+        * @param string $fileExtension
+        * @param string $iconIdentifier
+        */
+       public function registerFileExtension($fileExtension, $iconIdentifier) {
+               $this->fileExtensionMapping[$fileExtension] = $iconIdentifier;
+       }
+
+       /**
         * Fetches the configuration provided by registerIcon()
         *
         * @param string $identifier the icon identifier
@@ -1761,6 +1848,18 @@ class IconRegistry implements \TYPO3\CMS\Core\SingletonInterface {
        }
 
        /**
+        * @param string $fileExtension
+        * @return string
+        */
+       public function getIconIdentifierForFileExtension($fileExtension) {
+               // If the file extension is not valid use the default one
+               if (!isset($this->fileExtensionMapping[$fileExtension])) {
+                       $fileExtension = 'default';
+               }
+               return $this->fileExtensionMapping[$fileExtension];
+       }
+
+       /**
         * Load icons from TCA for each table and add them as "tcarecords-XX" to $this->icons
         */
        protected function registerTCAIcons() {
diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-70002-MakeItPossibleToRegisterOwnIconsForFileExtensions.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-70002-MakeItPossibleToRegisterOwnIconsForFileExtensions.rst
new file mode 100644 (file)
index 0000000..c0e0957
--- /dev/null
@@ -0,0 +1,19 @@
+============================================================================
+Feature: #70002 - Make it possible to register own icons for file extensions
+============================================================================
+
+Description
+===========
+
+The IconRegistry has been extended with a mapping of file extensions.
+
+
+Impact
+======
+
+It is now possible to register or overwrite the iconIdentifier for a file extension.
+
+.. code-block:: php
+
+       $iconRegistry = GeneralUtility::makeInstance(IconRegistry::class);
+       $iconRegistry->registerFileExtension('log', 'icon-identiifer-for-log-files');
index 1c42e00..685dcbf 100644 (file)
@@ -123,4 +123,47 @@ class IconRegistryTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                $this->assertContains('default-not-found', $result);
        }
 
+       /**
+        * @test
+        */
+       public function getIconIdentifierForFileExtensionReturnsDefaultIconIdentifierForEmptyFileExtension() {
+               $result = $this->subject->getIconIdentifierForFileExtension('');
+               $this->assertEquals('mimetypes-other-other', $result);
+       }
+
+       /**
+        * @test
+        */
+       public function getIconIdentifierForFileExtensionReturnsDefaultIconIdentifierForUnknownFileExtension() {
+               $result = $this->subject->getIconIdentifierForFileExtension('xyz');
+               $this->assertEquals('mimetypes-other-other', $result);
+       }
+
+       /**
+        * @test
+        */
+       public function getIconIdentifierForFileExtensionReturnsImageIconIdentifierForImageFileExtension() {
+               $result = $this->subject->getIconIdentifierForFileExtension('jpg');
+               $this->assertEquals('mimetypes-media-image', $result);
+       }
+
+       /**
+        * @test
+        */
+       public function registerFileExtensionRegisterAnIcon() {
+               $this->subject->registerFileExtension('abc', 'xyz');
+               $result = $this->subject->getIconIdentifierForFileExtension('abc');
+               $this->assertEquals('xyz', $result);
+       }
+
+       /**
+        * @test
+        */
+       public function registerFileExtensionOverwriteAnExistingIcon() {
+               $this->subject->registerFileExtension('jpg', 'xyz');
+               $result = $this->subject->getIconIdentifierForFileExtension('jpg');
+               $this->assertEquals('xyz', $result);
+       }
+
+
 }