#88833Extend possibility to create FileReference in frontend with a given File object
Description
Related to https://forge.typo3.org/issues/48965#note-21 it is still not possible, to create a FileReference in frontend context by a given File object. So still a custom extended FileReference class is neccessary. Please add a setFile() method to the native Extbase FileReference class like this:
/**
* Class FileReference
*/
class FileReference extends \TYPO3\CMS\Extbase\Domain\Model\FileReference {
/**
* uid of a sys_file
*
* @var integer
*/
protected $originalFileIdentifier;
/**
* setOriginalResource
*
* @param \TYPO3\CMS\Core\Resource\ResourceInterface $originalResource
* @return void
*/
public function setOriginalResource(\TYPO3\CMS\Core\Resource\ResourceInterface $originalResource) {
$this->originalResource = $originalResource;
$this->originalFileIdentifier = (int)$originalResource->getOriginalFile()->getUid();
}
/**
* setFile
*
* @param \TYPO3\CMS\Core\Resource\File $falFile
* @return void
*/
public function setFile(\TYPO3\CMS\Core\Resource\File $falFile) {
$this->originalFileIdentifier = (int)$falFile->getUid();
}
}
With this, creating a new FileReference in frontend will be possible by core for all three variants like following:
// A) File Objekt already exists in Storage(but Storage doesn't know it yet):
$file = ResourceFactory::getInstance()->retrieveFileOrFolderObject('path_to_file');
// B) File Objekt exists anywhere and will be copied to storage:
$storage = ResourceFactory::getInstance()->getDefaultStorage();
$file = $storage->getFolder('folder_name')->addFile('path_to_file');
// C) File Objekt doesn't yet existan will be created as an empty file (can be filled later using setContents() or else):
$storage = ResourceFactory::getInstance()->getDefaultStorage();
$file = $storage->getFolder('folder_name')->createFile('path_to_file', $folder);
// Create the file reference itself here (for A, B, and C)
$fileReference = $objectManager->get(\TYPO3\CMS\Extbase\Domain\Model\FileReference::class);
$fileReference->setFile($file); Reviews 1
Related issues 3
History 2535
Show 810 earlier messages
-
Christian Eßl changed Related to #66813 <span data-processor="relativetime" data-value="2019-09-24T18:49:44+00:00" > 09/24/19, 18:49 </span>
-
Christian Eßl changed Related to #90374 <span data-processor="relativetime" data-value="2020-02-16T13:23:22+00:00" > 02/16/20, 13:23 </span>
-
Jan Kornblum changed Related to #48965 <span data-processor="relativetime" data-value="2020-11-12T09:33:37+00:00" > 11/12/20, 09:33 </span>
-
Jan Kornblum commented and changed category <span data-processor="relativetime" data-value="2020-11-12T09:48:48+00:00" > 11/12/20, 09:48 </span>
Nobody interested in improving this? Since FAL has already been introduced in 6 LTS (?) there is still no native support for such basic tasks? I’m sorry if this is not as simple as it seems to me (i am not a core developer)…
If anybody will implement this missing feature, please also respect this (https://forge.typo3.org/issues/66813#note-3) related issue, to prevent the need to extend the TCA’s “foreign_match_fields” always.
-
Stefan Neufeind commented <span data-processor="relativetime" data-value="2021-07-21T09:26:04+00:00" > 07/21/21, 09:26 </span>
Stumbled across this again. We really should find a way to easily create a filereference – either by using a file-object and being able to create a filereference from there or like described here by creating a new reference and assigning it a file.
Use-case is for example: You have a FrontendUser-object, have an image in the filesystem (and can easily request a file-object for it) … and need to call FrontendUser::setImage() with an ObjectStorage holding a file-reference.
Maybe we could also allow to pass a file-object there and setImage would itself create the needed FileReference out of it.Blogpost (in German) refering to this issue here:
https://www.typo3tiger.de/blog/post/extbase-fal-filereference-erzeugen-update.html -
Jan Kornblum commented <span data-processor="relativetime" data-value="2023-03-04T15:25:30+00:00" > 03/04/23, 15:25 </span>
Update dead link “Blogpost (in German) refering to this issue here”:
https://buergel.dev/blog/post/extbase-fal-filereference-erzeugen-update.html
-
Gerrit Code Review pushed patch set 2 for main to change 78166 status
NewUnder Review <span data-processor="relativetime" data-value="2023-03-17T23:52:52+00:00" > 03/17/23, 23:52 </span> -
JAKOTA Design Group GmbH commented <span data-processor="relativetime" data-value="2023-03-18T00:07:09+00:00" > 03/18/23, 00:07 </span>
As requested, I looked into this issue and it seems that only the file setter is missing.
But I have to test this with a real project, which I haven’t done yet.
So please go ahead, if you have some time on your hand, and try this yourself.
-
Gerrit Code Review pushed patch set 3 for main to change 78166 <span data-processor="relativetime" data-value="2024-07-14T21:15:02+00:00" > 07/14/24, 21:15 </span>
-
Gerrit Code Review pushed patch set 4 for main to change 78166 <span data-processor="relativetime" data-value="2024-07-14T21:17:16+00:00" > 07/14/24, 21:17 </span>
-
Jan Kornblum commented <span data-processor="relativetime" data-value="2024-07-14T21:33:56+00:00" > 07/14/24, 21:33 </span>
I’ve adjusted this for current master and tested it in a real project.
-
JAKOTA Design Group GmbH commented <span data-processor="relativetime" data-value="2024-07-15T11:40:15+00:00" > 07/15/24, 11:40 </span>
Thanks for the update. I’ve also tested this in one of my projects. And it works as expected.
lets see what the what the devs have to say about it.
this could be seen as a bugfix or new feature and depending on this classification more work needs to be done. -
Gerrit Code Review pushed patch set 5 for main to change 78166 <span data-processor="relativetime" data-value="2024-07-15T12:20:27+00:00" > 07/15/24, 12:20 </span>
-
Gerrit Code Review pushed patch set 6 for main to change 78166 <span data-processor="relativetime" data-value="2024-07-15T12:22:47+00:00" > 07/15/24, 12:22 </span>
-
Gerrit Code Review pushed patch set 7 for main to change 78166 <span data-processor="relativetime" data-value="2024-07-15T16:02:21+00:00" > 07/15/24, 16:02 </span>
-
Gerrit Code Review pushed patch set 8 for main to change 78166 <span data-processor="relativetime" data-value="2024-10-18T13:38:19+00:00" > 10/18/24, 13:38 </span>
-
JAKOTA Design Group GmbH commented <span data-processor="relativetime" data-value="2025-03-19T13:59:07+00:00" > 03/19/25, 13:59 </span>
This, and all the related issues, should be closed as won’t fixed.
-
Jan Kornblum commented <span data-processor="relativetime" data-value="2025-03-19T14:26:49+00:00" > 03/19/25, 14:26 </span>
I disagree, because it is not (only) related to file uploads. So this issue should stay opened and (hopefully) be fixed.
-
Gerrit Code Review pushed patch set 9 for main to change 78166 <span data-processor="relativetime" data-value="2025-03-19T15:08:50+00:00" > 03/19/25, 15:08 </span>
-
JAKOTA Design Group GmbH commented <span data-processor="relativetime" data-value="2025-03-19T15:19:11+00:00" > 03/19/25, 15:19 </span>
I’ve updated the patch and we will see what comes of it.
-
Stefan Neufeind commented <span data-processor="relativetime" data-value="2025-07-07T12:24:34+00:00" > 07/07/25, 12:24 </span>
Just stumbled across this again. There are some disagreements in the review given. But it would be really great to find a final solution for this.
Tested what is in review currently and it helped solve my issue. The old solution from buergel.dev didn’t work on TYPO3 v11+ anymore it seems. -
Gerrit Code Review pushed patch set 10 for main to change 78166 <span data-processor="relativetime" data-value="2025-07-07T14:19:50+00:00" > 07/07/25, 14:19 </span>
-
JAKOTA Design Group GmbH commented <span data-processor="relativetime" data-value="2025-07-07T14:37:08+00:00" > 07/07/25, 14:37 </span>
I hope I did what was asked of me. :) lets see what the dev’s have to say about my new patch.
-
Gerrit Code Review pushed patch set 11 for main to change 78166 <span data-processor="relativetime" data-value="2025-09-29T20:05:11+00:00" > 09/29/25, 20:05 </span>
-
Garvin Hicking commented and changed status
Under ReviewClosed <span data-processor="relativetime" data-value="2026-02-27T09:48:45+00:00" > 02/27/26, 09:48 </span>We’ve added a comment in the patch to indicate why we believe this code is better maintained inside a project than the TYPO3 core. I hope that’s ok and understandable.
-
Jan Kornblum commented <span data-processor="relativetime" data-value="2026-03-02T13:35:37+00:00" > 03/02/26, 13:35 </span>
Can you please reopen this issue? It is now 2026 and there is still no way to create a file reference based on an existing file without manual work. So this is not an exotic use case. Extbase FAL support is still incomplete here, and that is not really a permanent solution.
-
Garvin Hicking commented and changed status
ClosedNew priorityShould haveCould have <span data-processor="relativetime" data-value="2026-03-02T13:43:02+00:00" > 03/02/26, 13:43 </span>I’m reopening this. I am unsure though how/if to solve this, because even a shorthand notation factory would need project-specific implementations, where a project could easily insert a factory with this with just a few lines of custom code:
$fileReference = GeneralUtility::makeInstance(FileReference::class); $fileReference->setOriginalResource( $this->resourceFactory->createFileReferenceObject([ 'uid_local' => $originalFile->getUid(), 'uid_foreign' => StringUtility::getUniqueId('NEW'), 'uid' => StringUtility::getUniqueId('NEW'), 'crop' => (string)$cropVariantCollection, ]) );Another problem is that we have few of these “shorthand” factories (however, a few are now getting addressed), so we should align this in the whole of the TYPO3 code base.
-
Jan Kornblum commented <span data-processor="relativetime" data-value="2026-03-05T10:11:09+00:00" > 03/05/26, 10:11 </span>
Regarding the project specific implementations, couldn’t an optional third parameter “$fileReferenceClassName” (defaults to core file reference) be added to the factory method which allows creation of local file references by passing different classes?
createFileReferenceFromOriginalFile(File $originalFile, ?CropVariantCollection $cropVariantCollection = null, string $fileReferenceClassName = FileReference::class): FileReference -
Garvin Hicking commented <span data-processor="relativetime" data-value="2026-03-05T10:28:56+00:00" > 03/05/26, 10:28 </span>
Passing strings as classnames and returning generic classes is not so great. In that case, (Symfony) autoconfigure/autowire/autotagging should be utilized, and then proper interface and such should be taken into consideration. I believe such a “makeInstance()” like feature hurts us in the long run.
Utilizing that autoconfigure/autowire and the likes also requires project-side specification. So we would exchange a 5-line length project-side implementation with a generic one that needs 5 lines of configuration…. ;)
-
Jan Kornblum commented <span data-processor="relativetime" data-value="2026-03-05T10:42:24+00:00" > 03/05/26, 10:42 </span>
OK. And what about the origin idea about adding a setFile() setter to the extbase reference?
-
Stefan Bürk commented <span data-processor="relativetime" data-value="2026-03-05T11:13:14+00:00" > 03/05/26, 11:13 </span>
The concern was not only the “class” name but more about additional data.
For example, if you want to set additional “sys_file_reference” data. The propsed change makes the crop variant stave as a object and it has been discussed that adding a non strict typed array for additional data should be avoided.
On top of that, extensions and projects tends to extend the sys_file_reference table with further additional fields which are not usable with that short hand method.
Starting thinking in this directetion the method signature becomes a beast and a maintenance burden, extending not really coverd or falling back to the “array” to put abinary data in. And in that case we can simply leave the crop stuff already out and simple have a “additionalReferenceData” array and merge it for the inner call
to have a small footprint method.Having a unflexible wrapping / factory method does not make sense if it only targes a minor group and the benefit simply blows away and projects/extensions needs to write it anyway manually.
The code/api is simply enough that discussiong a signature and handling to cover majority use-cases in one go ending up in passing a array does not make much sense in my eyes because it is simple enough to do it in a project if required.
Wrapping methods around methods around methods is a bad practice or leads to “multiple” outer methods for something which can be done with a simple 5 line code instead of having a maintenance burden.
If we would start with the proposed mfactory/method we will up getting reports to handle additional stuff and it will get more and more harder to do additions in a non-breaking way and blow up the method to a monster handling all the different things and in the end not having somethign for custom extended table data in place.
-
Stefan Bürk commented <span data-processor="relativetime" data-value="2026-03-05T11:16:15+00:00" > 03/05/26, 11:16 </span>
Jan Kornblum wrote in #note-31:
> OK. And what about the origin idea about adding a setFile() setter to the extbase reference?That’s literally `setOriginalResource()` already. Having there a outer method to create the refernce internally leads to the same discussion to have a suiting signature for not only the simple crop argument handling to pass it to the create ference object.
Creating a referne is dassn by the resource factory.
On the other hand, a entity/model/dto should not have services injected to do stuff (buisnuis logic) which counters design principles and making the extbase model a service itself.
-
JAKOTA Design Group GmbH commented <span data-processor="relativetime" data-value="2026-09-08T08:55:50+00:00" > 09/08/26, 08:55 </span>
Hi,
Having something like:
public function createFileReferenceFromOriginalFile(File $originalFile, array $fileReferenceData = []): FileReference { $fileReference = GeneralUtility::makeInstance(FileReference::class); $fileReference->setOriginalResource( $this->resourceFactory->createFileReferenceObject(array_merge($fileReferenceData, [ 'uid_local' => $file->getUid(), 'uid_foreign' => StringUtility::getUniqueId('NEW'), 'uid' => StringUtility::getUniqueId('NEW'), ])) ); return $fileReference; }in the T3 Core would help me out immensely.
This way it is just the basics but can be easily extended.Or maybe this can life in a FileUtility or something.
Just a thought, because I had to add something like this to a project again.
-
Georg Ringer commented and changed status
NewClosed <span data-processor="relativetime" data-value="2026-11-14T19:50:03+00:00" > 11/14/26, 19:50 </span>Hi,
thanks a lot for the report, and to everyone who tested and reworked the patch over the years. Your effort is much appreciated!
After a long discussion (see https://review.typo3.org/c/Packages/TYPO3.CMS/+/78166), the core team decided not to add a convenience API for this to the core, and I am closing the issue accordingly.
The reasoning:
setOriginalResource()on the ExtbaseFileReferencealready is the setter for the file, a separatesetFile()would only duplicate it.- A model must not create its own dependencies or call services, so a factory method on the model is not an option. A dedicated factory service (as tried in the patch) only wraps
ResourceFactory::createFileReferenceObject(). - Projects commonly extend
sys_file_referencewith their own fields or use their ownFileReferenceclass. A generic shortcut cannot cover this without ending up with a growing signature or an untyped array, which would become a maintenance burden and is hard to extend without breaking changes. - The code needed in a project is small and can be adapted to the project’s needs:
$fileReference = GeneralUtility::makeInstance(FileReference::class); $fileReference->setOriginalResource( $this->resourceFactory->createFileReferenceObject([ 'uid_local' => $originalFile->getUid(), 'uid_foreign' => StringUtility::getUniqueId('NEW'), 'uid' => StringUtility::getUniqueId('NEW'), ]) );If you feel the documentation should show this pattern more prominently, a documentation improvement is very welcome.
hope you understand
+1 for this, i dont know why this is not by default after such a time.