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