Home > database >  TYPO3 : how to get files/images based on sys_category
TYPO3 : how to get files/images based on sys_category

Time:09-02

My sys_categories have some files/images like this :

enter image description here

I tried to display them but I only can get the first one, I tried to loop to display all of them like this :

<f:if condition="{uid.images}">
     <f:for each="{uid.images}" as="files">
           <a href="{files.originalResource.publicUrl}">{files.originalResource.name}</a>
     /f:for>
</f:if>

But I get this error message :

(1/1) #1256475113 InvalidArgumentException
The argument "each" was registered with type "array", but is of type "TYPO3\CMS\Extbase\Domain\Model\FileReference" in view helper "TYPO3Fluid\Fluid\ViewHelpers\ForViewHelper".

there is a dump of {uid} variable :

enter image description here

In database I see correctly "2" on "images"

I don't know what mistake I did, there is an issue from my loop ? Or there is another way to display multiples files/images ?

Thanks for help

CodePudding user response:

In your Model you have defined "images" of type FileReference. This means "one" reference/file.

You should refactor this to an ObjectStorage of file references.

/**
 * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference>
 */
protected $images;

public function initializeObject()
{
    parent::initializeObject();

    $this->images    = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
}

/**
 * @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference>
 */
public function getImages(): \TYPO3\CMS\Extbase\Persistence\ObjectStorage
{
    return $this->images;
}

/**
 * @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference> $productVideos
 */
public function setImages(\TYPO3\CMS\Extbase\Persistence\ObjectStorage $images): void
{
    $this->images = $images;
}
  • Related