[BUGFIX] Allow "defaultValue" for select form elements 09/57709/2
authorRalf Zimmermann <ralf.zimmermann@tritum.de>
Fri, 20 Jul 2018 20:54:32 +0000 (22:54 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Sun, 29 Jul 2018 09:01:22 +0000 (11:01 +0200)
Allows the form editor to save the property "defaultValue" for
"Inspector-PropertyGridEditor" inspector editors. This rules for the
form element types "SingleSelect", "RadioButton", "MultiCheckbox"
and "MultiSelect".

Besides that a flaw in JavaScript is solved that truncated values
by one character (e.g. having "propert" instead of "property"), as
well as having a correct representation of objects and arrays when
initializing them in JavaScript.

Resolves: #85608
Resolves: #85670
Releases: master, 8.7
Change-Id: Ia82ee236e6becb36db13fb15e96c7caceab370d1
Reviewed-on: https://review.typo3.org/57709
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
typo3/sysext/form/Classes/Domain/Configuration/FrameworkConfiguration/Extractors/FormElement/MultiValuePropertiesExtractor.php
typo3/sysext/form/Classes/Domain/Configuration/FrameworkConfiguration/Extractors/PropertyCollectionElement/MultiValuePropertiesExtractor.php
typo3/sysext/form/Resources/Public/JavaScript/Backend/FormEditor/Core.js
typo3/sysext/form/Tests/Unit/Domain/Configuration/ConfigurationServiceTest.php

index 0154ffe..fb08315 100644 (file)
@@ -37,28 +37,53 @@ class MultiValuePropertiesExtractor extends AbstractExtractor
             $value !== 'Inspector-PropertyGridEditor'
             && $value !== 'Inspector-MultiSelectEditor'
             && $value !== 'Inspector-ValidationErrorMessageEditor'
+            && $value !== 'Inspector-RequiredValidatorEditor'
         ) {
             return;
         }
 
-        $propertyPath = implode(
-            '.',
-            [
-                'formElementsDefinition',
-                $formElementType,
-                'formEditor',
-                'editors',
-                $formEditorIndex,
-                'propertyPath',
-            ]
-        );
+        if ($value === 'Inspector-RequiredValidatorEditor') {
+            $propertyPath = implode(
+                '.',
+                [
+                    'formElementsDefinition',
+                    $formElementType,
+                    'formEditor',
+                    'editors',
+                    $formEditorIndex,
+                    'configurationOptions',
+                    'validationErrorMessage',
+                    'propertyPath',
+                ]
+            );
+        } else {
+            $propertyPath = implode(
+                '.',
+                [
+                    'formElementsDefinition',
+                    $formElementType,
+                    'formEditor',
+                    'editors',
+                    $formEditorIndex,
+                    'propertyPath',
+                ]
+            );
+        }
 
         $result = $this->extractorDto->getResult();
-        $result['formElements'][$formElementType]['multiValueProperties'][] = ArrayUtility::getValueByPath(
-            $this->extractorDto->getPrototypeConfiguration(),
-            $propertyPath,
-            '.'
-        );
+
+        if (ArrayUtility::isValidPath($this->extractorDto->getPrototypeConfiguration(), $propertyPath, '.')) {
+            $result['formElements'][$formElementType]['multiValueProperties'][] = ArrayUtility::getValueByPath(
+                $this->extractorDto->getPrototypeConfiguration(),
+                $propertyPath,
+                '.'
+            );
+        }
+
+        if ($value === 'Inspector-PropertyGridEditor') {
+            $result['formElements'][$formElementType]['multiValueProperties'][] = 'defaultValue';
+        }
+
         $this->extractorDto->setResult($result);
     }
 }
index bc925bc..f80a6a3 100644 (file)
@@ -78,6 +78,9 @@ class MultiValuePropertiesExtractor extends AbstractExtractor
             $identifier = ArrayUtility::getValueByPath($this->extractorDto->getPrototypeConfiguration(), $identifierPath, '.');
 
             $result['formElements'][$formElementType]['collections'][$propertyCollectionName][$identifier]['multiValueProperties'][] = $propertyValue;
+            if ($value === 'Inspector-PropertyGridEditor') {
+                $result['formElements'][$formElementType]['collections'][$propertyCollectionName][$identifier]['multiValueProperties'][] = 'defaultValue';
+            }
         } else {
             $result['formElements'][$formElementType]['multiValueProperties'][] = $propertyValue;
         }
index 9dfcb0f..b294926 100644 (file)
@@ -788,7 +788,7 @@ define(['jquery'], function($) {
          * @publish mixed
          */
         function set(key, value, disablePublishersOnSet) {
-          var obj, oldValue, path;
+          var obj, oldValue, path, firstPartOfPath, nextPartOfPath, index;
           utility().assert(utility().isNonEmptyString(key), 'Invalid parameter "key"', 1475361756);
           disablePublishersOnSet = !!disablePublishersOnSet;
 
@@ -797,20 +797,35 @@ define(['jquery'], function($) {
           path = key;
 
           while (path.indexOf('.') > 0) {
-            var firstPartOfPath, nextPartOfPath;
-
             firstPartOfPath = path.slice(0, path.indexOf('.'));
             path = path.slice(firstPartOfPath.length + 1);
+
             if ($.isNumeric(firstPartOfPath)) {
               firstPartOfPath = parseInt(firstPartOfPath);
             }
+
+            index = path.indexOf('.');
+            nextPartOfPath = index === -1 ? path : path.slice(0, index);
+
+            // initialize objects case they are undefined by looking up the type
+            // of the next path segment, the target type is guessed(!), thus e.g.
+            // "key" results in having an object, "123" results in having an array
             if ('undefined' === $.type(obj[firstPartOfPath])) {
-              nextPartOfPath = path.slice(0, path.indexOf('.'));
               if ($.isNumeric(nextPartOfPath)) {
                 obj[firstPartOfPath] = [];
               } else {
                 obj[firstPartOfPath] = {};
               }
+            // in case the previous guess was wrong, the initialized array
+            // is converted to an object when a non-numeric path segment is found
+            } else if (false === $.isNumeric(nextPartOfPath) && 'array' === $.type(obj[firstPartOfPath])) {
+              obj[firstPartOfPath] = obj[firstPartOfPath].reduce(
+                function(converted, item, itemIndex) {
+                  converted[itemIndex] = item;
+                  return converted;
+                },
+                {}
+              );
             }
             obj = obj[firstPartOfPath];
           }
index c503b0c..9bea353 100644 (file)
@@ -1089,6 +1089,7 @@ class ConfigurationServiceTest extends \TYPO3\TestingFramework\Core\Unit\UnitTes
                         'Text' => [
                             'multiValueProperties' => [
                                 'properties.foo.1',
+                                'defaultValue',
                                 'properties.foo.2',
                                 'properties.foo.3',
                             ],
@@ -1418,6 +1419,7 @@ class ConfigurationServiceTest extends \TYPO3\TestingFramework\Core\Unit\UnitTes
                                     'fooValidator' => [
                                         'multiValueProperties' => [
                                             'options.xxx',
+                                            'defaultValue',
                                             'options.yyy',
                                         ],
                                         'propertyPaths' => [