[CLEANUP] Alwas put null at the last position
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / ViewHelpers / Form / UploadedResourceViewHelper.php
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\Form\ViewHelpers\Form;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use TYPO3\CMS\Extbase\Domain\Model\FileReference;
19 use TYPO3\CMS\Extbase\Security\Cryptography\HashService;
20 use TYPO3\CMS\Fluid\ViewHelpers\Form\UploadViewHelper;
21
22 /**
23 * This ViewHelper makes the specified Image object available for its
24 * childNodes.
25 * In case the form is redisplayed because of validation errors, a previously
26 * uploaded image will be correctly used.
27 *
28 * Scope: frontend
29 * @api
30 */
31 class UploadedResourceViewHelper extends UploadViewHelper
32 {
33
34 /**
35 * @var HashService
36 */
37 protected $hashService;
38
39 /**
40 * @var \TYPO3\CMS\Extbase\Property\PropertyMapper
41 */
42 protected $propertyMapper;
43
44 /**
45 * @param HashService $hashService
46 * @internal
47 */
48 public function injectHashService(\TYPO3\CMS\Extbase\Security\Cryptography\HashService $hashService)
49 {
50 $this->hashService = $hashService;
51 }
52
53 /**
54 * @param \TYPO3\CMS\Extbase\Property\PropertyMapper $propertyMapper
55 * @internal
56 */
57 public function injectPropertyMapper(\TYPO3\CMS\Extbase\Property\PropertyMapper $propertyMapper)
58 {
59 $this->propertyMapper = $propertyMapper;
60 }
61
62 /**
63 * Initialize the arguments.
64 *
65 * @internal
66 */
67 public function initializeArguments()
68 {
69 parent::initializeArguments();
70 $this->registerArgument('as', 'string', '');
71 $this->registerArgument('accept', 'array', 'Values for the accept attribute', false, []);
72 }
73
74 /**
75 * @return string
76 * @api
77 */
78 public function render()
79 {
80 $output = '';
81
82 $as = $this->arguments['as'];
83 $accept = $this->arguments['accept'];
84 $resource = $this->getUploadedResource();
85
86 if (!empty($accept)) {
87 $this->tag->addAttribute('accept', implode(',', $accept));
88 }
89
90 if ($resource !== null) {
91 $resourcePointerIdAttribute = '';
92 if ($this->hasArgument('id')) {
93 $resourcePointerIdAttribute = ' id="' . htmlspecialchars($this->arguments['id']) . '-file-reference"';
94 }
95 $resourcePointerValue = $resource->getUid();
96 if ($resourcePointerValue === null) {
97 // Newly created file reference which is not persisted yet.
98 // Use the file UID instead, but prefix it with "file:" to communicate this to the type converter
99 $resourcePointerValue = 'file:' . $resource->getOriginalResource()->getOriginalFile()->getUid();
100 }
101 $output .= '<input type="hidden" name="' . $this->getName() . '[submittedFile][resourcePointer]" value="' . htmlspecialchars($this->hashService->appendHmac((string)$resourcePointerValue)) . '"' . $resourcePointerIdAttribute . ' />';
102
103 $this->templateVariableContainer->add($as, $resource);
104 $output .= $this->renderChildren();
105 $this->templateVariableContainer->remove($as);
106 }
107
108 $output .= parent::render();
109 return $output;
110 }
111
112 /**
113 * Return a previously uploaded resource.
114 * Return NULL if errors occurred during property mapping for this property.
115 *
116 * @return FileReference|null
117 */
118 protected function getUploadedResource()
119 {
120 if ($this->getMappingResultsForProperty()->hasErrors()) {
121 return null;
122 }
123 $resource = $this->getValueAttribute();
124 if ($resource instanceof FileReference) {
125 return $resource;
126 }
127 return $this->propertyMapper->convert($resource, FileReference::class);
128 }
129 }