‪TYPO3CMS  10.4
UploadedResourceViewHelper.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
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 
19 
24 
34 {
35 
39  protected ‪$hashService;
40 
44  protected ‪$propertyMapper;
45 
51  {
52  $this->hashService = ‪$hashService;
53  }
54 
60  {
61  $this->propertyMapper = ‪$propertyMapper;
62  }
63 
69  public function ‪initializeArguments()
70  {
71  parent::initializeArguments();
72  $this->registerArgument('as', 'string', '');
73  $this->registerArgument('accept', 'array', 'Values for the accept attribute', false, []);
74  }
75 
79  public function ‪render()
80  {
81  ‪$output = '';
82 
83  $as = $this->arguments['as'];
84  $accept = $this->arguments['accept'];
85  $resource = $this->‪getUploadedResource();
86 
87  if (!empty($accept)) {
88  $this->tag->addAttribute('accept', implode(',', $accept));
89  }
90 
91  if ($resource !== null) {
92  $resourcePointerIdAttribute = '';
93  if ($this->hasArgument('id')) {
94  $resourcePointerIdAttribute = ' id="' . htmlspecialchars($this->arguments['id']) . '-file-reference"';
95  }
96  $resourcePointerValue = $resource->getUid();
97  if ($resourcePointerValue === null) {
98  // Newly created file reference which is not persisted yet.
99  // Use the file UID instead, but prefix it with "file:" to communicate this to the type converter
100  $resourcePointerValue = 'file:' . $resource->getOriginalResource()->getOriginalFile()->getUid();
101  }
102  ‪$output .= '<input type="hidden" name="' . htmlspecialchars($this->‪getName()) . '[submittedFile][resourcePointer]" value="' . htmlspecialchars($this->hashService->appendHmac((string)$resourcePointerValue)) . '"' . $resourcePointerIdAttribute . ' />';
103 
104  $this->templateVariableContainer->add($as, $resource);
105  ‪$output .= $this->renderChildren();
106  $this->templateVariableContainer->remove($as);
107  }
108 
109  ‪$output .= parent::render();
110  return ‪$output;
111  }
112 
119  protected function ‪getUploadedResource()
120  {
121  if ($this->‪getMappingResultsForProperty()->hasErrors()) {
122  return null;
123  }
124  $resource = $this->‪getValueAttribute();
125  if ($resource instanceof ‪FileReference) {
126  return $resource;
127  }
128  return $this->propertyMapper->convert($resource, FileReference::class);
129  }
130 }
‪TYPO3\CMS\Form\ViewHelpers\Form\UploadedResourceViewHelper\initializeArguments
‪initializeArguments()
Definition: UploadedResourceViewHelper.php:67
‪TYPO3\CMS\Form\ViewHelpers\Form\UploadedResourceViewHelper\injectHashService
‪injectHashService(HashService $hashService)
Definition: UploadedResourceViewHelper.php:48
‪TYPO3\CMS\Fluid\ViewHelpers\Form\UploadViewHelper
Definition: UploadViewHelper.php:34
‪TYPO3\CMS\Form\ViewHelpers\Form\UploadedResourceViewHelper
Definition: UploadedResourceViewHelper.php:34
‪TYPO3\CMS\Extbase\Security\Cryptography\HashService
Definition: HashService.php:31
‪TYPO3\CMS\Form\ViewHelpers\Form\UploadedResourceViewHelper\getUploadedResource
‪FileReference null getUploadedResource()
Definition: UploadedResourceViewHelper.php:117
‪TYPO3\CMS\Form\ViewHelpers\Form\UploadedResourceViewHelper\injectPropertyMapper
‪injectPropertyMapper(PropertyMapper $propertyMapper)
Definition: UploadedResourceViewHelper.php:57
‪TYPO3\CMS\Form\ViewHelpers\Form\UploadedResourceViewHelper\$hashService
‪HashService $hashService
Definition: UploadedResourceViewHelper.php:38
‪TYPO3\CMS\Extbase\Property\PropertyMapper
Definition: PropertyMapper.php:37
‪TYPO3\CMS\Form\ViewHelpers\Form\UploadedResourceViewHelper\render
‪string render()
Definition: UploadedResourceViewHelper.php:77
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\getMappingResultsForProperty
‪TYPO3 CMS Extbase Error Result getMappingResultsForProperty()
Definition: AbstractFormFieldViewHelper.php:355
‪$output
‪$output
Definition: annotationChecker.php:119
‪TYPO3\CMS\Extbase\Domain\Model\FileReference
Definition: FileReference.php:28
‪TYPO3\CMS\Form\ViewHelpers\Form
Definition: DatePickerViewHelper.php:22
‪TYPO3\CMS\Form\ViewHelpers\Form\UploadedResourceViewHelper\$propertyMapper
‪TYPO3 CMS Extbase Property PropertyMapper $propertyMapper
Definition: UploadedResourceViewHelper.php:42
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\getName
‪string getName()
Definition: AbstractFormFieldViewHelper.php:90
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\getValueAttribute
‪mixed getValueAttribute()
Definition: AbstractFormFieldViewHelper.php:151