Reputation: 2243
I'm trying to update record having FAL image. Here is my update action,
$obj = $this->mediaAlbumRepository->findByUid(GeneralUtility::_GP('album'));
$obj->setTitle(GeneralUtility::_GP('title'));
$obj->setDescription(GeneralUtility::_GP('description'));
$obj->setWebdescription(GeneralUtility::_GP('webdescription'));
$obj->setType(GeneralUtility::_GP('type'));
$obj->setDatetime(new \DateTime());
$obj->setGalleryOwner($this->user['uid']);
$assetsMetaData = GeneralUtility::_GP('imgMetaData');
$utilityObj = GeneralUtility::makeInstance(UploadAssetsProviderUtility::class);
$fileObj = $utilityObj->processAssets($_FILES, $assetsMetaData, $uploadFolderId);
$fileObjStorage = GeneralUtility::makeInstance(ObjectStorage::class);
foreach($fileObj as $item) {
$fileObjStorage->attach($item);
}
if(GeneralUtility::_GP('type') == 'static') {
$obj->setFiles($fileObjStorage);
}
$this->mediaAlbumRepository->update($obj);
UploadAssetsProviderUtility.php
I have below function which convert $_FILE to Filereference object with utility class.
/**
* Create file reference object
*
* @param UploadedFile $fileImage
* @param Folder $uploadFolder
* @param String $uploadFolderId
* @param Array $metainfo
* @return FileReference $fileReference
*/
protected function uploadFile($fileImage, $uploadFolder, $uploadFolderId, $fileMeta = []): FileReference
{
[$storageId, $storagePath] = explode(':', $uploadFolderId, 2);
$storage = $this->resourceFactory->getStorageObject($storageId);
$fileReference = GeneralUtility::makeInstance(
FileReference::class,
$assetProp,
$this->resourceFactory
);
if(is_array($fileImage)) {
$file = $storage->addFile(
$fileImage['tmp_name'],
$uploadFolder,
$fileImage['name']
);
$assetProp = [
'uid_local' => $file->getUid(),
'uid_foreign' => StringUtility::getUniqueId('NEW_'),
'uid' => StringUtility::getUniqueId('NEW_'),
'crop' => null,
];
$metaTitle = $fileImage['falMetaData']['title'] ? $fileImage['falMetaData']['title'] : $fileImage['name'];
$metaAlt = $fileImage['falMetaData']['alt'] ? $fileImage['falMetaData']['alt'] : $fileImage['name'];
$metaLink = $fileImage['falMetaData']['link'] ? $fileImage['falMetaData']['link'] : '';
$metaDescription = $fileImage['falMetaData']['description'] ? $fileImage['falMetaData']['description'] : '';
$fileReference->setOriginalResource($file);
} else {
// here $metainfo->uid has existing Filereference UID if already exist and find Filereference object
$fileImage = $this->resourceFactory->getFileReferenceObject($metainfo->uid);
$properies = $fileImage->getReferenceProperties();
$metaTitle = $fileMeta['falMetaData']['title'] ? $fileMeta['falMetaData']['title'] : $fileMeta['name'];
$metaAlt = $fileMeta['falMetaData']['alt'] ? $fileMeta['falMetaData']['alt'] : $fileMeta['name'];
$metaLink = $fileMeta['falMetaData']['link'] ? $fileMeta['falMetaData']['link'] : '';
$metaDescription = $fileMeta['falMetaData']['description'] ? $fileMeta['falMetaData']['description'] : '';
$fileReference->setUidLocal($properies['uid_local']);
}
$fileReference->setTitle($metaTitle);
$fileReference->setAlternative($metaAlt);
$fileReference->setDescription($metaDescription);
$fileReference->setLink($metaLink);
$fileReference->setTableLocal('sys_file');
$fileReference->setTablenames($this->foreignTable);
return $fileReference;
}
Here if you check condition if(is_array($fileImage)) {
to check its new file ($_FILE). Now, if its new, it working properly and return Filereference object and store to table.
Now, I am having issue with update. When I tried to update file, it create another Filereference object instead of updating existing one.
Now, is there any way to update FAL meta property if Filereference exist. If its not exist then create new one.
Upvotes: 0
Views: 249
Reputation: 2683
First of all in your TYPO3 Version you can avoid going through the $_FILES
global and instead checkout $this->request->getUploadedFiles();
inside a Extbase-Controller, otherwise you will find them inside $GLOBALS['TYPO3_REQUEST']
. Its much easier to handle the images from here than using the mentioned global variable.
But at all: Inside the request will only be informations about the uploaded file in case that actually a real upload happened. So if there are no uploads you can be sure that any other submitted informations from a Fluid-Form are properties of already existing relations which Extbase should handle by itself. Lets say you have a object "Car" inside your <f:form>
and its contains images inside the property "carPictures" you can iterate over all existing images and add form fields regarding their Reference properties by targeting those informations with the "property" attribute of each form field.
So iterate through all the existing images within a <f:for each...>
and adress theire properties like <f:form.textfield property="carPictures.{fileReference.uid}.title" />
then add an additional hidden field to each image like <f:form.hidden property="carPictures.{fileReference.uid}" value="{fileReference}" />
by this way extbase will notice by itself which attribute change is belonging to which existing image, it will keep them and also update the attributes.
Upvotes: 0