‪TYPO3CMS  9.5
UploadedResourceViewHelper.php
Go to the documentation of this file.
1 <?php
2 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 
21 
31 {
32 
36  protected ‪$hashService;
37 
41  protected ‪$propertyMapper;
42 
47  public function ‪injectHashService(\‪TYPO3\CMS\‪Extbase\Security\Cryptography\‪HashService ‪$hashService)
48  {
49  $this->hashService = ‪$hashService;
50  }
51 
56  public function ‪injectPropertyMapper(\‪TYPO3\CMS\‪Extbase\Property\PropertyMapper ‪$propertyMapper)
57  {
58  $this->propertyMapper = ‪$propertyMapper;
59  }
60 
66  public function ‪initializeArguments()
67  {
68  parent::initializeArguments();
69  $this->registerArgument('as', 'string', '');
70  $this->registerArgument('accept', 'array', 'Values for the accept attribute', false, []);
71  }
72 
76  public function ‪render()
77  {
78  ‪$output = '';
79 
80  $as = $this->arguments['as'];
81  $accept = $this->arguments['accept'];
82  $resource = $this->‪getUploadedResource();
83 
84  if (!empty($accept)) {
85  $this->tag->addAttribute('accept', implode(',', $accept));
86  }
87 
88  if ($resource !== null) {
89  $resourcePointerIdAttribute = '';
90  if ($this->hasArgument('id')) {
91  $resourcePointerIdAttribute = ' id="' . htmlspecialchars($this->arguments['id']) . '-file-reference"';
92  }
93  $resourcePointerValue = $resource->getUid();
94  if ($resourcePointerValue === null) {
95  // Newly created file reference which is not persisted yet.
96  // Use the file UID instead, but prefix it with "file:" to communicate this to the type converter
97  $resourcePointerValue = 'file:' . $resource->getOriginalResource()->getOriginalFile()->getUid();
98  }
99  ‪$output .= '<input type="hidden" name="' . htmlspecialchars($this->‪getName()) . '[submittedFile][resourcePointer]" value="' . htmlspecialchars($this->hashService->appendHmac((string)$resourcePointerValue)) . '"' . $resourcePointerIdAttribute . ' />';
100 
101  $this->templateVariableContainer->add($as, $resource);
102  ‪$output .= $this->renderChildren();
103  $this->templateVariableContainer->remove($as);
104  }
105 
106  ‪$output .= parent::render();
107  return ‪$output;
108  }
109 
116  protected function ‪getUploadedResource()
117  {
118  if ($this->‪getMappingResultsForProperty()->hasErrors()) {
119  return null;
120  }
121  $resource = $this->‪getValueAttribute();
122  if ($resource instanceof ‪FileReference) {
123  return $resource;
124  }
125  return $this->propertyMapper->convert($resource, FileReference::class);
126  }
127 }
‪TYPO3\CMS\Extbase\Annotation
Definition: IgnoreValidation.php:4
‪TYPO3\CMS\Form\ViewHelpers\Form\UploadedResourceViewHelper\initializeArguments
‪initializeArguments()
Definition: UploadedResourceViewHelper.php:64
‪TYPO3\CMS\Fluid\ViewHelpers\Form\UploadViewHelper
Definition: UploadViewHelper.php:33
‪TYPO3
‪TYPO3\CMS\Form\ViewHelpers\Form\UploadedResourceViewHelper
Definition: UploadedResourceViewHelper.php:31
‪TYPO3\CMS\Extbase\Security\Cryptography\HashService
Definition: HashService.php:24
‪TYPO3\CMS\Form\ViewHelpers\Form\UploadedResourceViewHelper\getUploadedResource
‪FileReference null getUploadedResource()
Definition: UploadedResourceViewHelper.php:114
‪TYPO3\CMS\Form\ViewHelpers\Form\UploadedResourceViewHelper\$hashService
‪HashService $hashService
Definition: UploadedResourceViewHelper.php:35
‪TYPO3\CMS\Form\ViewHelpers\Form\UploadedResourceViewHelper\injectPropertyMapper
‪injectPropertyMapper(\TYPO3\CMS\Extbase\Property\PropertyMapper $propertyMapper)
Definition: UploadedResourceViewHelper.php:54
‪TYPO3\CMS\Form\ViewHelpers\Form\UploadedResourceViewHelper\render
‪string render()
Definition: UploadedResourceViewHelper.php:74
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\getMappingResultsForProperty
‪TYPO3 CMS Extbase Error Result getMappingResultsForProperty()
Definition: AbstractFormFieldViewHelper.php:348
‪$output
‪$output
Definition: annotationChecker.php:113
‪TYPO3\CMS\Extbase\Domain\Model\FileReference
Definition: FileReference.php:25
‪TYPO3\CMS\Form\ViewHelpers\Form
Definition: DatePickerViewHelper.php:3
‪TYPO3\CMS\Form\ViewHelpers\Form\UploadedResourceViewHelper\injectHashService
‪injectHashService(\TYPO3\CMS\Extbase\Security\Cryptography\HashService $hashService)
Definition: UploadedResourceViewHelper.php:45
‪TYPO3\CMS\Form\ViewHelpers\Form\UploadedResourceViewHelper\$propertyMapper
‪TYPO3 CMS Extbase Property PropertyMapper $propertyMapper
Definition: UploadedResourceViewHelper.php:39
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\getName
‪string getName()
Definition: AbstractFormFieldViewHelper.php:86
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\getValueAttribute
‪mixed getValueAttribute()
Definition: AbstractFormFieldViewHelper.php:147