fabarea / media_upload

(Mass) upload Media on the Frontend - TYPO3 CMS extension
Other
19 stars 23 forks source link

How can I create a multiple file upload? #40

Open green-bits opened 5 years ago

green-bits commented 5 years ago

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 commented 5 years ago

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