[BUGFIX] Respect foreign_record_defaults for inline children 93/43993/5
authorAnja Leichsenring <aleichsenring@ab-softlab.de>
Sun, 11 Oct 2015 19:24:52 +0000 (21:24 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Thu, 15 Oct 2015 11:09:50 +0000 (13:09 +0200)
Inline child records can receive default values from their parent.
This option is now respected again.

Change-Id: Iaa3dbf3497d8afd0b271619847e79c8cf8ba17b6
Resolves: #70597
Releases: master
Reviewed-on: http://review.typo3.org/43993
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: Mathias Schreiber <mathias.schreiber@wmdb.de>
Tested-by: Mathias Schreiber <mathias.schreiber@wmdb.de>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
typo3/sysext/backend/Classes/Controller/FormInlineAjaxController.php
typo3/sysext/backend/Classes/Form/FormDataProvider/InlineOverrideChildTca.php [new file with mode: 0644]
typo3/sysext/backend/Classes/Form/FormDataProvider/InlineOverruleTypesArray.php [deleted file]
typo3/sysext/backend/Tests/Unit/Form/FormDataProvider/InlineOverrrideChildTcaTest.php [new file with mode: 0644]
typo3/sysext/backend/Tests/Unit/Form/FormDataProvider/InlineOverruleTypesArrayTest.php [deleted file]
typo3/sysext/core/Configuration/DefaultConfiguration.php

index 33466ca..838f379 100644 (file)
@@ -120,37 +120,6 @@ class FormInlineAjaxController
         }
         $childData = $formDataCompiler->compile($formDataCompilerInput);
 
-        // Set default values for new created records
-        // @todo: This should be moved over to some data provider? foreign_record_defaults is currently not handled
-        // @todo: at all, but also not used in core itself. Bonus question: There is "foreign_types", there is this
-        // @todo: default setting stuff ... why can't just "all" TCA be overwritten by parent similar to TCA type
-        // @todo: related columnsOverrides? Another gem: foreign_selector_fieldTcaOverride overwrites TCA of foreign_selector
-        // @todo: depending on parent ...
-        /**
-        if (isset($config['foreign_record_defaults']) && is_array($config['foreign_record_defaults'])) {
-            $foreignTableConfig = $GLOBALS['TCA'][$child['table']];
-            // The following system relevant fields can't be set by foreign_record_defaults
-            $notSettableFields = [
-                'uid', 'pid', 't3ver_oid', 't3ver_id', 't3ver_label', 't3ver_wsid', 't3ver_state', 't3ver_stage',
-                't3ver_count', 't3ver_tstamp', 't3ver_move_id'
-            ];
-            $configurationKeysForNotSettableFields = [
-                'crdate', 'cruser_id', 'delete', 'origUid', 'transOrigDiffSourceField', 'transOrigPointerField',
-                'tstamp'
-            ];
-            foreach ($configurationKeysForNotSettableFields as $configurationKey) {
-                if (isset($foreignTableConfig['ctrl'][$configurationKey])) {
-                    $notSettableFields[] = $foreignTableConfig['ctrl'][$configurationKey];
-                }
-            }
-            foreach ($config['foreign_record_defaults'] as $fieldName => $defaultValue) {
-                if (isset($foreignTableConfig['columns'][$fieldName]) && !in_array($fieldName, $notSettableFields)) {
-                    $record[$fieldName] = $defaultValue;
-                }
-            }
-        }
-         */
-
         // Set language of new child record to the language of the parent record:
         // @todo: To my understanding, the below case can't happen: With localizationMode select, lang overlays
         // @todo: of children are only created with the "synchronize" button that will trigger a different ajax action.
diff --git a/typo3/sysext/backend/Classes/Form/FormDataProvider/InlineOverrideChildTca.php b/typo3/sysext/backend/Classes/Form/FormDataProvider/InlineOverrideChildTca.php
new file mode 100644 (file)
index 0000000..029f4f5
--- /dev/null
@@ -0,0 +1,80 @@
+<?php
+namespace TYPO3\CMS\Backend\Form\FormDataProvider;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use TYPO3\CMS\Backend\Form\FormDataProviderInterface;
+
+/**
+ * Override some child TCA in an inline parent child relation.
+ */
+class InlineOverrideChildTca implements FormDataProviderInterface
+{
+
+    /**
+     * Inline parent TCA may override some TCA of children.
+     *
+     * @param array $result
+     * @return array
+     */
+    public function addData(array $result)
+    {
+        // Replace types definition of inline child if foreign_types is defined in inlineParentConfig
+        if (isset($result['inlineParentConfig']['foreign_types'])) {
+            foreach ($result['inlineParentConfig']['foreign_types'] as $type => $config) {
+                $result['processedTca']['types'][$type] = $config;
+            }
+        }
+
+        // Set default values for (new) child if foreign_record_defaults is defined in inlineParentConfig
+        if (isset($result['inlineParentConfig']['foreign_record_defaults']) && is_array($result['inlineParentConfig']['foreign_record_defaults'])) {
+            $foreignTableConfig = $GLOBALS['TCA'][$result['inlineParentConfig']['foreign_table']];
+            // The following system relevant fields can't be set by foreign_record_defaults
+            $notSetableFields = [
+                'uid',
+                'pid',
+                't3ver_oid',
+                't3ver_id',
+                't3ver_label',
+                't3ver_wsid',
+                't3ver_state',
+                't3ver_stage',
+                't3ver_count',
+                't3ver_tstamp',
+                't3ver_move_id',
+            ];
+            // Optional configuration fields used in child table. If set, they must not be overridden, either
+            $configurationKeysForNotSettableFields = [
+                'crdate',
+                'cruser_id',
+                'delete',
+                'origUid',
+                'transOrigDiffSourceField',
+                'transOrigPointerField',
+                'tstamp',
+            ];
+            foreach ($configurationKeysForNotSettableFields as $configurationKey) {
+                if (isset($foreignTableConfig['ctrl'][$configurationKey])) {
+                    $notSetableFields[] = $foreignTableConfig['ctrl'][$configurationKey];
+                }
+            }
+            foreach ($result['inlineParentConfig']['foreign_record_defaults'] as $fieldName => $defaultValue) {
+                if (isset($foreignTableConfig['columns'][$fieldName]) && !in_array($fieldName, $notSetableFields, TRUE)) {
+                    $result['processedTca']['columns'][$fieldName]['config']['default'] = $defaultValue;
+                }
+            }
+        }
+        return $result;
+    }
+}
diff --git a/typo3/sysext/backend/Classes/Form/FormDataProvider/InlineOverruleTypesArray.php b/typo3/sysext/backend/Classes/Form/FormDataProvider/InlineOverruleTypesArray.php
deleted file mode 100644 (file)
index 98dd1df..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-<?php
-namespace TYPO3\CMS\Backend\Form\FormDataProvider;
-
-/*
- * This file is part of the TYPO3 CMS project.
- *
- * It is free software; you can redistribute it and/or modify it under
- * the terms of the GNU General Public License, either version 2
- * of the License, or any later version.
- *
- * For the full copyright and license information, please read the
- * LICENSE.txt file that was distributed with this source code.
- *
- * The TYPO3 project - inspiring people to share!
- */
-
-use TYPO3\CMS\Backend\Form\FormDataProviderInterface;
-
-/**
- * Add overrule types arrays for inline child records (FAL related)
- */
-class InlineOverruleTypesArray implements FormDataProviderInterface
-{
-
-    /**
-     * replace types definition for inline children if overruleTypesArray is defined
-     *
-     * @param array $result
-     *
-     * @return array
-     * @throws \UnexpectedValueException
-     */
-    public function addData(array $result)
-    {
-        if (isset($result['inlineParentConfig']['foreign_types'])) {
-            foreach ($result['inlineParentConfig']['foreign_types'] as $type => $config) {
-                $result['processedTca']['types'][$type] = $config;
-            }
-        }
-
-        return $result;
-    }
-}
diff --git a/typo3/sysext/backend/Tests/Unit/Form/FormDataProvider/InlineOverrrideChildTcaTest.php b/typo3/sysext/backend/Tests/Unit/Form/FormDataProvider/InlineOverrrideChildTcaTest.php
new file mode 100644 (file)
index 0000000..ea41b33
--- /dev/null
@@ -0,0 +1,156 @@
+<?php
+namespace typo3\sysext\backend\Tests\Unit\Form\FormDataProvider;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use TYPO3\CMS\Backend\Form\FormDataProvider\InlineOverrideChildTca;
+use TYPO3\CMS\Core\Tests\UnitTestCase;
+
+/**
+ * InlineOverrideChildTca Test file
+ */
+class InlineOverrrideChildTcaTest extends UnitTestCase
+{
+
+    /**
+     * @var InlineOverrideChildTca
+     */
+    protected $subject;
+
+    protected function setUp()
+    {
+        $this->subject = new InlineOverrideChildTca();
+    }
+
+    /**
+     * @test
+     */
+    public function addDataOverrulesShowitemByGivenInlineOverruleTypes()
+    {
+        $input = [
+            'inlineParentConfig' => [
+                'foreign_types' => [
+                    'aType' => [
+                        'showitem' => 'keepMe',
+                    ],
+                ],
+            ],
+            'processedTca' => [
+                'types' => [
+                    'aType' => [
+                        'showitem' => 'keepMe, aField',
+                    ],
+                    'bType' => [
+                        'showitem' => 'keepMe, aField',
+                    ],
+                ],
+            ],
+        ];
+
+        $expected = $input;
+        $expected['processedTca']['types']['aType']['showitem'] = 'keepMe';
+
+        $this->assertSame($expected, $this->subject->addData($input));
+    }
+
+    /**
+     * @test
+     */
+    public function addDataAddsTypeShowitemByGivenInlineOverruleTypes()
+    {
+        $input = [
+            'inlineParentConfig' => [
+                'foreign_types' => [
+                    'aType' => [
+                        'showitem' => 'keepMe',
+                    ],
+                    'cType' => [
+                        'showitem' => 'keepMe',
+                    ],
+                ],
+            ],
+            'processedTca' => [
+                'types' => [
+                    'aType' => [
+                        'showitem' => 'keepMe, aField',
+                    ],
+                    'bType' => [
+                        'showitem' => 'keepMe, aField',
+                    ],
+                ],
+            ],
+        ];
+
+        $expected = $input;
+        $expected['processedTca']['types']['aType']['showitem'] = 'keepMe';
+        $expected['processedTca']['types']['cType']['showitem'] = 'keepMe';
+
+        $this->assertSame($expected, $this->subject->addData($input));
+    }
+
+    /**
+     * @test
+     */
+    public function addDataSetsDefaultValueForChildRecordColumn()
+    {
+        $GLOBALS['TCA']['aTable']['columns']['aType'] = [];
+        $input = [
+            'inlineParentConfig' => [
+                'foreign_table' => 'aTable',
+                'foreign_record_defaults' => [
+                    'aType' => '42',
+                ],
+            ],
+            'processedTca' => [
+                'columns' => [
+                    'aType' => [
+                        'config' => [],
+                    ],
+                ],
+            ],
+        ];
+
+        $expected = $input;
+        $expected['processedTca']['columns']['aType']['config']['default'] = '42';
+
+        $this->assertSame($expected, $this->subject->addData($input));
+    }
+
+    /**
+     * @test
+     */
+    public function addDataIgnoresDefaultValueForRestrictedField()
+    {
+        $GLOBALS['TCA']['aTable']['columns']['pid'] = [];
+        $input = [
+            'inlineParentConfig' => [
+                'foreign_table' => 'aTable',
+                'foreign_record_defaults' => [
+                    'pid' => '42',
+                ],
+            ],
+            'processedTca' => [
+                'columns' => [
+                    'aType' => [
+                        'config' => [],
+                    ],
+                ],
+            ],
+        ];
+
+        $expected = $input;
+
+        $this->assertSame($expected, $this->subject->addData($input));
+    }
+}
diff --git a/typo3/sysext/backend/Tests/Unit/Form/FormDataProvider/InlineOverruleTypesArrayTest.php b/typo3/sysext/backend/Tests/Unit/Form/FormDataProvider/InlineOverruleTypesArrayTest.php
deleted file mode 100644 (file)
index 27dc7f3..0000000
+++ /dev/null
@@ -1,100 +0,0 @@
-<?php
-namespace typo3\sysext\backend\Tests\Unit\Form\FormDataProvider;
-
-/*
- * This file is part of the TYPO3 CMS project.
- *
- * It is free software; you can redistribute it and/or modify it under
- * the terms of the GNU General Public License, either version 2
- * of the License, or any later version.
- *
- * For the full copyright and license information, please read the
- * LICENSE.txt file that was distributed with this source code.
- *
- * The TYPO3 project - inspiring people to share!
- */
-use TYPO3\CMS\Backend\Form\FormDataProvider\InlineOverruleTypesArray;
-use TYPO3\CMS\Core\Tests\UnitTestCase;
-
-/**
- * InlineOverruleTypesArray Test file
- */
-class InlineOverruleTypesArrayTest extends UnitTestCase
-{
-
-    /**
-     * @var InlineOverruleTypesArray
-     */
-    protected $subject;
-
-    protected function setUp()
-    {
-        $this->subject = new InlineOverruleTypesArray();
-    }
-
-    /**
-     * @test
-     */
-    public function addDataOverrulesShowitemsByGivenInlineOverruleTypes()
-    {
-        $input = [
-            'inlineParentConfig' => [
-                'foreign_types' => [
-                    'aType' => [
-                        'showitem' => 'keepMe',
-                    ],
-                ],
-            ],
-            'processedTca' => [
-                'types' => [
-                    'aType' => [
-                        'showitem' => 'keepMe, aField',
-                    ],
-                    'bType' => [
-                        'showitem' => 'keepMe, aField',
-                    ],
-                ],
-            ],
-        ];
-
-        $expected = $input;
-        $expected['processedTca']['types']['aType']['showitem'] = 'keepMe';
-
-        $this->assertSame($expected, $this->subject->addData($input));
-    }
-
-    /**
-     * @test
-     */
-    public function addDataAddsTypeShowitemsByGivenInlineOverruleTypes()
-    {
-        $input = [
-            'inlineParentConfig' => [
-                'foreign_types' => [
-                    'aType' => [
-                        'showitem' => 'keepMe',
-                    ],
-                    'cType' => [
-                        'showitem' => 'keepMe',
-                    ],
-                ],
-            ],
-            'processedTca' => [
-                'types' => [
-                    'aType' => [
-                        'showitem' => 'keepMe, aField',
-                    ],
-                    'bType' => [
-                        'showitem' => 'keepMe, aField',
-                    ],
-                ],
-            ],
-        ];
-
-        $expected = $input;
-        $expected['processedTca']['types']['aType']['showitem'] = 'keepMe';
-        $expected['processedTca']['types']['cType']['showitem'] = 'keepMe';
-
-        $this->assertSame($expected, $this->subject->addData($input));
-    }
-}
index 7fc83b2..1dc10d7 100644 (file)
@@ -324,9 +324,14 @@ return array(
                             \TYPO3\CMS\Backend\Form\FormDataProvider\PageTsConfig::class,
                         ),
                     ),
+                    \TYPO3\CMS\Backend\Form\FormDataProvider\InlineOverrideChildTca::class => array(
+                        'depends' => array(
+                            \TYPO3\CMS\Backend\Form\FormDataProvider\InitializeProcessedTca::class,
+                        ),
+                    ),
                     \TYPO3\CMS\Backend\Form\FormDataProvider\ParentPageTca::class => array(
                         'depends' => array(
-                            \TYPO3\CMS\Backend\Form\FormDataProvider\InitializeProcessedTca::class
+                            \TYPO3\CMS\Backend\Form\FormDataProvider\InlineOverrideChildTca::class
                         ),
                     ),
                     \TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseRowInitializeNew::class => array(
@@ -416,11 +421,6 @@ return array(
                             \TYPO3\CMS\Backend\Form\FormDataProvider\TcaColumnsOverrides::class
                         ),
                     ),
-                    \TYPO3\CMS\Backend\Form\FormDataProvider\InlineOverruleTypesArray::class => array(
-                        'depends' => array(
-                            \TYPO3\CMS\Backend\Form\FormDataProvider\TcaColumnsOverrides::class,
-                        ),
-                    ),
                     \TYPO3\CMS\Backend\Form\FormDataProvider\TcaColumnsProcessShowitem::class => array(
                         'depends' => array(
                             \TYPO3\CMS\Backend\Form\FormDataProvider\TcaColumnsOverrides::class
@@ -431,7 +431,7 @@ return array(
                             \TYPO3\CMS\Backend\Form\FormDataProvider\TcaColumnsProcessCommon::class,
                             \TYPO3\CMS\Backend\Form\FormDataProvider\TcaColumnsProcessRecordTitle::class,
                             \TYPO3\CMS\Backend\Form\FormDataProvider\TcaColumnsProcessPlaceholders::class,
-                            \TYPO3\CMS\Backend\Form\FormDataProvider\InlineOverruleTypesArray::class,
+                            \TYPO3\CMS\Backend\Form\FormDataProvider\InlineOverrideChildTca::class,
                             \TYPO3\CMS\Backend\Form\FormDataProvider\TcaColumnsProcessShowitem::class,
                         ),
                     ),
@@ -618,9 +618,14 @@ return array(
                 ),
                 'inlineParentRecord' => array(
                     \TYPO3\CMS\Backend\Form\FormDataProvider\InitializeProcessedTca::class => array(),
-                    \TYPO3\CMS\Backend\Form\FormDataProvider\TcaColumnsRemoveUnused::class => array(
+                    \TYPO3\CMS\Backend\Form\FormDataProvider\InlineOverrideChildTca::class => array(
                         'depends' => array(
                             \TYPO3\CMS\Backend\Form\FormDataProvider\InitializeProcessedTca::class,
+                        )
+                    ),
+                    \TYPO3\CMS\Backend\Form\FormDataProvider\TcaColumnsRemoveUnused::class => array(
+                        'depends' => array(
+                            \TYPO3\CMS\Backend\Form\FormDataProvider\InlineOverrideChildTca::class,
                         ),
                     ),
                     \TYPO3\CMS\Backend\Form\FormDataProvider\TcaInlineExpandCollapseState::class => array(