TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
UploadedResourceViewHelper.php
Go to the documentation of this file.
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 
22 
33 {
34 
38  protected $hashService;
39 
43  protected $propertyMapper;
44 
50  public function injectHashService(\TYPO3\CMS\Extbase\Security\Cryptography\HashService $hashService)
51  {
52  $this->hashService = $hashService;
53  }
54 
60  public function injectPropertyMapper(\TYPO3\CMS\Extbase\Property\PropertyMapper $propertyMapper)
61  {
62  $this->propertyMapper = $propertyMapper;
63  }
64 
71  public function initializeArguments()
72  {
73  parent::initializeArguments();
74  $this->registerArgument('as', 'string', '');
75  $this->registerArgument('accept', 'array', 'Values for the accept attribute', false, []);
76  }
77 
82  public function render()
83  {
84  $output = '';
85 
86  $as = $this->arguments['as'];
87  $accept = $this->arguments['accept'];
88  $resource = $this->getUploadedResource();
89 
90  if (!empty($accept)) {
91  $this->tag->addAttribute('accept', implode(',', $accept));
92  }
93 
94  if ($resource !== null) {
95  $resourcePointerIdAttribute = '';
96  if ($this->hasArgument('id')) {
97  $resourcePointerIdAttribute = ' id="' . htmlspecialchars($this->arguments['id']) . '-file-reference"';
98  }
99  $resourcePointerValue = $resource->getUid();
100  if ($resourcePointerValue === null) {
101  // Newly created file reference which is not persisted yet.
102  // Use the file UID instead, but prefix it with "file:" to communicate this to the type converter
103  $resourcePointerValue = 'file:' . $resource->getOriginalResource()->getOriginalFile()->getUid();
104  }
105  $output .= '<input type="hidden" name="' . $this->getName() . '[submittedFile][resourcePointer]" value="' . htmlspecialchars($this->hashService->appendHmac((string)$resourcePointerValue)) . '"' . $resourcePointerIdAttribute . ' />';
106 
107  $this->templateVariableContainer->add($as, $resource);
108  $output .= $this->renderChildren();
109  $this->templateVariableContainer->remove($as);
110  }
111 
112  $output .= parent::render();
113  return $output;
114  }
115 
122  protected function getUploadedResource()
123  {
124  if ($this->getMappingResultsForProperty()->hasErrors()) {
125  return null;
126  }
127  $resource = $this->getValueAttribute();
128  if ($resource instanceof FileReference) {
129  return $resource;
130  }
131  return $this->propertyMapper->convert($resource, FileReference::class);
132  }
133 }
injectPropertyMapper(\TYPO3\CMS\Extbase\Property\PropertyMapper $propertyMapper)
injectHashService(\TYPO3\CMS\Extbase\Security\Cryptography\HashService $hashService)