‪TYPO3CMS  ‪main
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 
25 
35 {
39  protected ‪$tagName = 'input';
40 
43 
45  {
46  $this->hashService = ‪$hashService;
47  }
48 
50  {
51  $this->propertyMapper = ‪$propertyMapper;
52  }
53 
54  public function ‪initializeArguments(): void
55  {
56  parent::initializeArguments();
57  $this->registerArgument('as', 'string', '');
58  $this->registerArgument('accept', 'array', 'Values for the accept attribute', false, []);
59  $this->registerArgument('errorClass', 'string', 'CSS class to set if there are errors for this ViewHelper', false, 'f3-form-error');
60  $this->registerTagAttribute('disabled', 'string', 'Specifies that the input element should be disabled when the page loads');
61  $this->registerTagAttribute('multiple', 'string', 'Specifies that the file input element should allow multiple selection of files');
62  $this->registerUniversalTagAttributes();
63  }
64 
65  public function ‪render(): string
66  {
67  ‪$output = '';
68 
69  $name = $this->‪getName();
70  $as = $this->arguments['as'];
71  $accept = $this->arguments['accept'];
72  $resource = $this->‪getUploadedResource();
73 
74  if (!empty($accept)) {
75  $this->tag->addAttribute('accept', implode(',', $accept));
76  }
77 
78  if ($resource !== null) {
79  $resourcePointerIdAttribute = '';
80  if ($this->hasArgument('id')) {
81  $resourcePointerIdAttribute = ' id="' . htmlspecialchars($this->arguments['id']) . '-file-reference"';
82  }
83  $resourcePointerValue = $resource->getUid();
84  if ($resourcePointerValue === null) {
85  // Newly created file reference which is not persisted yet.
86  // Use the file UID instead, but prefix it with "file:" to communicate this to the type converter
87  $resourcePointerValue = 'file:' . $resource->getOriginalResource()->getOriginalFile()->getUid();
88  }
89  ‪$output .= '<input type="hidden" name="' . htmlspecialchars($this->‪getName()) . '[submittedFile][resourcePointer]" value="' . htmlspecialchars($this->hashService->appendHmac((string)$resourcePointerValue, HashScope::ResourcePointer->prefix())) . '"' . $resourcePointerIdAttribute . ' />';
90 
91  $this->templateVariableContainer->add($as, $resource);
92  ‪$output .= $this->renderChildren();
93  $this->templateVariableContainer->remove($as);
94  }
95 
96  foreach (['name', 'type', 'tmp_name', 'error', 'size'] as $fieldName) {
97  $this->‪registerFieldNameForFormTokenGeneration($name . '[' . $fieldName . ']');
98  }
99  $this->tag->addAttribute('type', 'file');
100 
101  if (isset($this->arguments['multiple'])) {
102  $this->tag->addAttribute('name', $name . '[]');
103  } else {
104  $this->tag->addAttribute('name', $name);
105  }
106 
107  $this->‪setErrorClassAttribute();
108  ‪$output .= $this->tag->render();
109 
110  return ‪$output;
111  }
112 
117  protected function ‪getUploadedResource(): ?‪FileReference
118  {
119  if ($this->‪getMappingResultsForProperty()->hasErrors()) {
120  return null;
121  }
122  $resource = $this->‪getValueAttribute();
123  if ($resource instanceof ‪FileReference) {
124  return $resource;
125  }
126  return $this->propertyMapper->convert($resource, FileReference::class);
127  }
128 }
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\setErrorClassAttribute
‪setErrorClassAttribute()
Definition: AbstractFormFieldViewHelper.php:331
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\getMappingResultsForProperty
‪getMappingResultsForProperty()
Definition: AbstractFormFieldViewHelper.php:353
‪TYPO3\CMS\Form\ViewHelpers\Form\UploadedResourceViewHelper\getUploadedResource
‪getUploadedResource()
Definition: UploadedResourceViewHelper.php:116
‪TYPO3\CMS\Form\ViewHelpers\Form\UploadedResourceViewHelper\initializeArguments
‪initializeArguments()
Definition: UploadedResourceViewHelper.php:53
‪TYPO3\CMS\Form\ViewHelpers\Form\UploadedResourceViewHelper\injectHashService
‪injectHashService(HashService $hashService)
Definition: UploadedResourceViewHelper.php:43
‪TYPO3\CMS\Form\Security\HashScope
‪HashScope
Definition: HashScope.php:25
‪TYPO3\CMS\Form\ViewHelpers\Form\UploadedResourceViewHelper
Definition: UploadedResourceViewHelper.php:35
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormViewHelper\registerFieldNameForFormTokenGeneration
‪registerFieldNameForFormTokenGeneration(string $fieldName)
Definition: AbstractFormViewHelper.php:100
‪TYPO3\CMS\Form\ViewHelpers\Form\UploadedResourceViewHelper\injectPropertyMapper
‪injectPropertyMapper(PropertyMapper $propertyMapper)
Definition: UploadedResourceViewHelper.php:48
‪TYPO3\CMS\Form\ViewHelpers\Form\UploadedResourceViewHelper\$propertyMapper
‪PropertyMapper $propertyMapper
Definition: UploadedResourceViewHelper.php:41
‪TYPO3\CMS\Form\ViewHelpers\Form\UploadedResourceViewHelper\render
‪render()
Definition: UploadedResourceViewHelper.php:64
‪TYPO3\CMS\Form\ViewHelpers\Form\UploadedResourceViewHelper\$hashService
‪HashService $hashService
Definition: UploadedResourceViewHelper.php:40
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\getName
‪getName()
Definition: AbstractFormFieldViewHelper.php:79
‪TYPO3\CMS\Extbase\Property\PropertyMapper
Definition: PropertyMapper.php:30
‪TYPO3\CMS\Form\ViewHelpers\Form\UploadedResourceViewHelper\$tagName
‪string $tagName
Definition: UploadedResourceViewHelper.php:38
‪$output
‪$output
Definition: annotationChecker.php:114
‪TYPO3\CMS\Extbase\Domain\Model\FileReference
Definition: FileReference.php:28
‪TYPO3\CMS\Form\ViewHelpers\Form
Definition: DatePickerViewHelper.php:22
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper
Definition: AbstractFormFieldViewHelper.php:37
‪TYPO3\CMS\Core\Crypto\HashService
Definition: HashService.php:27
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\getValueAttribute
‪mixed getValueAttribute()
Definition: AbstractFormFieldViewHelper.php:147