@Sven Kalbhenn Versuchs doch mal mit diesem VH. Musst natürlich VENDOR
und ExtName
anpassen.
<?php
namespace VENDOR\ExtName\ViewHelpers;
use TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper;
use TYPO3\CMS\Extbase\Security\Cryptography\HashService;
use TYPO3\CMS\Extbase\Property\PropertyMapper;
use TYPO3\CMS\Extbase\Domain\Model\FileReference;
class UploadViewHelper extends AbstractFormFieldViewHelper
{
protected $tagName = 'input';
protected HashService $hashService;
protected PropertyMapper $propertyMapper;
public function initializeArguments(): void
{
parent::initializeArguments();
$this->registerTagAttribute('disabled', 'string', 'Specifies that the input element should be disabled when the page loads');
$this->registerTagAttribute('multiple', 'string', 'Specifies that the file input element should allow multiple selection of files');
$this->registerTagAttribute('accept', 'string', 'Specifies the allowed file extensions to upload via comma-separated list, example ".png,.gif"');
$this->registerArgument('errorClass', 'string', 'CSS class to set if there are errors for this ViewHelper', false, 'f3-form-error');
$this->registerUniversalTagAttributes();
}
public function injectHashService(HashService $hashService): void
{
$this->hashService = $hashService;
}
public function injectPropertyMapper(PropertyMapper $propertyMapper): void
{
$this->propertyMapper = $propertyMapper;
}
public function render(): string
{
$output = '';
$resource = $this->getUploadedResource();
if ($resource !== null) {
$resourcePointerIdAttribute = '';
if ($this->hasArgument('id')) {
$resourcePointerIdAttribute = ' id="' . htmlspecialchars($this->arguments['id']) . '-file-reference"';
}
$resourcePointerValue = $resource->getUid();
if ($resourcePointerValue === null) {
$resourcePointerValue = 'file:' . $resource->getOriginalResource()->getOriginalFile()->getUid();
}
$attributes = [
'class="file-upload"',
'type="hidden"',
'name="' . $this->getName() . '[submittedFile][resourcePointer]"',
'value="' . htmlspecialchars($this->hashService->appendHmac((string)$resourcePointerValue)) . '"',
$resourcePointerIdAttribute
];
$output .= '<input ' . implode(' ', $attributes) . ' />';
$this->templateVariableContainer->add('resource', $resource);
$output .= $this->renderChildren();
$this->templateVariableContainer->remove('resource');
}
$output .= parent::render();
return $output;
}
protected function getUploadedResource(): FileReference|null
{
if ($this->getMappingResultsForProperty()->hasErrors()) {
return null;
}
$resource = $this->getValueAttribute();
if ($resource instanceof FileReference) {
return $resource;
}
return $this->propertyMapper->convert($resource, FileReference::class);
}
}