bithost-gmbh / pdfviewhelpers

TYPO3 CMS extension that provides various Fluid ViewHelpers to generate PDF documents.
GNU General Public License v3.0
44 stars 19 forks source link

Image viewhelper do not accept FileReference #208

Closed thorstenBorn-cosmoblonde closed 1 year ago

thorstenBorn-cosmoblonde commented 1 year ago

Describe the bug Using a FileReference object as src parameter leads to an error.

Environment TYPO3 version(s): v11.5.16 pdfviewhelpers version: v2.4.1

Steps to reproduce Using a FileReference Object as src parameter leads to an error: Invalid file src provided, must be either a uid, combined identifier, path/filename or implement FileInterface. ERROR: 1536560752 in ConversionService. The src parameter is handled there just for FileInterface and source path.

Expected behavior Using the image referenced by FileReference like written in the docs: This ViewHelper renders an image given as src. As src argument you may provide a valid TYPO3 path or an object implementing TYPO3 FAL FileInterface (e.g. File or FileReference). Or I have not understood something correctly.

Possible solution Extend function convertFileSrcToFileObject like this - not fully tested

if ($src instanceof FileInterface) {
    $file = $src;
} else if ($src instanceof \TYPO3\CMS\Extbase\Domain\Model\FileReference  ) {
    $file = $src->getOriginalResource()->getOriginalFile();
} else {
    try {
        $file = $this->resourceFactory->retrieveFileOrFolderObject($src);
    } catch (\Exception $e) {
        //invalid file provided
        $previousException = $e;
        $previousExceptionMessage = ' ' . $e->getMessage();
    }
}
maechler commented 1 year ago

@thorstenBorn-cosmoblonde Thanks for this very nice bug report! I agree, the docs are not specific enough, unfortunately there are two FileReference classes:

  1. TYPO3\CMS\Core\Resource\FileReference -> implements FileInterface
  2. TYPO3\CMS\Extbase\Domain\Model\FileReference -> does not implement FileInterface

However I think it would be a nice addition to seamlessly support Extbase FileReference, so I will add support for it. In the meantime you could easily workaround this by passing the originalResource, which already implements the FileInterface, to the image ViewHelper. We are doing this in the template shipped for EXT:news, please see:

https://github.com/bithost-gmbh/pdfviewhelpers/blob/2aec3a80fc2b0f3cd649d20c3ab5469a7a7523f2/Resources/Private/Templates/Extensions/News/Templates/News/Detail.html#L27