* ``` * * @see https://docs.typo3.org/permalink/t3viewhelper:typo3-fluid-form-uploaddeletecheckbox */ final class UploadDeleteCheckboxViewHelper extends AbstractTagBasedViewHelper { /** * @var string */ protected $tagName = 'input'; public function __construct( private readonly HashService $hashService, private readonly ExtensionService $extensionService, ) { parent::__construct(); } public function initializeArguments(): void { parent::initializeArguments(); $this->registerArgument('id', 'string', 'ID of the generated checkbox element'); $this->registerArgument('property', 'string', 'Name of object property', true); $this->registerArgument('fileReference', FileReference::class, 'The file reference object', true); } public function render(): string { /** @var ?FileReference $fileReference */ $fileReference = $this->arguments['fileReference']; $property = $this->arguments['property']; $idAttribute = $this->arguments['id'] ?? ''; // Early return, if no file reference given if (!$fileReference instanceof FileReference) { return ''; } $this->tag->addAttribute('type', 'checkbox'); $request = ($this->renderingContext->getAttribute(ServerRequestInterface::class)); $extbaseRequestParams = $request->getAttribute('extbase'); $extensionName = $extbaseRequestParams->getControllerExtensionName(); $pluginName = $extbaseRequestParams->getPluginName(); if ($extensionName === '' || $pluginName === '') { throw new \RuntimeException('ExtensionName or PluginName not set in Extbase request', 1719660837); } $deleteData = [ 'property' => $property, 'fileReference' => $fileReference->getUid(), ]; $pluginNamespace = $this->extensionService->getPluginNamespace($extensionName, $pluginName); $formObjectName = $this->getFormObjectName(); $fileReferenceIdentifier = $this->hashService->hmac($property . $fileReference->getUid(), self::class); $nameAttribute = $pluginNamespace . '[' . FileHandlingService::DELETE_IDENTIFIER . ']' . '[' . $formObjectName . ']' . '[' . $fileReferenceIdentifier . ']'; $valueAttribute = $this->hashService->appendHmac( json_encode($deleteData, JSON_THROW_ON_ERROR), FileHandlingService::DELETE_IDENTIFIER ); $checked = false; if ($this->hasMappingErrorOccurred($extbaseRequestParams)) { $checked = $this->getCheckedState($request, $pluginNamespace, $formObjectName, $fileReferenceIdentifier); } $this->tag->addAttribute('id', $idAttribute); $this->tag->addAttribute('name', $nameAttribute); $this->tag->addAttribute('value', (string)$valueAttribute); if ($checked === true) { $this->tag->addAttribute('checked', 'checked'); } return $this->tag->render(); } /** * Returns the boolean checked state for the given identifier evaluated from POST data. */ private function getCheckedState( ServerRequestInterface $request, string $fieldNamePrefix, string $formObjectName, mixed $identifier ): bool { return (bool)($request->getParsedBody()[$fieldNamePrefix][FileHandlingService::DELETE_IDENTIFIER][$formObjectName][$identifier] ?? false); } private function getFormObjectName(): string { $formObjectName = $this->renderingContext->getViewHelperVariableContainer()->get( FormViewHelper::class, 'formObjectName' ); if (empty($formObjectName)) { throw new \RuntimeException('UploadDeleteCheckboxViewHelper can only be used on Fluid form context', 1719655880); } return $formObjectName; } /** * Checks if a property mapping error has occurred in the last request. */ private function hasMappingErrorOccurred(ExtbaseRequestParameters $extbaseRequest): bool { return $extbaseRequest->getOriginalRequest() !== null; } }