registerArgument('row', 'array', 'content data', true); $this->registerArgument('tableName', 'string', 'table name', true); $this->registerArgument('fieldName', 'string', 'field name', true); } /** * Render a constant * * @return string Value of constant */ public function render(): string { $table = $this->arguments['tableName']; $field = $this->arguments['fieldName']; $row = $this->arguments['row']; $fileReferences = $this->resolveFileReferences($table, $field, $row); $fileObject = is_array($fileReferences) ? current($fileReferences)->getOriginalFile() : null; if ($fileObject && $fileObject->isMissing()) { /** @var IconFactory $iconFactory */ $iconFactory = GeneralUtility::makeInstance(IconFactory::class); $label = $this->getLanguageService()->sL( 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:warning.file_missing' ); return $iconFactory ->getIcon('mimetypes-other-other', IconSize::MEDIUM, 'overlay-missing') ->setTitle($label . ' ' . $fileObject->getName()) ->render(); } $previewFile = $fileObject->process( ProcessedFile::CONTEXT_IMAGEPREVIEW, [ 'width' => 64, 'height' => 64, ] ); return $this->linkEditContent('', $row ); } public function resolveFileReferences( string $tableName, string $fieldName, array $element, ?int $workspaceId = null ): ?array { if (!($schema = self::getTcaSchema($tableName))?->hasField($fieldName)) { return null; } $field = $schema->getField($fieldName); if ($field instanceof FileFieldType === false) { return null; } $fileReferences = []; $relationHandler = GeneralUtility::makeInstance(RelationHandler::class); if ($workspaceId !== null) { $relationHandler->setWorkspaceId($workspaceId); } $relationHandler->initializeForField( $tableName, $field->getConfiguration(), $element, $element[$fieldName], ); $relationHandler->processDeletePlaceholder(); $referenceUids = $relationHandler->tableArray[$field->getConfiguration()['foreign_table']] ?? []; foreach ($referenceUids as $referenceUid) { try { $fileReference = GeneralUtility::makeInstance(ResourceFactory::class)->getFileReferenceObject( $referenceUid, [], $workspaceId === 0 ); $fileReferences[$fileReference->getUid()] = $fileReference; } catch (FileDoesNotExistException $e) { /** * We just catch the exception here * Reasoning: There is nothing an editor or even admin could do */ } catch (\InvalidArgumentException $e) { /** * The storage does not exist anymore * Log the exception message for admins as they maybe can restore the storage */ // @extensionScannerIgnoreLine self::getLogger()->error($e->getMessage(), [ 'table' => $tableName, 'fieldName' => $fieldName, 'referenceUid' => $referenceUid, 'exception' => $e, ]); } } return $fileReferences; } protected function linkEditContent(string $linkText, $row): string { if (empty($linkText)) { return $linkText; } $backendUser = $this->getBackendUser(); if ( $backendUser->check('tables_modify', 'tt_content') && $backendUser->recordEditAccessInternals('tt_content', $row) && new Permission( $backendUser->calcPerms(BackendUtility::getRecord('pages', $row['pid']) ?? []) )->editContentPermissionIsGranted() ) { $urlParameters = [ 'edit' => [ 'tt_content' => [ $row['uid'] => 'edit', ], ], // @extensionScannerIgnoreLine 'returnUrl' => $this->getRequest()->getAttribute('normalizedParams')->getRequestUri() . '#element-tt_content-' . $row['uid'], ]; /** @var UriBuilder $uriBuilder */ $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class); $url = (string)$uriBuilder->buildUriFromRoute('record_edit', $urlParameters); return '' . $linkText . ''; } return $linkText; } protected static function getLogger(): LoggerInterface { return GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__); } protected static function getTcaSchema(string $tableName): ?TcaSchema { $schemaFactory = GeneralUtility::makeInstance(TcaSchemaFactory::class); return $schemaFactory->has($tableName) ? $schemaFactory->get($tableName) : null; } protected function getBackendUser(): BackendUserAuthentication { return $GLOBALS['BE_USER']; } protected function getLanguageService(): LanguageService { return $GLOBALS['LANG']; } protected function getRequest(): ServerRequestInterface { return $GLOBALS['TYPO3_REQUEST']; } }