[BUGFIX] Eliminate gremlins in image cropper
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / Element / ImageManipulationElement.php
index 903affa..3d2b9ff 100644 (file)
@@ -120,7 +120,9 @@ class ImageManipulationElement extends AbstractFormElement
         parent::__construct($nodeFactory, $data);
         // Would be great, if we could inject the view here, but since the constructor is in the interface, we can't
         $this->templateView = GeneralUtility::makeInstance(StandaloneView::class);
         parent::__construct($nodeFactory, $data);
         // Would be great, if we could inject the view here, but since the constructor is in the interface, we can't
         $this->templateView = GeneralUtility::makeInstance(StandaloneView::class);
-        $this->templateView->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates/ImageManipulation/ImageCropping.html'));
+        $this->templateView->setLayoutRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Layouts/')]);
+        $this->templateView->setPartialRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Partials/ImageManipulation/')]);
+        $this->templateView->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates/ImageManipulation/ImageManipulationElement.html'));
         $this->uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
     }
 
         $this->uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
     }
 
@@ -136,29 +138,30 @@ class ImageManipulationElement extends AbstractFormElement
         $parameterArray = $this->data['parameterArray'];
         $config = $this->populateConfiguration($parameterArray['fieldConf']['config']);
 
         $parameterArray = $this->data['parameterArray'];
         $config = $this->populateConfiguration($parameterArray['fieldConf']['config']);
 
-        if ($config['readOnly']) {
-            $options = [];
-            $options['parameterArray'] = [
-                'fieldConf' => [
-                    'config' => $parameterArray['fieldConf']['config'],
-                ],
-                'itemFormElValue' => $parameterArray['itemFormElValue'],
-            ];
-            $options['renderType'] = 'none';
-
-            // Early return in case the field is set to read only
-            return $this->nodeFactory->create($options)->render();
-        }
-
         $file = $this->getFile($this->data['databaseRow'], $config['file_field']);
         if (!$file) {
             // Early return in case we do not find a file
             return $resultArray;
         }
 
         $file = $this->getFile($this->data['databaseRow'], $config['file_field']);
         if (!$file) {
             // Early return in case we do not find a file
             return $resultArray;
         }
 
-        $config = $this->processConfiguration($config, $parameterArray['itemFormElValue'] ?? '{}');
+        $config = $this->processConfiguration($config, $parameterArray['itemFormElValue'], $file);
+
+        $fieldInformationResult = $this->renderFieldInformation();
+        $fieldInformationHtml = $fieldInformationResult['html'];
+        $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
+
+        $fieldControlResult = $this->renderFieldControl();
+        $fieldControlHtml = $fieldControlResult['html'];
+        $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldControlResult, false);
+
+        $fieldWizardResult = $this->renderFieldWizard();
+        $fieldWizardHtml = $fieldWizardResult['html'];
+        $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
 
         $arguments = [
 
         $arguments = [
+            'fieldInformation' => $fieldInformationHtml,
+            'fieldControl' => $fieldControlHtml,
+            'fieldWizard' => $fieldWizardHtml,
             'isAllowedFileExtension' => in_array(strtolower($file->getExtension()), GeneralUtility::trimExplode(',', strtolower($config['allowedExtensions'])), true),
             'image' => $file,
             'formEngine' => [
             'isAllowedFileExtension' => in_array(strtolower($file->getExtension()), GeneralUtility::trimExplode(',', strtolower($config['allowedExtensions'])), true),
             'image' => $file,
             'formEngine' => [
@@ -182,7 +185,8 @@ class ImageManipulationElement extends AbstractFormElement
                 $arguments['formEngine']['validation'] = $this->getValidationDataAsJsonString(['required' => true]);
             }
         }
                 $arguments['formEngine']['validation'] = $this->getValidationDataAsJsonString(['required' => true]);
             }
         }
-        $resultArray['html'] = $this->templateView->renderSection('Element', $arguments);
+        $this->templateView->assignMultiple($arguments);
+        $resultArray['html'] = $this->templateView->render();
 
         return $resultArray;
     }
 
         return $resultArray;
     }
@@ -271,13 +275,18 @@ class ImageManipulationElement extends AbstractFormElement
     /**
      * @param array $config
      * @param string $elementValue
     /**
      * @param array $config
      * @param string $elementValue
+     * @param File $file
      * @return array
      * @throws \TYPO3\CMS\Core\Imaging\ImageManipulation\InvalidConfigurationException
      */
      * @return array
      * @throws \TYPO3\CMS\Core\Imaging\ImageManipulation\InvalidConfigurationException
      */
-    protected function processConfiguration(array $config, string $elementValue)
+    protected function processConfiguration(array $config, string &$elementValue, File $file)
     {
         $cropVariantCollection = CropVariantCollection::create($elementValue, $config['cropVariants']);
     {
         $cropVariantCollection = CropVariantCollection::create($elementValue, $config['cropVariants']);
+        if (empty($config['readOnly'])) {
+            $cropVariantCollection = $cropVariantCollection->applyRatioRestrictionToSelectedCropArea($file);
+        }
         $config['cropVariants'] = $cropVariantCollection->asArray();
         $config['cropVariants'] = $cropVariantCollection->asArray();
+        $elementValue = (string)$cropVariantCollection;
         $config['allowedExtensions'] = implode(', ', GeneralUtility::trimExplode(',', $config['allowedExtensions'], true));
         return $config;
     }
         $config['allowedExtensions'] = implode(', ', GeneralUtility::trimExplode(',', $config['allowedExtensions'], true));
         return $config;
     }