Search code examples
uploadtypo3extbasefal

TYPO3 FAL upload frontend add title, alternative and description


I have an extension where user can upload in frontend some images to their frontend-profile. Image upload and saving to user-profile works fine, I user following code - but is there a simple way to add title, alternative and description too?

Thanks for help! Martin

/** @var \TYPO3\CMS\Core\Resource\StorageRepository $storageRepository */
        $storageRepository = $this->objectManager->get('TYPO3\CMS\Core\Resource\StorageRepository');
        /** @var \TYPO3\CMS\Core\Resource\ResourceStorage $storage */
        $storage = $storageRepository->findByUid('1');

        #$storageRepository = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\StorageRepository');
        #$storage           = $storageRepository->findByUid(1); # Fileadmin = 1
        #$saveFolder        = $storage->getFolder($this->settings['uploadFolder']);

        $fileData = array();
        $fileData['name'] = $_FILES['tx_key_datenwartung']['name']['logo'][0];
        $fileData['type'] = $_FILES['tx_key_datenwartung']['type']['logo'][0];
        $fileData['tmp_name'] = $_FILES['tx_key_datenwartung']['tmp_name']['logo'][0];
        $fileData['size'] = $_FILES['tx_key_datenwartung']['size']['logo'][0];


        // this will already handle the moving of the file to the storage:
        $newFileObject = $storage->addFile(
                $fileData['tmp_name'], $saveFolder, $userUid.'_'.$fileData['name']
        );
        $newFileObject = $storage->getFile($newFileObject->getIdentifier());
        #$newFile = $this->fileRepository->findByUid($newFileObject->getProperty('uid'));

        $newFile = $newFileObject->getUid();

        $persistenceManager = $this->objectManager->get('TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager');
        $persistenceManager->persistAll();

        /** @var \Vendor\Myextension\Domain\Model\FileReference $newFileReference */
        $newFileReference = $this->objectManager->get('Ven\Key\Domain\Model\FileReference');
        $newFileReference->setFile($newFile);


        $user->addLogo($newFileReference);

Solution

  • .... could solve it myself. just add setter to model and add texts:

    /**
     * Set alternative
     *
     * @param \xx\yy\Domain\Model\File $file
     */
    public function setAlternative($alternative) {
        $this->alternative = $alternative;
    }
    
    and to controller:

    $newFileReference->setAlternative('your alternative text for image');