vhs icon indicating copy to clipboard operation
vhs copied to clipboard

v:resource.image and v:resource.file leads to error (undefined method getCategoryRelationTableName) - with FIX

Open woodyc79 opened this issue 6 years ago • 2 comments

Hello!

I try to collect images from a category with <v:resource.image categories="{settings.categories}" as="imageresources"> ....... </v:resource.image> and I get this error: Call to undefined method FluidTYPO3\Vhs\ViewHelpers\Resource\ImageViewHelper::getCategoryRelationTableName()

This is caused in vhs/Classes/ViewHelpers/Resource/AbstractResourceViewHelper.php: image

The Method getCategoryRelationTableName ist not here any more - how can I fix that? Or which other possibilities do i have to collect the images of a category?

Thank you! Kind regards, Chris

woodyc79 avatar Jan 09 '18 11:01 woodyc79

OK I found the problem: I think there should be called the function $this->getTablenameForSystemConfiguration, not $this->getCategoryRelationTableName

woodyc79 avatar Jan 09 '18 11:01 woodyc79

Unfortunately this was still not working (TYPO3 8). Why? If the extension "Advanced file metadata" (filemetadata) is installed, the function $this->getTablenameForSystemConfiguration() returns the tablename of sys_file_metadata. (Otherwise it returns sys_files).

If this is happening, in function getFiles we will get a result-array with the UIDs (uid_foreign) of the table sys_file_metadata (not sys_file). But vhs/Classes/ViewHelpers/Resource/AbstractResourceViewHelper.php expects an an array with sys_files UIDs (because it's using it with $file = $resourceFactory->getFileObject($fileUid))!

Currently a v:resource.file or v:resource.image call with a categories parameter always returns back the WRONG files.

So if we use sys_file_metadata, we have to get the UIDs of it's associated sys_file.

To look if my presumptions are right, I created a (quick & dirty) function that gets the sys_file_metadata UIDs and returns it's sys_file UIDs: ` /** * @param array $afileUids * gets an array of file UIDs (sys_files) from an array of Reference UIDs (sys_file_reference) */ private function getFileUidsFromFileReferences($aFileUids) { if (is_array($aFileUids)) { $aFileUids = $aFileUids; } elseif (true === is_string($aFileUids)) { $aFileUids = GeneralUtility::trimExplode(',', $aFileUids, true); } else { $aFileUids = (array) $aFileUids; }

    $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
	    'file',
	    $this->getTablenameForSystemConfiguration(),
	    sprintf(
		    'uid IN (%s)',
		    implode(",",$aFileUids)
	    )
    );
    
    $fileUids = array_unique(array_column($rows, 'file'));

    return $fileUids;
}

`

And this is the corrected SQL Query with conversion of sys_file_reference UIDs to sys_file UIDs: ` public function getFiles($onlyProperties = false, $identifier = null, $categories = null) { ...... ......

 if (false === empty($categories)) {
        $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
            'uid_foreign',
            'sys_category_record_mm',
            sprintf(
                'tablenames = \'%s\' AND uid_local IN (%s)',
                $this->getTablenameForSystemConfiguration(), //fixed wrong method name 
                implode(',', $GLOBALS['TYPO3_DB']->fullQuoteArray($categories, 'sys_category_record_mm'))
            )
        );

        $fileUids = array_unique(array_column($rows, 'uid_foreign'));
        //Now FIX UIDs if they are sys_file_metadata UIDs
        if($this->getTablenameForSystemConfiguration()=='sys_file_metadata')
               $fileUids=$this->getFileUidsFromFileReferences($fileUids);
        
        if (true === empty($identifier)) {
            foreach ($fileUids as $fileUid) {
        ......
        ......

`

Now everything is working - this returns the correct files again!

I hope, I could help.

Kind regards, Chris

woodyc79 avatar Jan 09 '18 23:01 woodyc79