[TASK] Replace FileInfoHook with renderType
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / Element / FileInfoElement.php
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\Backend\Form\Element;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use TYPO3\CMS\Backend\Utility\BackendUtility;
19 use TYPO3\CMS\Core\Resource\File;
20 use TYPO3\CMS\Core\Resource\ProcessedFile;
21 use TYPO3\CMS\Core\Resource\ResourceFactory;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23 use TYPO3\CMS\Lang\LanguageService;
24
25 /**
26 * This renderType is used with type=user in FAL for table sys_file and
27 * sys_file_metadata, for field fileinfo and renders an informational
28 * element with image preview, filename, size and similar.
29 */
30 class FileInfoElement extends AbstractFormElement
31 {
32
33 /**
34 * Handler for single nodes
35 *
36 * @return array As defined in initializeResultArray() of AbstractNode
37 */
38 public function render(): array
39 {
40 $resultArray = $this->initializeResultArray();
41
42 $fileUid = 0;
43 if ($this->data['tableName'] === 'sys_file') {
44 $fileUid = (int)$this->data['databaseRow']['uid'];
45 } elseif ($this->data['tableName'] === 'sys_file_metadata') {
46 $fileUid = (int)$this->data['databaseRow']['file'][0];
47 }
48
49 $fileObject = null;
50 if ($fileUid > 0) {
51 $fileObject = ResourceFactory::getInstance()->getFileObject($fileUid);
52 }
53 $resultArray['html'] = $this->renderFileInformationContent($fileObject);
54 return $resultArray;
55 }
56
57 /**
58 * Renders a HTML Block with file information
59 *
60 * @param File $file
61 * @return string
62 */
63 protected function renderFileInformationContent(File $file = null): string
64 {
65 /** @var LanguageService $lang */
66 $lang = $GLOBALS['LANG'];
67
68 if ($file !== null) {
69 $processedFile = $file->process(ProcessedFile::CONTEXT_IMAGEPREVIEW, ['width' => 150, 'height' => 150]);
70 $previewImage = $processedFile->getPublicUrl(true);
71 $content = '';
72 if ($file->isMissing()) {
73 $content .= '<span class="label label-danger label-space-right">'
74 . htmlspecialchars($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:warning.file_missing'))
75 . '</span>';
76 }
77 if ($previewImage) {
78 $content .= '<img src="' . htmlspecialchars($previewImage) . '" ' .
79 'width="' . $processedFile->getProperty('width') . '" ' .
80 'height="' . $processedFile->getProperty('height') . '" ' .
81 'alt="" class="t3-tceforms-sysfile-imagepreview" />';
82 }
83 $content .= '<strong>' . htmlspecialchars($file->getName()) . '</strong>';
84 $content .= ' (' . htmlspecialchars(GeneralUtility::formatSize($file->getSize())) . 'bytes)<br />';
85 $content .= BackendUtility::getProcessedValue('sys_file', 'type', $file->getType()) . ' (' . $file->getMimeType() . ')<br />';
86 $content .= htmlspecialchars($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_misc.xlf:fileMetaDataLocation')) . ': ';
87 $content .= htmlspecialchars($file->getStorage()->getName()) . ' - ' . htmlspecialchars($file->getIdentifier()) . '<br />';
88 $content .= '<br />';
89 } else {
90 $content = '<h2>' . htmlspecialchars($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_misc.xlf:fileMetaErrorInvalidRecord')) . '</h2>';
91 }
92
93 return $content;
94 }
95 }