* ``` * * 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; } }