Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I create a multiple file upload? #40

Open
green-bits opened this issue Jan 17, 2019 · 1 comment
Open

How can I create a multiple file upload? #40

green-bits opened this issue Jan 17, 2019 · 1 comment

Comments

@green-bits
Copy link

I manage to upload several files, but they are not connected to my model. In the table sys_file_reference the uid_foreign is missing.
My model is called Place and has the property images.
I followed the instructions in the manual except for the following modifications:

SQL
My images field is not of type varchar but int.

...
images int(11) unsigned NOT NULL default '0',
...

TCA
In TCA I define the maximum number and the local_table for sys_file_reference

'images' => [
    ...
    'config' => \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::getFileFieldTCAConfig(
        'images',
        [
            ...
            'maxitems' => 10,
            'foreign_match_fields' => [
                'fieldname' => 'images',
                'tablenames' => 'tx_foejplaces_domain_model_place',
                'table_local' => 'sys_file',
            ],
        ],
        $GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext']
    ),
],

Model

class Place extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity
{
/**
     * images
     *
     * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference>
     * @cascade remove
     */
    protected $images;

/**
     * __construct
     */
    public function __construct()
    {
        //Do not remove the next line: It would break the functionality
        $this->initStorageObjects();
    }

 /**
     * Initializes all ObjectStorage properties
     * Do not modify this method!
     * It will be rewritten on each save in the extension builder
     * You may modify the constructor of this class instead
     * 
     * @return void
     */
    protected function initStorageObjects()
    {
        ...
        $this->images = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
    }

/**
     * Adds a FileReference
     *
     * @param \TYPO3\CMS\Extbase\Domain\Model\FileReference $image
     * @return void
     */
    public function addImage(\TYPO3\CMS\Extbase\Domain\Model\FileReference $image) {
        $this->images->attach($image);
    }

    /**
     * Removes a FileReference
     *
     * @param \TYPO3\CMS\Extbase\Domain\Model\FileReference $imageToRemove The FileReference to be removed
     * @return void
     */
    public function removeImage(\TYPO3\CMS\Extbase\Domain\Model\FileReference $imageToRemove) {
        $this->images->detach($imageToRemove);
    }

    /**
     * Returns the images
     *
     * @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference> $images
     */
    public function getImages() {
        return $this->images;
    }

    /**
     * Sets the images
     *
     * @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference> $images
     * @return void
     */
    public function setImages(\TYPO3\CMS\Extbase\Persistence\ObjectStorage $images) {
        $this->images = $images;
    }
...
}

Controller
In the controller I replaced the line $storage = ResourceFactory::getInstance()->getStorageObject(1); which did not work for me.

/**
     * action create
     *
     * @param \GreenBits\FoejPlaces\Domain\Model\Place $newPlace
     * @return void
     */
    public function createAction(\GreenBits\FoejPlaces\Domain\Model\Place $newPlace)
    {
        $this->addFlashMessage('The object was created.', '', \TYPO3\CMS\Core\Messaging\AbstractMessage::WARNING);

        # A property name is needed in case specified in the Fluid Widget
        # <mu:widget.upload property="images"/>
        $uploadedFiles = $this->uploadFileService->getUploadedFiles('images');

        # Process uploaded files and move them into a Resource Storage (FAL)
        foreach($uploadedFiles as $uploadedFile) {

            /** @var \Fab\MediaUpload\UploadedFile $uploadedFile */
            $uploadedFile->getTemporaryFileNameAndPath();

            // $storage = ResourceFactory::getInstance()->getStorageObject(1);
            $resourceFactory = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance();
            $storage = $resourceFactory->getDefaultStorage();

            /** @var File $file */
            $file = $storage->addFile(
                $uploadedFile->getTemporaryFileNameAndPath(),
                $storage->getRootLevelFolder(),
                $uploadedFile->getFileName(),
                \TYPO3\CMS\Core\Resource\DuplicationBehavior::RENAME
            );

            # Note: Use method `addUploadedFile` instead of `addFile` if file is uploaded
            # via a regular "input" control instead of the upload widget (fine uploader plugin)
            # $file = $storage->addUploadedFile()

            $fileReference = $this->objectManager->get(\GreenBits\FoejPlaces\Domain\Model\FileReference::class);
            $fileReference->setFile($file);
            $newPlace->addImage($fileReference);
        }
       $this->placeRepository->add($newPlace);
      //  $this->redirect('list');
    }
@Kephson
Copy link
Contributor

Kephson commented Aug 12, 2019

Hi, I pushed an example extension for TYPO3 9.5: https://github.com/Kephson/fe_upload_example
I think this issue can be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants