TYPO3 v15 dev-main snapshot ()
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\ViewHelpers\Form;
|
||||
|
||||
use TYPO3\CMS\Core\Crypto\HashService;
|
||||
use TYPO3\CMS\Extbase\Domain\Model\FileReference;
|
||||
use TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper;
|
||||
use TYPO3\CMS\Form\Security\HashScope;
|
||||
|
||||
/**
|
||||
* ViewHelper which renders a checkbox for file deletion in EXT:form.
|
||||
*
|
||||
* This ViewHelper is similar to Extbase's UploadDeleteCheckboxViewHelper but adapted
|
||||
* for the EXT:form context. It renders a checkbox that, when checked, marks the
|
||||
* associated file for deletion on form submission.
|
||||
*
|
||||
* Example usage:
|
||||
* ```
|
||||
* <formvh:form.uploadDeleteCheckbox
|
||||
* property="{element.identifier}"
|
||||
* fileReference="{file}"
|
||||
* fileIndex="{iterator.index}"
|
||||
* />
|
||||
* ```
|
||||
*
|
||||
* Scope: frontend
|
||||
*
|
||||
* @see https://docs.typo3.org/permalink/t3viewhelper:typo3-form-form-uploaddeletecheckbox
|
||||
*/
|
||||
final class UploadDeleteCheckboxViewHelper extends AbstractFormFieldViewHelper
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $tagName = 'input';
|
||||
|
||||
public function __construct(
|
||||
private readonly HashService $hashService,
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function initializeArguments(): void
|
||||
{
|
||||
parent::initializeArguments();
|
||||
$this->registerArgument('fileReference', FileReference::class, 'The file reference object', true);
|
||||
$this->registerArgument('fileIndex', 'int', 'Index of the file in multiple upload context', false, 0);
|
||||
}
|
||||
|
||||
public function render(): string
|
||||
{
|
||||
/** @var FileReference|null $fileReference */
|
||||
$fileReference = $this->arguments['fileReference'];
|
||||
$fileIndex = (int)$this->arguments['fileIndex'];
|
||||
|
||||
// Early return if no file reference given
|
||||
if (!$fileReference instanceof FileReference) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$this->tag->addAttribute('type', 'checkbox');
|
||||
|
||||
// Build the deletion data that will be validated on submit
|
||||
$deleteData = [
|
||||
'property' => $this->arguments['property'],
|
||||
'fileIndex' => $fileIndex,
|
||||
'fileUid' => $fileReference->getUid() ?? $fileReference->getOriginalResource()->getOriginalFile()->getUid(),
|
||||
];
|
||||
|
||||
// Create HMAC-signed value
|
||||
$valueAttribute = $this->hashService->appendHmac(
|
||||
json_encode($deleteData, JSON_THROW_ON_ERROR),
|
||||
HashScope::DeleteFile->prefix()
|
||||
);
|
||||
|
||||
// Build name attribute using the form field prefix
|
||||
$name = $this->getName();
|
||||
$nameAttribute = $name . '[__deleteFile][' . $fileIndex . ']';
|
||||
|
||||
$this->tag->addAttribute('name', $nameAttribute);
|
||||
$this->tag->addAttribute('value', $valueAttribute);
|
||||
|
||||
// Check if this checkbox was previously checked (in case of validation errors)
|
||||
if ($this->isChecked($fileIndex)) {
|
||||
$this->tag->addAttribute('checked', 'checked');
|
||||
}
|
||||
|
||||
return $this->tag->render();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the checkbox for the given file index was checked in the current request
|
||||
*/
|
||||
private function isChecked(int $fileIndex): bool
|
||||
{
|
||||
$value = $this->getValueAttribute();
|
||||
if (is_array($value) && isset($value['__deleteFile'][$fileIndex])) {
|
||||
return !empty($value['__deleteFile'][$fileIndex]);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,162 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\ViewHelpers\Form;
|
||||
|
||||
use TYPO3\CMS\Core\Crypto\HashService;
|
||||
use TYPO3\CMS\Extbase\Domain\Model\FileReference;
|
||||
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
|
||||
use TYPO3\CMS\Extbase\Property\PropertyMapper;
|
||||
use TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper;
|
||||
use TYPO3\CMS\Form\Security\HashScope;
|
||||
|
||||
/**
|
||||
* This ViewHelper makes the specified Image object available for its
|
||||
* childNodes.
|
||||
* In case the form is redisplayed because of validation errors, a previously
|
||||
* uploaded image will be correctly used.
|
||||
*
|
||||
* Scope: frontend
|
||||
*
|
||||
* @see https://docs.typo3.org/permalink/t3viewhelper:typo3-form-form-uploadedresource
|
||||
*/
|
||||
final class UploadedResourceViewHelper extends AbstractFormFieldViewHelper
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $tagName = 'input';
|
||||
|
||||
public function __construct(
|
||||
private readonly HashService $hashService,
|
||||
private readonly PropertyMapper $propertyMapper,
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function initializeArguments(): void
|
||||
{
|
||||
parent::initializeArguments();
|
||||
$this->registerArgument('as', 'string', '');
|
||||
$this->registerArgument('accept', 'array', 'Values for the accept attribute', false, []);
|
||||
$this->registerArgument('errorClass', 'string', 'CSS class to set if there are errors for this ViewHelper', false, 'f3-form-error');
|
||||
$this->registerArgument('multiple', 'boolean', 'Defines the upload element accepting multiple files', false, false);
|
||||
}
|
||||
|
||||
public function render(): string
|
||||
{
|
||||
$output = '';
|
||||
|
||||
$name = $this->getName();
|
||||
$as = $this->arguments['as'];
|
||||
$accept = $this->arguments['accept'];
|
||||
$multiple = $this->arguments['multiple'];
|
||||
$resource = $this->getUploadedResource();
|
||||
|
||||
if (!empty($accept)) {
|
||||
$this->tag->addAttribute('accept', implode(',', $accept));
|
||||
}
|
||||
|
||||
if ($resource !== null) {
|
||||
if ($resource instanceof FileReference) {
|
||||
$resourcePointerValue = $resource->getUid() ?? ('file:' . $resource->getOriginalResource()->getOriginalFile()->getUid());
|
||||
$output .= $this->buildResourcePointerInput(
|
||||
0,
|
||||
(string)$resourcePointerValue,
|
||||
$this->buildResourcePointerIdAttribute(),
|
||||
);
|
||||
} elseif ($resource instanceof ObjectStorage) {
|
||||
foreach ($resource as $file) {
|
||||
$index = $resource->getPosition($file);
|
||||
$resourcePointerValue = $file->getUid() ?? ('file:' . $file->getOriginalResource()->getOriginalFile()->getUid());
|
||||
$output .= $this->buildResourcePointerInput(
|
||||
$index,
|
||||
(string)$resourcePointerValue,
|
||||
$this->buildResourcePointerIdAttribute('-' . $index),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$this->templateVariableContainer->add($as, $resource);
|
||||
$output .= $this->renderChildren();
|
||||
$this->templateVariableContainer->remove($as);
|
||||
}
|
||||
|
||||
foreach (['name', 'type', 'tmp_name', 'error', 'size'] as $fieldName) {
|
||||
$this->registerFieldNameForFormTokenGeneration($name . '[' . $fieldName . ']');
|
||||
}
|
||||
$this->tag->addAttribute('type', 'file');
|
||||
|
||||
if ($multiple === true) {
|
||||
$this->tag->addAttribute('name', $name . '[]');
|
||||
$this->tag->addAttribute('multiple', true);
|
||||
} else {
|
||||
$this->tag->addAttribute('name', $name);
|
||||
}
|
||||
|
||||
$this->setErrorClassAttribute();
|
||||
$output .= $this->tag->render();
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
private function buildResourcePointerInput(int $index, string $resourcePointerValue, string $idAttribute): string
|
||||
{
|
||||
$name = htmlspecialchars($this->getName());
|
||||
$hmac = htmlspecialchars($this->hashService->appendHmac($resourcePointerValue, HashScope::ResourcePointer->prefix()));
|
||||
return '<input type="hidden"'
|
||||
. ' name="' . $name . '[__submittedFiles][' . $index . '][submittedFile][resourcePointer]"'
|
||||
. ' value="' . $hmac . '"'
|
||||
. $idAttribute
|
||||
. ' />';
|
||||
}
|
||||
|
||||
private function buildResourcePointerIdAttribute(string $suffix = ''): string
|
||||
{
|
||||
if (!isset($this->additionalArguments['id'])) {
|
||||
return '';
|
||||
}
|
||||
return ' id="' . htmlspecialchars($this->additionalArguments['id']) . '-file-reference' . $suffix . '"';
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a previously uploaded resource.
|
||||
* Return NULL if errors occurred during property mapping for this property.
|
||||
*/
|
||||
private function getUploadedResource(): FileReference|ObjectStorage|null
|
||||
{
|
||||
if ($this->getMappingResultsForProperty()->hasErrors()) {
|
||||
return null;
|
||||
}
|
||||
$resource = $this->getValueAttribute();
|
||||
if ($resource instanceof ObjectStorage) {
|
||||
return $resource;
|
||||
}
|
||||
if ($resource instanceof FileReference) {
|
||||
// When multiple uploads are enabled but the stored value is a single
|
||||
// FileReference, wrap it in an ObjectStorage so that the Fluid template's
|
||||
// f:for ViewHelper receives an iterable instead of crashing.
|
||||
if ($this->arguments['multiple']) {
|
||||
$storage = new ObjectStorage();
|
||||
$storage->attach($resource);
|
||||
return $storage;
|
||||
}
|
||||
return $resource;
|
||||
}
|
||||
return $this->propertyMapper->convert($resource, FileReference::class);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user