typo3 8: FileReference aus Bild erzeugen

15. Januar 2018

Ausgangssituation: in einem sehr alten Typo3 existierten die Bilder der tx_members im Folder uploads/tx_members/ und waren in der Datenbank nur als Dateiname (logo_alt) gespeichert. Um daraus dann FileReference Objekte zu erzeugen, habe ich folgendes gemacht.

1. FileReference angelegt

  1. class FileReference extends \TYPO3\CMS\Extbase\Domain\Model\AbstractFileFolder {
  2. /**
  3. * Uid of the referenced sys_file. Needed for extbase to serialize the
  4. * reference correctly.
  5. *
  6. * @var int
  7. */
  8. protected $uidLocal;
  9. /**
  10. * @var string
  11. */
  12. protected $tablenames = 'tx_vendorext_domain_model_member';
  13. /**
  14. * @param \TYPO3\CMS\Core\Resource\ResourceInterface $originalResource
  15. */
  16. public function setOriginalResource(\TYPO3\CMS\Core\Resource\ResourceInterface $originalResource) {
  17. $this->originalResource = $originalResource;
  18. $this->uidLocal = (int)$originalResource->getUid();
  19. }
  20. /**
  21. * @return \TYPO3\CMS\Core\Resource\FileReference
  22. */
  23. public function getOriginalResource() {
  24. if($this->originalResource === NULL) {
  25. $this->originalResource = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->getFileReferenceObject(
  26. $this->getUid()
  27. );
  28. }
  29. return $this->originalResource;
  30. }
  31. /**
  32. * @return string
  33. */
  34. public function getTablenames() {
  35. return $this->tablenames;
  36. }
  37. /**
  38. * @param string $tablenames
  39. */
  40. public function setTablenames($tablenames) {
  41. $this->tablenames = $tablenames;
  42. }
  43. /**
  44. * setFile
  45. *
  46. * @param \TYPO3\CMS\Core\Resource\File $falFile
  47. * @return void
  48. */
  49. public function setFile(\TYPO3\CMS\Core\Resource\File $falFile) {
  50. $this->originalFileIdentifier = (int)$falFile->getUid();
  51. }
  52. }

2. mapping in ext_typoscript_setup.txt festgelegt:

  1. config.tx_extbase {
  2. persistence{
  3. classes {
  4. Vendor\Ext\Domain\Model\FileReference {
  5. mapping {
  6. tableName = sys_file_reference
  7. columns {
  8. uid_local.mapOnProperty = originalFileIdentifier
  9. }
  10. }
  11. }
  12. }
  13. }
  14. }

3. im Controller einmal alles durchlaufen und neu in “logo” gespeichert:

  1. private function setLogos(){
  2. $storageRepository = $this->objectManager->get('TYPO3\\CMS\\Core\\Resource\\StorageRepository');
  3. $storage = $storageRepository->findByUid('1');
  4. $targetFolder = $storage->getFolder('tx_members/');
  5. $persistenceManager = $this->objectManager->get('TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager');
  6. $members = $this->memberRepository->findAll();
  7. foreach($members as $member){
  8. $logoAlt = $member->getLogoAlt();
  9. if(!$member->getLogo() && $logoAlt && file_exists(PATH_site.'uploads/tx_members/'.$logoAlt)) {
  10. $file = $storage->addFile('uploads/tx_members/' . $logoAlt, $targetFolder, $logoAlt, 'replace', false);
  11. $newFileReference = $this->objectManager->get('Vendor\\Ext\\Domain\\Model\\FileReference');
  12. $newFileReference->setFile($file);
  13. $member->setLogo($newFileReference);
  14. $this->memberRepository->update($member);
  15. $persistenceManager->persistAll();
  16. }
  17. }
  18. }