[BUGFIX] FormEngine: Fix placeholder traversal for select type relations 40/44440/3
authorMorton Jonuschat <m.jonuschat@mojocode.de>
Sat, 31 Oct 2015 15:20:15 +0000 (16:20 +0100)
committerChristian Kuhn <lolli@schwarzbu.ch>
Sat, 31 Oct 2015 17:54:24 +0000 (18:54 +0100)
When creating a new record that resolves the placeholder from a related
record via a select type field empty values get filtered out. Otherwise
an exception gets raised as 0 is not a positive integer.

Resolves: #70146
Releases: master
Change-Id: I2e49380afed40df160db7e911d3128e6664f33d0
Reviewed-on: https://review.typo3.org/44440
Reviewed-by: Sebastian Fischer <typo3@evoweb.de>
Tested-by: Sebastian Fischer <typo3@evoweb.de>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
typo3/sysext/backend/Classes/Form/FormDataProvider/TcaInputPlaceholders.php
typo3/sysext/backend/Tests/Unit/Form/FormDataProvider/TcaInputPlaceholdersTest.php

index 4e43724..66cbfbd 100644 (file)
@@ -64,9 +64,8 @@ class TcaInputPlaceholders implements FormDataProviderInterface
             }
 
             // Remove empty placeholders
-            if (empty($fieldConfig['config']['placeholder'])) {
+            if (empty($result['processedTca']['columns'][$fieldName]['config']['placeholder'])) {
                 unset($result['processedTca']['columns'][$fieldName]['config']['placeholder']);
-                continue;
             }
         }
 
@@ -103,8 +102,9 @@ class TcaInputPlaceholders implements FormDataProviderInterface
 
         switch ($fieldConfig['type']) {
             case 'select':
-                // The FormDataProviders already resolved the select items to an array of uids
-                $possibleUids = $value;
+                // The FormDataProviders already resolved the select items to an array of uids,
+                // filter out empty values that occur when no related record has been selected.
+                $possibleUids = array_filter($value);
                 $foreignTableName = $fieldConfig['foreign_table'];
                 break;
             case 'group':
@@ -125,9 +125,9 @@ class TcaInputPlaceholders implements FormDataProviderInterface
             $value = $this->getPlaceholderValue($fieldNameArray, $relatedFormData, $recursionLevel + 1);
         }
 
-        // @todo: This might not be the best solution. The database row
-        // @todo: can include array type values. Final resolution would
-        // @todo: need to take the recursion into account.
+        if ($recursionLevel === 0 && is_array($value)) {
+            $value = implode(', ', $value);
+        }
         return (string)$value;
     }
 
index 289c047..a45e61a 100644 (file)
@@ -14,6 +14,8 @@ namespace TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider;
  * The TYPO3 project - inspiring people to share!
  */
 
+use Prophecy\Argument;
+use Prophecy\Prophecy\ObjectProphecy;
 use TYPO3\CMS\Backend\Form\FormDataCompiler;
 use TYPO3\CMS\Backend\Form\FormDataGroup\TcaInputPlaceholderRecord;
 use TYPO3\CMS\Backend\Form\FormDataProvider\TcaInputPlaceholders;
@@ -160,7 +162,7 @@ class TcaInputPlaceholdersTest extends UnitTestCase
             ],
         ];
 
-        /** @var TcaInputPlaceholderRecord $languageService */
+        /** @var FormDataCompiler|ObjectProphecy $formDataCompilerProphecy */
         $formDataCompilerProphecy = $this->prophesize(FormDataCompiler::class);
         GeneralUtility::addInstance(FormDataCompiler::class, $formDataCompilerProphecy->reveal());
         $formDataCompilerProphecy->compile([
@@ -182,6 +184,41 @@ class TcaInputPlaceholdersTest extends UnitTestCase
     /**
      * @test
      */
+    public function addDataReturnsNoPlaceholderForNewSelectTypeRelation()
+    {
+        $input = [
+            'tableName' => 'aTable',
+            'databaseRow' => [
+                'aField' => '',
+                'aRelationField' => [],
+            ],
+            'processedTca' => [
+                'columns' => [
+                    'aField' => [
+                        'config' => [
+                            'type' => 'input',
+                            'placeholder' => '__row|aRelationField|aForeignField',
+                        ]
+                    ],
+                    'aRelationField' => [
+                        'config' => [
+                            'type' => 'select',
+                            'foreign_table' => 'aForeignTable'
+                        ]
+                    ]
+                ],
+            ],
+        ];
+
+        $expected = $input;
+        unset($expected['processedTca']['columns']['aField']['config']['placeholder']);
+
+        $this->assertSame($expected, $this->subject->addData($input));
+    }
+
+    /**
+     * @test
+     */
     public function addDataReturnsValueFromGroupTypeRelation()
     {
         $input = [