‪TYPO3CMS  ‪main
PropertyMappingConfiguration.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 
32 
38 {
39  public function ‪__construct(protected readonly ‪ValidatorResolver $validatorResolver)
40  {
41  }
42 
53  public function ‪afterBuildingFinished(‪RenderableInterface $renderable)
54  {
55  if ($renderable instanceof ‪FileUpload) {
56  // Set the property mapping configuration for the file upload element.
57  // * Add the UploadedFileReferenceConverter to convert an uploaded file to a
58  // FileReference.
59  // * Add the MimeTypeValidator to the UploadedFileReferenceConverter to
60  // delete non-valid file types directly.
61  // * Setup the storage:
62  // If the property "saveToFileMount" exist for this element it will be used.
63  // If this file mount or the property "saveToFileMount" does not exist
64  // the default storage "1:/user_uploads/" will be used. Uploads are placed
65  // in a dedicated sub-folder (e.g. ".../form_<40-chars-hash>/actual.file").
66 
67  $typeConverter = GeneralUtility::makeInstance(UploadedFileReferenceConverter::class);
69  $propertyMappingConfiguration = $renderable->getRootForm()
70  ->getProcessingRule($renderable->‪getIdentifier())
71  ->getPropertyMappingConfiguration()
72  ->setTypeConverter($typeConverter);
73 
74  $allowedMimeTypes = [];
75  $validators = [];
76  if (isset($renderable->getProperties()['allowedMimeTypes']) && \is_array($renderable->getProperties()['allowedMimeTypes'])) {
77  $allowedMimeTypes = array_filter($renderable->getProperties()['allowedMimeTypes']);
78  }
79  if (!empty($allowedMimeTypes)) {
80  $mimeTypeValidator = $this->validatorResolver->createValidator(MimeTypeValidator::class, ['allowedMimeTypes' => $allowedMimeTypes]);
81  $validators = [$mimeTypeValidator];
82  }
83 
84  $renderable->getRootForm()
85  ->getProcessingRule($renderable->‪getIdentifier())
86  ->filterValidators(
87  static function (‪$validator) use (&$validators) {
88  if (‪$validator instanceof ‪NotEmptyValidator) {
89  return true;
90  }
91  $validators[] = ‪$validator;
92  return false;
93  }
94  );
95 
96  $uploadConfiguration = [
99  ];
100 
101  $saveToFileMountIdentifier = $renderable->getProperties()['saveToFileMount'] ?? '';
102  if ($this->‪checkSaveFileMountAccess($saveToFileMountIdentifier)) {
103  $uploadConfiguration[‪UploadedFileReferenceConverter::CONFIGURATION_UPLOAD_FOLDER] = $saveToFileMountIdentifier;
104  } else {
105  // @todo Why should uploaded files be stored to the same directory as the *.form.yaml definitions?
106  $persistenceIdentifier = $renderable->getRootForm()->getPersistenceIdentifier();
107  if (!empty($persistenceIdentifier)) {
108  $pathinfo = ‪PathUtility::pathinfo($persistenceIdentifier);
109  $saveToFileMountIdentifier = $pathinfo['dirname'];
110  if ($this->‪checkSaveFileMountAccess($saveToFileMountIdentifier)) {
111  $uploadConfiguration[‪UploadedFileReferenceConverter::CONFIGURATION_UPLOAD_FOLDER] = $saveToFileMountIdentifier;
112  }
113  }
114  }
115  $propertyMappingConfiguration->setTypeConverterOptions(UploadedFileReferenceConverter::class, $uploadConfiguration);
116  return;
117  }
118 
119  if ($renderable->‪getType() === 'Date') {
120  // Set the property mapping configuration for the `Date` element.
121 
123  $propertyMappingConfiguration = $renderable->getRootForm()->getProcessingRule($renderable->‪getIdentifier())->getPropertyMappingConfiguration();
124  // @see https://www.w3.org/TR/2011/WD-html-markup-20110405/input.date.html#input.date.attrs.value
125  // 'Y-m-d' = https://tools.ietf.org/html/rfc3339#section-5.6 -> full-date
126  $propertyMappingConfiguration->setTypeConverterOption(DateTimeConverter::class, ‪DateTimeConverter::CONFIGURATION_DATE_FORMAT, 'Y-m-d');
127  }
128  }
129 
133  protected function ‪checkSaveFileMountAccess(string $saveToFileMountIdentifier): bool
134  {
135  if (empty($saveToFileMountIdentifier)) {
136  return false;
137  }
138 
139  if (‪PathUtility::isExtensionPath($saveToFileMountIdentifier)) {
140  return false;
141  }
142 
143  $resourceFactory = GeneralUtility::makeInstance(ResourceFactory::class);
144 
145  try {
146  $resourceFactory->getFolderObjectFromCombinedIdentifier($saveToFileMountIdentifier);
147  return true;
148  } catch (\InvalidArgumentException $e) {
149  return false;
150  }
151  }
152 
156  public function ‪afterFormStateInitialized(‪FormRuntime $formRuntime): void
157  {
158  foreach ($formRuntime->‪getFormDefinition()->getRenderablesRecursively() as $renderable) {
159  $this->‪adjustPropertyMappingForFileUploadsAtRuntime($formRuntime, $renderable);
160  }
161  }
162 
173  ‪FormRuntime $formRuntime,
174  ‪RenderableInterface $renderable
175  ): void {
176  if (!$renderable instanceof ‪FileUpload
177  || $formRuntime->‪getFormSession() === null
178  || !$formRuntime->‪canProcessFormSubmission()
179  ) {
180  return;
181  }
182  $renderable->getRootForm()
183  ->getProcessingRule($renderable->‪getIdentifier())
184  ->getPropertyMappingConfiguration()
185  ->setTypeConverterOption(
186  UploadedFileReferenceConverter::class,
188  $formRuntime->‪getFormSession()->getIdentifier()
189  );
190  }
191 }
‪TYPO3\CMS\Form\Mvc\Property\TypeConverter\UploadedFileReferenceConverter
Definition: UploadedFileReferenceConverter.php:49
‪TYPO3\CMS\Form\Mvc\Property\PropertyMappingConfiguration
Definition: PropertyMappingConfiguration.php:38
‪TYPO3\CMS\Form\Domain\Runtime\FormRuntime
Definition: FormRuntime.php:106
‪TYPO3\CMS\Core\Utility\PathUtility\isExtensionPath
‪static isExtensionPath(string $path)
Definition: PathUtility.php:117
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:27
‪TYPO3\CMS\Extbase\Validation\ValidatorResolver
Definition: ValidatorResolver.php:38
‪TYPO3\CMS\Form\Mvc\Property
‪TYPO3\CMS\Form\Domain\Runtime\FormRuntime\canProcessFormSubmission
‪canProcessFormSubmission()
Definition: FormRuntime.php:740
‪TYPO3\CMS\Form\Mvc\Property\PropertyMappingConfiguration\afterBuildingFinished
‪afterBuildingFinished(RenderableInterface $renderable)
Definition: PropertyMappingConfiguration.php:53
‪TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter
Definition: DateTimeConverter.php:64
‪TYPO3\CMS\Form\Mvc\Property\PropertyMappingConfiguration\adjustPropertyMappingForFileUploadsAtRuntime
‪adjustPropertyMappingForFileUploadsAtRuntime(FormRuntime $formRuntime, RenderableInterface $renderable)
Definition: PropertyMappingConfiguration.php:172
‪TYPO3\CMS\Form\Domain\Runtime\FormRuntime\Lifecycle\AfterFormStateInitializedInterface
Definition: AfterFormStateInitializedInterface.php:29
‪TYPO3\CMS\Form\Mvc\Property\PropertyMappingConfiguration\afterFormStateInitialized
‪afterFormStateInitialized(FormRuntime $formRuntime)
Definition: PropertyMappingConfiguration.php:156
‪TYPO3\CMS\Form\Domain\Model\Renderable\RenderableInterface
Definition: RenderableInterface.php:32
‪TYPO3\CMS\Form\Mvc\Property\TypeConverter\UploadedFileReferenceConverter\CONFIGURATION_UPLOAD_FOLDER
‪const CONFIGURATION_UPLOAD_FOLDER
Definition: UploadedFileReferenceConverter.php:53
‪TYPO3\CMS\Form\Mvc\Property\TypeConverter\UploadedFileReferenceConverter\CONFIGURATION_UPLOAD_CONFLICT_MODE
‪const CONFIGURATION_UPLOAD_CONFLICT_MODE
Definition: UploadedFileReferenceConverter.php:58
‪TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter\CONFIGURATION_DATE_FORMAT
‪const CONFIGURATION_DATE_FORMAT
Definition: DateTimeConverter.php:68
‪TYPO3\CMS\Form\Mvc\Property\PropertyMappingConfiguration\__construct
‪__construct(protected readonly ValidatorResolver $validatorResolver)
Definition: PropertyMappingConfiguration.php:39
‪$validator
‪if(isset($args['d'])) $validator
Definition: validateRstFiles.php:262
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:41
‪TYPO3\CMS\Form\Mvc\Property\TypeConverter\UploadedFileReferenceConverter\CONFIGURATION_UPLOAD_SEED
‪const CONFIGURATION_UPLOAD_SEED
Definition: UploadedFileReferenceConverter.php:63
‪TYPO3\CMS\Form\Mvc\Validation\MimeTypeValidator
Definition: MimeTypeValidator.php:33
‪TYPO3\CMS\Form\Mvc\Property\PropertyMappingConfiguration\checkSaveFileMountAccess
‪checkSaveFileMountAccess(string $saveToFileMountIdentifier)
Definition: PropertyMappingConfiguration.php:133
‪TYPO3\CMS\Form\Domain\Model\Renderable\RootRenderableInterface\getType
‪getType()
‪TYPO3\CMS\Form\Domain\Model\Renderable\RootRenderableInterface\getIdentifier
‪getIdentifier()
‪TYPO3\CMS\Form\Domain\Runtime\FormRuntime\getFormSession
‪getFormSession()
Definition: FormRuntime.php:748
‪TYPO3\CMS\Form\Domain\Runtime\FormRuntime
Definition: FormSession.php:18
‪TYPO3\CMS\Form\Mvc\Property\TypeConverter\UploadedFileReferenceConverter\CONFIGURATION_FILE_VALIDATORS
‪const CONFIGURATION_FILE_VALIDATORS
Definition: UploadedFileReferenceConverter.php:68
‪TYPO3\CMS\Extbase\Validation\Validator\NotEmptyValidator
Definition: NotEmptyValidator.php:24
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:51
‪TYPO3\CMS\Form\Domain\Model\FormElements\FileUpload
Definition: FileUpload.php:28
‪TYPO3\CMS\Form\Domain\Runtime\FormRuntime\getFormDefinition
‪getFormDefinition()
Definition: FormRuntime.php:988
‪TYPO3\CMS\Core\Utility\PathUtility\pathinfo
‪static string string[] pathinfo(string $path, int $options=PATHINFO_ALL)
Definition: PathUtility.php:270