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