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