Skip to content

Instantly share code, notes, and snippets.

@Schweriner
Created August 18, 2021 11:01
Show Gist options
  • Save Schweriner/3e7e150fb3340d9fb3adcbca853bca6c to your computer and use it in GitHub Desktop.
Save Schweriner/3e7e150fb3340d9fb3adcbca853bca6c to your computer and use it in GitHub Desktop.
Migrate DCE Image Field to FAL
/** @var \TYPO3\CMS\Core\Database\Query\QueryBuilder $queryBuilder */
$queryBuilder = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ConnectionPool::class)
->getQueryBuilderForTable('tt_content');
$elements = $queryBuilder->select('*')->from('tt_content')
->where(
$queryBuilder->expr()->andX(
$queryBuilder->expr()->eq('CType', $queryBuilder->createNamedParameter('dce_dceuid1')),
$queryBuilder->expr()->eq('deleted', $queryBuilder->createNamedParameter('0'))
)
)->execute()->fetchAll();
if($elements) {
/** @var \TYPO3\CMS\Core\Service\FlexFormService $flexFormUtility */
$flexFormUtility = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Service\FlexFormService::class);
/** @var \TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools $flexFormTools */
$flexFormTools = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools::class);
/** @var \TYPO3\CMS\Core\Resource\ResourceFactory $resourceFactory */
$resourceFactory = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\ResourceFactory::class);
$storage = $resourceFactory->getStorageObject(11);
/** @var DataHandler $dataHandler */
$dataHandler = GeneralUtility::makeInstance(DataHandler::class);
foreach ($elements as $element) {
$flexform = $flexFormUtility->convertFlexFormContentToArray($element['pi_flexform']);
if(true === isset($flexform['settings']['image'])) {
if(true === $storage->hasFile('/' . $flexform['settings']['image'])) {
$file = $storage->getFile('/' . $flexform['settings']['image']);
$newFlexForm = GeneralUtility::xml2array($element['pi_flexform']);
$newFlexForm['data']['sheet.tabGeneral']['lDEF']['settings.dceimage'] = [
'vDEF' => 1
];
$newFlexForm = $flexFormTools->flexArray2Xml($newFlexForm);
$newId = 'NEW1234';
$data = array();
$data['sys_file_reference'][$newId] = array(
'table_local' => 'sys_file',
'uid_local' => $file->getUid(),
'tablenames' => 'tt_content',
'uid_foreign' => $element['uid'],
'fieldname' => 'dceimage',
'pid' => $element['pid']
);
$queryBuilder->update('tt_content')
->set('pi_flexform', $newFlexForm)
->where(
$queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($element['uid']))
)->execute();
$dataHandler->start($data, array());
$dataHandler->process_datamap();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment