[TYPO3-dev] Import Extension add media files
Jigal van Hemert
jigal.van.hemert at typo3.org
Sun May 31 14:09:26 CEST 2015
Hi,
On 31/05/2015 11:33, DirkHo wrote:
> I'm developing a content importer for data from my old website to use it
> in typo3. Therefore I wanted to upload related files (PDFs, GIFs, JPEGs,
> e.g.).
>
> Could you please give me a hint, which classes to use from Typo3 to
> upload these files to Typo3 and add them as media file to the related
> content element?
A few snippets as inspiration (not completely tested, may contain small
errors):
First copy file to final destination in fileadmin and import it into FAL:
// copy using absolute path and filenames
copy($sourceFile, $targetFile);
// calculate path and filename relative to site
$identifier = PathUtility::getRelativePath(PATH_site,
PathUtility::dirname($targetFile)) . PathUtility::basename($targetFile);
/** @var \TYPO3\CMS\Core\Resource\File $fileObject */
$fileObject =
ResourceFactory::getInstance()->retrieveFileOrFolderObject($identifier);
$newRecordId = $fileObject->getUid();
The file is now known in FAL and we have the UID of the FAL record for
further use.
If you want to add it as an image to a text w/image content element and
it's a new content element you can use something like:
$recordData = array(
// page to place content element
'pid' => 55,
'colPos' => 2,
'title' => 'title of content',
'bodytext' => 'text of content element',
// $newRecordId see earlier snippet
'image' => $newRecordId;
);
$newElementIdMarker = uniqid('NEW');
$dataMap = array(
'tt_content' => array(
$newElementIdMarker => $recordData
),
);
/** @var \TYPO3\CMS\Core\DataHandling\DataHandler $tce */
$tce =
GeneralUtility::makeInstance('\\TYPO3\\CMS\\Core\\DataHandling\\DataHandler');
$tce->start($data, array());
$tce->process_datamap();
$newContenElementId = $tce->substNEWwithIDs[$newElementIdMarker];
For more information on what you can do with DataHandler (in 4.x known
as TCEmain) see:
- http://blog.tolleiv.de/2010/03/handling-data-in-typo3-with-tcemain/
-
http://docs.typo3.org/typo3cms/CoreApiReference/ApiOverview/Typo3CoreEngine/Index.html
and following pages.
At first DataHandler seems a bit awkward or complicated, but you'll save
yourself a lot of work and trouble by using it. DataHandler will
automatically fill / update fields like crdata, tstamp; it will
automatically create the records in the intermediate tables (mm-tables,
file references) based on the definitions in TCA (you give it a comma
separated list of UIDs); it will log operations in system log, history
tables; extensions which hook into DataHandler (e.g. EXT:solr) will be
notified of content changes and can take actions; caches are cleared
when needed and so on.
If you need to store HTML formatted content in an RTE field you need
some further code to prepare it as if an editor has entered it in the
backend.
Good luck with your import!
--
Jigal van Hemert
TYPO3 CMS Active Contributor
TYPO3 .... inspiring people to share!
Get involved: typo3.org
More information about the TYPO3-dev
mailing list