Skip to content

Instantly share code, notes, and snippets.

@sorenmalling
Last active December 19, 2015 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sorenmalling/5975542 to your computer and use it in GitHub Desktop.
Save sorenmalling/5975542 to your computer and use it in GitHub Desktop.
function processDatamap_postProcessFieldArray ($status, $table, $id, &$fieldArray, $parentObject) {
if($table == 'sys_file_collection' && in_array($status, array('update', 'new'))) {
if(array_key_exists('uploadfile', $fieldArray)) {
$uploadFolder = $GLOBALS['TCA'][$table]['columns']['uploadfile']['config']['uploadfolder'];
$uploadedImages = explode(',', $fieldArray['uploadfile']);
unset($fieldArray['uploadfile']); // Don't want it to get persistsed as it serves a dummy
/** @var $fileCollectionRepository TYPO3\CMS\Core\Resource\FileCollectionRepository */
$fileCollectionRepository = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\FileCollectionRepository');
$fileCollectionObject = $fileCollectionRepository->findByUid($id);
$fileCollectionObject->loadContents();
/** @var $resourceFactory \TYPO3\CMS\Core\Resource\ResourceFactory */
$resourceFactory = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\ResourceFactory');
/** @var $indexerService \TYPO3\CMS\Core\Resource\Service\IndexerService */
$indexerService = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\Service\\IndexerService');
$indexerService->setFactory($resourceFactory);
// run indexing of every storage
$storageRecord = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow('*', 'sys_file_storage', 'deleted = 0 AND uid = 4');
foreach ($uploadedImages as $image) {
$fileObject = $resourceFactory->getFileObjectFromCombinedIdentifier('4:' . $image); #retrieveFileOrFolderObject($uploadFolder . DIRECTORY_SEPARATOR . $image);
$indexedFileObject = $indexerService->indexFile($fileObject);
$fileCollectionObject->add($indexedFileObject);
}
$fileCollectionObject->persist(); // Nothing is persisted, no file objects are saved to the collection
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment