‪TYPO3CMS  9.5
PropertyMappingConfiguration.php
Go to the documentation of this file.
1 <?php
2 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 
30 
36 {
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 
63  $typeConverter = GeneralUtility::makeInstance(ObjectManager::class)
64  ->get(UploadedFileReferenceConverter::class);
66  $propertyMappingConfiguration = $renderable->getRootForm()
67  ->getProcessingRule($renderable->‪getIdentifier())
68  ->getPropertyMappingConfiguration()
69  ->setTypeConverter($typeConverter);
70 
71  $allowedMimeTypes = [];
72  $validators = [];
73  if (isset($renderable->getProperties()['allowedMimeTypes']) && \is_array($renderable->getProperties()['allowedMimeTypes'])) {
74  $allowedMimeTypes = array_filter($renderable->getProperties()['allowedMimeTypes']);
75  }
76  if (!empty($allowedMimeTypes)) {
77  $mimeTypeValidator = GeneralUtility::makeInstance(ObjectManager::class)
78  ->get(MimeTypeValidator::class, ['allowedMimeTypes' => $allowedMimeTypes]);
79  $validators = [$mimeTypeValidator];
80  }
81 
82  $processingRule = $renderable->getRootForm()->getProcessingRule($renderable->‪getIdentifier());
83  foreach ($processingRule->getValidators() as ‪$validator) {
84  if (!(‪$validator instanceof ‪NotEmptyValidator)) {
85  $validators[] = ‪$validator;
86  $processingRule->removeValidator(‪$validator);
87  }
88  }
89 
90  $uploadConfiguration = [
93  ];
94 
95  $saveToFileMountIdentifier = $renderable->getProperties()['saveToFileMount'] ?? '';
96  if ($this->‪checkSaveFileMountAccess($saveToFileMountIdentifier)) {
97  $uploadConfiguration[‪UploadedFileReferenceConverter::CONFIGURATION_UPLOAD_FOLDER] = $saveToFileMountIdentifier;
98  } else {
99  // @todo Why should uploaded files be stored to the same directory as the *.form.yaml definitions?
100  $persistenceIdentifier = $renderable->getRootForm()->getPersistenceIdentifier();
101  if (!empty($persistenceIdentifier)) {
102  $pathinfo = ‪PathUtility::pathinfo($persistenceIdentifier);
103  $saveToFileMountIdentifier = $pathinfo['dirname'];
104  if ($this->‪checkSaveFileMountAccess($saveToFileMountIdentifier)) {
105  $uploadConfiguration[‪UploadedFileReferenceConverter::CONFIGURATION_UPLOAD_FOLDER] = $saveToFileMountIdentifier;
106  }
107  }
108  }
109  $propertyMappingConfiguration->setTypeConverterOptions(UploadedFileReferenceConverter::class, $uploadConfiguration);
110  return;
111  }
112 
113  if ($renderable->‪getType() === 'Date') {
114  // Set the property mapping configuration for the `Date` element.
115 
117  $propertyMappingConfiguration = $renderable->getRootForm()->getProcessingRule($renderable->‪getIdentifier())->getPropertyMappingConfiguration();
118  // @see https://www.w3.org/TR/2011/WD-html-markup-20110405/input.date.html#input.date.attrs.value
119  // 'Y-m-d' = https://tools.ietf.org/html/rfc3339#section-5.6 -> full-date
120  $propertyMappingConfiguration->setTypeConverterOption(DateTimeConverter::class, ‪DateTimeConverter::CONFIGURATION_DATE_FORMAT, 'Y-m-d');
121  }
122  }
123 
129  protected function ‪checkSaveFileMountAccess(string $saveToFileMountIdentifier): bool
130  {
131  if (empty($saveToFileMountIdentifier)) {
132  return false;
133  }
134 
135  $resourceFactory = GeneralUtility::makeInstance(ObjectManager::class)
136  ->get(ResourceFactory::class);
137 
138  try {
139  $resourceFactory->getFolderObjectFromCombinedIdentifier($saveToFileMountIdentifier);
140  return true;
141  } catch (\InvalidArgumentException $e) {
142  return false;
143  }
144  }
145 
149  public function ‪afterFormStateInitialized(‪FormRuntime $formRuntime): void
150  {
151  foreach ($formRuntime->‪getFormDefinition()->‪getRenderablesRecursively() as $renderable) {
152  $this->‪adjustPropertyMappingForFileUploadsAtRuntime($formRuntime, $renderable);
153  }
154  }
155 
169  ‪FormRuntime $formRuntime,
170  ‪RenderableInterface $renderable
171  ): void {
172  if (!$renderable instanceof ‪FileUpload
173  || $formRuntime->‪getFormSession() === null
174  || !$formRuntime->‪canProcessFormSubmission()
175  ) {
176  return;
177  }
178  $renderable->getRootForm()
179  ->getProcessingRule($renderable->‪getIdentifier())
180  ->getPropertyMappingConfiguration()
181  ->setTypeConverterOption(
182  UploadedFileReferenceConverter::class,
184  $formRuntime->‪getFormSession()->‪getIdentifier()
185  );
186  }
187 }
‪TYPO3\CMS\Form\Mvc\Property\TypeConverter\UploadedFileReferenceConverter
Definition: UploadedFileReferenceConverter.php:42
‪TYPO3\CMS\Form\Mvc\Property\PropertyMappingConfiguration
Definition: PropertyMappingConfiguration.php:36
‪TYPO3\CMS\Form\Domain\Runtime\FormRuntime
Definition: FormRuntime.php:97
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:23
‪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:780
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractCompositeRenderable\getRenderablesRecursively
‪RenderableInterface[] getRenderablesRecursively()
Definition: AbstractCompositeRenderable.php:137
‪TYPO3\CMS\Form\Domain\Runtime\FormRuntime\FormSession\getIdentifier
‪string getIdentifier()
Definition: FormSession.php:51
‪TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter
Definition: DateTimeConverter.php:56
‪TYPO3\CMS\Form\Mvc\Property\PropertyMappingConfiguration\adjustPropertyMappingForFileUploadsAtRuntime
‪adjustPropertyMappingForFileUploadsAtRuntime(FormRuntime $formRuntime, RenderableInterface $renderable)
Definition: PropertyMappingConfiguration.php:168
‪TYPO3\CMS\Form\Domain\Runtime\FormRuntime\Lifecycle\AfterFormStateInitializedInterface
Definition: AfterFormStateInitializedInterface.php:27
‪TYPO3\CMS\Core\Utility\PathUtility\pathinfo
‪static string array pathinfo($path, $options=null)
Definition: PathUtility.php:207
‪TYPO3\CMS\Form\Domain\Model\Renderable\RootRenderableInterface\getIdentifier
‪string getIdentifier()
‪TYPO3\CMS\Form\Mvc\Property\PropertyMappingConfiguration\afterFormStateInitialized
‪afterFormStateInitialized(FormRuntime $formRuntime)
Definition: PropertyMappingConfiguration.php:149
‪TYPO3\CMS\Form\Domain\Model\Renderable\RenderableInterface
Definition: RenderableInterface.php:28
‪TYPO3\CMS\Form\Mvc\Property\TypeConverter\UploadedFileReferenceConverter\CONFIGURATION_UPLOAD_FOLDER
‪const CONFIGURATION_UPLOAD_FOLDER
Definition: UploadedFileReferenceConverter.php:47
‪TYPO3\CMS\Form\Mvc\Property\TypeConverter\UploadedFileReferenceConverter\CONFIGURATION_UPLOAD_CONFLICT_MODE
‪const CONFIGURATION_UPLOAD_CONFLICT_MODE
Definition: UploadedFileReferenceConverter.php:52
‪TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter\CONFIGURATION_DATE_FORMAT
‪const CONFIGURATION_DATE_FORMAT
Definition: DateTimeConverter.php:60
‪$validator
‪if(isset($args['d'])) $validator
Definition: validateRstFiles.php:218
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:33
‪TYPO3\CMS\Form\Mvc\Property\TypeConverter\UploadedFileReferenceConverter\CONFIGURATION_UPLOAD_SEED
‪const CONFIGURATION_UPLOAD_SEED
Definition: UploadedFileReferenceConverter.php:57
‪TYPO3\CMS\Form\Mvc\Validation\MimeTypeValidator
Definition: MimeTypeValidator.php:31
‪TYPO3\CMS\Form\Domain\Runtime\FormRuntime\getFormSession
‪FormSession null getFormSession()
Definition: FormRuntime.php:789
‪TYPO3\CMS\Form\Domain\Runtime\FormRuntime
Definition: FormSession.php:3
‪TYPO3\CMS\Form\Domain\Runtime\FormRuntime\getFormDefinition
‪FormDefinition getFormDefinition()
Definition: FormRuntime.php:1063
‪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:62
‪TYPO3\CMS\Extbase\Validation\Validator\NotEmptyValidator
Definition: NotEmptyValidator.php:21
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Form\Domain\Model\FormElements\FileUpload
Definition: FileUpload.php:24
‪TYPO3\CMS\Extbase\Object\ObjectManager
Definition: ObjectManager.php:25
‪TYPO3\CMS\Form\Mvc\Property\PropertyMappingConfiguration\checkSaveFileMountAccess
‪bool checkSaveFileMountAccess(string $saveToFileMountIdentifier)
Definition: PropertyMappingConfiguration.php:129