Skip to content

Instantly share code, notes, and snippets.

@chriwo
Created March 1, 2023 10:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chriwo/2ebfe4b2f08682b344280e6e36c1568e to your computer and use it in GitHub Desktop.
Save chriwo/2ebfe4b2f08682b344280e6e36c1568e to your computer and use it in GitHub Desktop.
Integrate a file upload for a custom file field with EXT:femanager v6.3.4
<?php
declare(strict_types=1);
namespace Vendor\ExtensionName\DataProcessor;
use Exception;
use In2code\Femanager\DataProcessor\ImageManipulation;
use In2code\Femanager\Domain\Service\FileService;
use In2code\Femanager\Utility\FileUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
class CustomFileUpload extends ImageManipulation
{
/**
* @throws Exception
*/
public function process(array $arguments): array
{
$this->allowImageProperties();
foreach ($this->getPropertiesForUpload() as $property) {
if ($this->isFileIdentifierGiven($arguments, $property) || $this->isUploadError($arguments, $property)) {
unset($arguments['user'][$property]);
} else {
$fileItem = (array)$arguments['user'][$property];
$fileService = GeneralUtility::makeInstance(
FileService::class,
$this->getNewImageName($fileItem),
$fileItem
);
if ($fileService->isEverythingValid()) {
FileUtility::createFolderIfNotExists($this->getUploadFolder());
$pathAndFilename = $this->upload($fileItem);
$fileIdentifier = $fileService->indexFile($pathAndFilename);
$identifier = $this->createSysFileRelation($fileIdentifier);
$arguments['user'][$property] = $identifier;
} else {
unset($arguments['user'][$property]);
}
}
}
return $arguments;
}
}
Index: Classes/Domain/Validator/ServersideValidator.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
see issue 459 https://github.com/in2code-de/femanager/issues/459
===================================================================
diff --git a/Classes/Domain/Validator/ServersideValidator.php b/Classes/Domain/Validator/ServersideValidator.php
--- a/Classes/Domain/Validator/ServersideValidator.php
+++ b/Classes/Domain/Validator/ServersideValidator.php (date 1676617094991)
@@ -322,6 +322,11 @@
}
if (is_object($value)) {
+ // revalidate patch after issue 459 is closed
+ // see: https://github.com/in2code-de/femanager/issues/459
+ if (property_exists($value, 'uidLocal')) {
+ $value = $value->_getProperty('uidLocal');
+ }
if (method_exists($value, 'getUid')) {
$value = $value->getUid();
}
# Configuration/Services.yaml
services:
_defaults:
autowire: true
autoconfigure: true
public: true
Vendor\ExtensionName\:
resource: '../Classes/*'
Vendor\ExtensionName\DataProcessor\CustomFileUpload:
public: false
arguments:
$configuration: array
plugin.tx_femanager {
settings {
news {
validation {
custom_file_field {
required = 1
}
}
}
dataProcessors {
100 {
class = Vendor\ExtensionName\DataProcessor\CustomFileUpload
config {
propertyNamesForUpload = custom_file_field
sysFileRelation {
tablenames = fe_users
fieldname = custom_file_field
table_local = sys_file
}
}
events {
New = create
}
}
}
}
}
<?php
declare(strict_types=1);
namespace Vendor\ExtensionName\Domain\Model;
use TYPO3\CMS\Extbase\Domain\Model\FileReference;
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
class User extends \In2code\Femanager\Domain\Model\User
{
protected ?FileReference $customFileField = null;
public function getCustomFileField(): ?FileReference
{
return $this->customFileField;
}
public function setCustomFileField(FileReference $customFileField): void
{
$this->customFileField = $customFileField;
}
}
@GhanshyamBhava
Copy link

thanks for sharing this. But, I am getting this error. Im not sure why. I debug into dataprocessor. I am getting argument array including file info.

image

Debug: DocumentFileUpload.php

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment