TYPO3 CMS  TYPO3_8-7
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 
32 {
33 
37  protected $hashService;
38 
42  protected $propertyMapper;
43 
48  public function injectHashService(\TYPO3\CMS\Extbase\Security\Cryptography\HashService $hashService)
49  {
50  $this->hashService = $hashService;
51  }
52 
57  public function injectPropertyMapper(\TYPO3\CMS\Extbase\Property\PropertyMapper $propertyMapper)
58  {
59  $this->propertyMapper = $propertyMapper;
60  }
61 
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 
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 
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 }
injectPropertyMapper(\TYPO3\CMS\Extbase\Property\PropertyMapper $propertyMapper)
injectHashService(\TYPO3\CMS\Extbase\Security\Cryptography\HashService $hashService)