[TASK] Replace FileInfoHook with renderType 74/51874/8
authorAnja Leichsenring <aleichsenring@ab-softlab.de>
Mon, 27 Feb 2017 14:00:18 +0000 (15:00 +0100)
committerChristian Kuhn <lolli@schwarzbu.ch>
Mon, 27 Feb 2017 18:07:53 +0000 (19:07 +0100)
Switch a userFunc to a proper renderType used in fileinfo field
of sys_file and sys_file_metadata table.

Resolves: #80039
Releases: master
Change-Id: I7093027716cb43fa2f00044f51d918cc8f91ee6e
Reviewed-on: https://review.typo3.org/51874
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Josef Glatz <josef.glatz@typo3.org>
Tested-by: Josef Glatz <josef.glatz@typo3.org>
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
typo3/sysext/backend/Classes/Form/Element/FileInfoElement.php [new file with mode: 0644]
typo3/sysext/backend/Classes/Form/NodeFactory.php
typo3/sysext/core/Classes/Resource/Hook/FileInfoHook.php [deleted file]
typo3/sysext/core/Configuration/TCA/sys_file.php
typo3/sysext/core/Configuration/TCA/sys_file_metadata.php

diff --git a/typo3/sysext/backend/Classes/Form/Element/FileInfoElement.php b/typo3/sysext/backend/Classes/Form/Element/FileInfoElement.php
new file mode 100644 (file)
index 0000000..2a34f1a
--- /dev/null
@@ -0,0 +1,95 @@
+<?php
+declare(strict_types=1);
+namespace TYPO3\CMS\Backend\Form\Element;
+
+/*
+ * 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\Backend\Utility\BackendUtility;
+use TYPO3\CMS\Core\Resource\File;
+use TYPO3\CMS\Core\Resource\ProcessedFile;
+use TYPO3\CMS\Core\Resource\ResourceFactory;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Lang\LanguageService;
+
+/**
+ * This renderType is used with type=user in FAL for table sys_file and
+ * sys_file_metadata, for field fileinfo and renders an informational
+ * element with image preview, filename, size and similar.
+ */
+class FileInfoElement extends AbstractFormElement
+{
+
+    /**
+     * Handler for single nodes
+     *
+     * @return array As defined in initializeResultArray() of AbstractNode
+     */
+    public function render(): array
+    {
+        $resultArray = $this->initializeResultArray();
+
+        $fileUid = 0;
+        if ($this->data['tableName'] === 'sys_file') {
+            $fileUid = (int)$this->data['databaseRow']['uid'];
+        } elseif ($this->data['tableName'] === 'sys_file_metadata') {
+            $fileUid = (int)$this->data['databaseRow']['file'][0];
+        }
+
+        $fileObject = null;
+        if ($fileUid > 0) {
+            $fileObject = ResourceFactory::getInstance()->getFileObject($fileUid);
+        }
+        $resultArray['html'] = $this->renderFileInformationContent($fileObject);
+        return $resultArray;
+    }
+
+    /**
+     * Renders a HTML Block with file information
+     *
+     * @param File $file
+     * @return string
+     */
+    protected function renderFileInformationContent(File $file = null): string
+    {
+        /** @var LanguageService $lang */
+        $lang = $GLOBALS['LANG'];
+
+        if ($file !== null) {
+            $processedFile = $file->process(ProcessedFile::CONTEXT_IMAGEPREVIEW, ['width' => 150, 'height' => 150]);
+            $previewImage = $processedFile->getPublicUrl(true);
+            $content = '';
+            if ($file->isMissing()) {
+                $content .= '<span class="label label-danger label-space-right">'
+                    . htmlspecialchars($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:warning.file_missing'))
+                    . '</span>';
+            }
+            if ($previewImage) {
+                $content .= '<img src="' . htmlspecialchars($previewImage) . '" ' .
+                    'width="' . $processedFile->getProperty('width') . '" ' .
+                    'height="' . $processedFile->getProperty('height') . '" ' .
+                    'alt="" class="t3-tceforms-sysfile-imagepreview" />';
+            }
+            $content .= '<strong>' . htmlspecialchars($file->getName()) . '</strong>';
+            $content .= ' (' . htmlspecialchars(GeneralUtility::formatSize($file->getSize())) . 'bytes)<br />';
+            $content .= BackendUtility::getProcessedValue('sys_file', 'type', $file->getType()) . ' (' . $file->getMimeType() . ')<br />';
+            $content .= htmlspecialchars($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_misc.xlf:fileMetaDataLocation')) . ': ';
+            $content .= htmlspecialchars($file->getStorage()->getName()) . ' - ' . htmlspecialchars($file->getIdentifier()) . '<br />';
+            $content .= '<br />';
+        } else {
+            $content = '<h2>' . htmlspecialchars($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_misc.xlf:fileMetaErrorInvalidRecord')) . '</h2>';
+        }
+
+        return $content;
+    }
+}
index 4523734..9ba4098 100644 (file)
@@ -90,6 +90,7 @@ class NodeFactory
         'textTable' => Element\TextTableElement::class,
         'unknown' => Element\UnknownElement::class,
         'user' => Element\UserElement::class,
+        'fileInfo' => Element\FileInfoElement::class,
 
         // Default classes to enrich single elements
         'fieldControl' => NodeExpansion\FieldControl::class,
diff --git a/typo3/sysext/core/Classes/Resource/Hook/FileInfoHook.php b/typo3/sysext/core/Classes/Resource/Hook/FileInfoHook.php
deleted file mode 100644 (file)
index 4a10f68..0000000
+++ /dev/null
@@ -1,100 +0,0 @@
-<?php
-namespace TYPO3\CMS\Core\Resource\Hook;
-
-/*
- * 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\Backend\Utility\BackendUtility;
-use TYPO3\CMS\Core\Resource\File;
-use TYPO3\CMS\Core\Resource\ProcessedFile;
-use TYPO3\CMS\Core\Resource\ResourceFactory;
-use TYPO3\CMS\Core\Utility\GeneralUtility;
-use TYPO3\CMS\Lang\LanguageService;
-
-/**
- * Utility class to render TCEforms information about a sys_file record
- */
-class FileInfoHook
-{
-    /**
-     * User function for sys_file (element)
-     *
-     * @param array $propertyArray the array with additional configuration options.
-     * @return string The HTML code for the TCEform field
-     */
-    public function renderFileInfo(array $propertyArray)
-    {
-        $fileRecord = $propertyArray['row'];
-        $fileObject = null;
-        if ($fileRecord['uid'] > 0) {
-            $fileObject = ResourceFactory::getInstance()->getFileObject((int)$fileRecord['uid']);
-        }
-        return $this->renderFileInformationContent($fileObject);
-    }
-
-    /**
-     * User function for sys_file_meta (element)
-     *
-     * @param array $propertyArray the array with additional configuration options.
-     * @return string The HTML code for the TCEform field
-     */
-    public function renderFileMetadataInfo(array $propertyArray)
-    {
-        $fileMetadataRecord = $propertyArray['row'];
-        $fileObject = null;
-        if (!empty($fileMetadataRecord['file']) && $fileMetadataRecord['file'][0] > 0) {
-            $fileObject = ResourceFactory::getInstance()->getFileObject((int)$fileMetadataRecord['file'][0]);
-        }
-
-        return $this->renderFileInformationContent($fileObject);
-    }
-
-    /**
-     * Renders a HTML Block with file information
-     *
-     * @param File $file
-     * @return string
-     */
-    protected function renderFileInformationContent(File $file = null)
-    {
-        /** @var LanguageService $lang */
-        $lang = $GLOBALS['LANG'];
-
-        if ($file !== null) {
-            $processedFile = $file->process(ProcessedFile::CONTEXT_IMAGEPREVIEW, ['width' => 150, 'height' => 150]);
-            $previewImage = $processedFile->getPublicUrl(true);
-            $content = '';
-            if ($file->isMissing()) {
-                $content .= '<span class="label label-danger label-space-right">'
-                    . htmlspecialchars($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:warning.file_missing'))
-                    . '</span>';
-            }
-            if ($previewImage) {
-                $content .= '<img src="' . htmlspecialchars($previewImage) . '" ' .
-                            'width="' . $processedFile->getProperty('width') . '" ' .
-                            'height="' . $processedFile->getProperty('height') . '" ' .
-                            'alt="" class="t3-tceforms-sysfile-imagepreview" />';
-            }
-            $content .= '<strong>' . htmlspecialchars($file->getName()) . '</strong>';
-            $content .= ' (' . htmlspecialchars(GeneralUtility::formatSize($file->getSize())) . 'bytes)<br />';
-            $content .= BackendUtility::getProcessedValue('sys_file', 'type', $file->getType()) . ' (' . $file->getMimeType() . ')<br />';
-            $content .= htmlspecialchars($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_misc.xlf:fileMetaDataLocation')) . ': ';
-            $content .= htmlspecialchars($file->getStorage()->getName()) . ' - ' . htmlspecialchars($file->getIdentifier()) . '<br />';
-            $content .= '<br />';
-        } else {
-            $content = '<h2>' . htmlspecialchars($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_misc.xlf:fileMetaErrorInvalidRecord')) . '</h2>';
-        }
-
-        return $content;
-    }
-}
index 0549b8e..b92b097 100644 (file)
@@ -30,7 +30,7 @@ return [
         'fileinfo' => [
             'config' => [
                 'type' => 'user',
-                'userFunc' => 'TYPO3\\CMS\\Core\\Resource\\Hook\\FileInfoHook->renderFileInfo'
+                'renderType' => 'fileInfo',
             ]
         ],
         'storage' => [
index 69a51c1..c98aa0b 100644 (file)
@@ -78,7 +78,7 @@ return [
         'fileinfo' => [
             'config' => [
                 'type' => 'user',
-                'userFunc' => 'TYPO3\\CMS\\Core\\Resource\\Hook\\FileInfoHook->renderFileMetadataInfo'
+                'renderType' => 'fileInfo',
             ]
         ],
         'file' => [