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