Created
April 20, 2018 13:48
-
-
Save rpflamm/eb7f0f2e82c8bd616f8ab764215331ff to your computer and use it in GitHub Desktop.
File Upload with TYPO3 8.7.13
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Vendor\MyExt\Controller; | |
class MyController extends ActionController | |
{ | |
/** | |
* action create | |
* | |
* @param \Vendor\MyExt\Domain\Model\MyModel $myModel | |
* | |
* @return void | |
*/ | |
public function createAction(MyModel $myModel) | |
{ | |
$this->myModelRepository->add($myModel); | |
$this->persistenceManager->persistAll(); | |
$this->redirect('list'); | |
} | |
public function initializeCreateAction() | |
{ | |
if ($this->request->hasArgument('myModel')) { | |
$data = $this->request->getArgument('myModel'); | |
$propertyMappingConfiguration = $this->arguments->getArgument('myModel')->getPropertyMappingConfiguration(); | |
# Prevent from errors without uploads | |
foreach (['logo', 'pdf', 'images'] as $uploadField) { | |
if (array_key_exists($uploadField, $data) && is_array($data[$uploadField])) { | |
foreach ($data[$uploadField] as $key => $value) { | |
if ($value['error'] == UPLOAD_ERR_NO_FILE) { | |
unset($data[$uploadField][$key]); | |
} else { | |
# if not empty allow property with sub properties | |
$propertyMappingConfiguration->forProperty($uploadField)->allowProperties($key)->allowAllProperties(); | |
} | |
} | |
# if no uploaded file remove field completly | |
if (empty($data[$uploadField])) { | |
unset($data[$uploadField]); | |
} | |
} | |
} | |
$this->request->setArgument('myModel', $data); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Vendor\MyExt\Domain\Model; | |
class MyModel extends AbstractEntity | |
{ | |
/** | |
* __construct | |
*/ | |
public function __construct() | |
{ | |
//Do not remove the next line: It would break the functionality | |
$this->initStorageObjects(); | |
} | |
/** | |
* Initializes all ObjectStorage properties | |
* | |
* @return void | |
*/ | |
protected function initStorageObjects() | |
{ | |
$this->logo = new ObjectStorage(); | |
$this->images = new ObjectStorage(); | |
$this->pdf = new ObjectStorage(); | |
} | |
/** | |
* @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference> | |
* @cascade remove | |
*/ | |
protected $images = null; | |
/** | |
* @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference> | |
* @cascade remove | |
* @validate NotEmpty | |
*/ | |
protected $logo = null; | |
/** | |
* @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference> | |
* @cascade remove | |
*/ | |
protected $pdf = null; | |
/** | |
* 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(ObjectStorage $images) | |
{ | |
$this->images = $images; | |
} | |
/** | |
* Returns the logo | |
* | |
* @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference> $logo | |
*/ | |
public function getLogo() | |
{ | |
return $this->logo; | |
} | |
/** | |
* Sets the logo | |
* | |
* @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference> $logo | |
* | |
* @return void | |
*/ | |
public function setLogo(ObjectStorage $logo) | |
{ | |
$this->logo = $logo; | |
} | |
/** | |
* Returns the pdf | |
* | |
* @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference> $pdf | |
*/ | |
public function getPdf() | |
{ | |
return $this->pdf; | |
} | |
/** | |
* Sets the pdf | |
* | |
* @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference> $pdf | |
* | |
* @return void | |
*/ | |
public function setPdf(ObjectStorage $pdf) | |
{ | |
$this->pdf = $pdf; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
return [ | |
'ctrl' => [ | |
/**** strip ***/ | |
], | |
'interface' => '...', | |
'types' => '...', | |
'columns' => [ | |
/**** strip ***/ | |
'logo' => [ | |
'exclude' => false, | |
'label' => 'LLL:EXT:myext/Resources/Private/Language/locallang_db.xlf:tx_myext_domain_model_mymodel.logo', | |
'config' => \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::getFileFieldTCAConfig('logo', [ | |
'appearance' => [ | |
'createNewRelationLinkTitle' => 'LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:images.addFileReference' | |
], | |
'foreign_types' => [ | |
'0' => [ | |
'showitem' => ' | |
--palette--;LLL:EXT:lang/locallang_tca.xlf:sys_file_reference.imageoverlayPalette;imageoverlayPalette, | |
--palette--;;filePalette' | |
], | |
\TYPO3\CMS\Core\Resource\File::FILETYPE_TEXT => [ | |
'showitem' => ' | |
--palette--;LLL:EXT:lang/locallang_tca.xlf:sys_file_reference.imageoverlayPalette;imageoverlayPalette, | |
--palette--;;filePalette' | |
], | |
\TYPO3\CMS\Core\Resource\File::FILETYPE_IMAGE => [ | |
'showitem' => ' | |
--palette--;LLL:EXT:lang/locallang_tca.xlf:sys_file_reference.imageoverlayPalette;imageoverlayPalette, | |
--palette--;;filePalette' | |
], | |
\TYPO3\CMS\Core\Resource\File::FILETYPE_AUDIO => [ | |
'showitem' => ' | |
--palette--;LLL:EXT:lang/locallang_tca.xlf:sys_file_reference.imageoverlayPalette;imageoverlayPalette, | |
--palette--;;filePalette' | |
], | |
\TYPO3\CMS\Core\Resource\File::FILETYPE_VIDEO => [ | |
'showitem' => ' | |
--palette--;LLL:EXT:lang/locallang_tca.xlf:sys_file_reference.imageoverlayPalette;imageoverlayPalette, | |
--palette--;;filePalette' | |
], | |
\TYPO3\CMS\Core\Resource\File::FILETYPE_APPLICATION => [ | |
'showitem' => ' | |
--palette--;LLL:EXT:lang/locallang_tca.xlf:sys_file_reference.imageoverlayPalette;imageoverlayPalette, | |
--palette--;;filePalette' | |
] | |
], | |
'maxitems' => 1 | |
], $GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext']), | |
], | |
'pdf' => [] # same as logo | |
'images' => [] # same as logo; without maxitems | |
/**** strip ***/ | |
], | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<f:form action="create" enctype="multipart/form-data" name="myModel" object="{myModel}" class="form-horizontal"> | |
<div class="form-group {s:form.ifHasError(property:'myModel.logo', then:'has-error')}"> | |
<label for="logo" class="col-sm-2 control-label"><f:translate key="tx_myext_domain_model_mymodel.logo" /> <span class="required">*</span></label> | |
<div class="col-sm-10"> | |
<f:form.upload property="logo.0" id="logo" class="form-control"/> | |
</div> | |
</div> | |
<div class="form-group {s:form.ifHasError(property:'myModel.images', then:'has-error')}"> | |
<label for="images" class="col-sm-2 control-label"><f:translate key="tx_myext_domain_model_mymodel.images" /></label> | |
<div class="col-sm-10"> | |
<f:form.upload property="images" id="images" multiple="1" class="form-control"/> | |
</div> | |
</div> | |
<div class="form-group {s:form.ifHasError(property:'myModel.pdf', then:'has-error')}"> | |
<label for="pdf" class="col-sm-2 control-label"><f:translate key="tx_myext_domain_model_mymodel.pdf" /></label> | |
<div class="col-sm-10"> | |
<f:form.upload property="pdf.0" id="pdf" class="form-control"/> | |
</div> | |
</div> | |
</f:form> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment