[TASK] FormEngine: Move flex form TCA migration to data provider 76/43276/7
authorMorton Jonuschat <m.jonuschat@mojocode.de>
Sun, 13 Sep 2015 13:52:00 +0000 (15:52 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Mon, 14 Sep 2015 16:25:22 +0000 (18:25 +0200)
Move the TCA flex form migration from the FlexFormElementContainer
to the TcaFlex data provider to separate parsing and rendering.

Resolves: #69718
Releases: master
Change-Id: Id9e132483394ff8efac0f21441798ff469ae3091
Reviewed-on: http://review.typo3.org/43276
Reviewed-by: Nicole Cordes <typo3@cordes.co>
Tested-by: Nicole Cordes <typo3@cordes.co>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
typo3/sysext/backend/Classes/Form/Container/FlexFormElementContainer.php
typo3/sysext/backend/Classes/Form/FormDataProvider/TcaFlex.php
typo3/sysext/backend/Tests/Unit/Form/FormDataProvider/TcaFlexTest.php

index 9be93cb..06c2ef2 100644 (file)
@@ -15,7 +15,6 @@ namespace TYPO3\CMS\Backend\Form\Container;
  */
 
 use TYPO3\CMS\Backend\Form\ElementConditionMatcher;
-use TYPO3\CMS\Core\Migrations\TcaMigration;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Lang\LanguageService;
 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
@@ -99,27 +98,6 @@ class FlexFormElementContainer extends AbstractContainer {
                                        continue;
                                }
 
-                               // On-the-fly migration for flex form "TCA"
-                               // @deprecated since TYPO3 CMS 7, will be removed in TYPO3 CMS 8. This can be removed *if* no additional TCA migration is added with CMS 8, see class TcaMigration
-                               // @todo: this migration should be done in flex provider?
-                               $dummyTca = array(
-                                       'dummyTable' => array(
-                                               'columns' => array(
-                                                       'dummyField' => $flexFormFieldArray,
-                                               ),
-                                       ),
-                               );
-                               $tcaMigration = GeneralUtility::makeInstance(TcaMigration::class);
-                               $migratedTca = $tcaMigration->migrate($dummyTca);
-                               $messages = $tcaMigration->getMessages();
-                               if (!empty($messages)) {
-                                       $context = 'FormEngine did an on-the-fly migration of a flex form data structure. This is deprecated and will be removed'
-                                               . ' with TYPO3 CMS 8. Merge the following changes into the flex form definition of table ' . $table . ' in field ' . $fieldName . ':';
-                                       array_unshift($messages, $context);
-                                       GeneralUtility::deprecationLog(implode(LF, $messages));
-                               }
-                               $flexFormFieldArray = $migratedTca['dummyTable']['columns']['dummyField'];
-
                                // Set up options for single element
                                $fakeParameterArray = array(
                                        'fieldConf' => array(
index aff0f17..957a973 100644 (file)
@@ -19,6 +19,7 @@ use TYPO3\CMS\Backend\Form\FormDataGroup\FlexFormSegment;
 use TYPO3\CMS\Backend\Form\FormDataProviderInterface;
 use TYPO3\CMS\Backend\Utility\BackendUtility;
 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
+use TYPO3\CMS\Core\Migrations\TcaMigration;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 
 /**
@@ -60,6 +61,7 @@ class TcaFlex extends AbstractItemProvider implements FormDataProviderInterface
                        $result = $this->removeExcludeFieldsFromDataStructure($result, $fieldName, $flexIdentifier);
                        $result = $this->removeDisabledFieldsFromDataStructure($result, $fieldName, $pageTsConfigOfFlex);
                        $result = $this->prepareLanguageHandlingInDataValues($result, $fieldName);
+                       $result = $this->migrateFlexformTcaDataStructureElements($result, $fieldName);
                        $result = $this->modifyDataStructureAndDataValuesByFlexFormSegmentGroup($result, $fieldName, $pageTsConfigOfFlex);
                }
 
@@ -851,6 +853,66 @@ class TcaFlex extends AbstractItemProvider implements FormDataProviderInterface
        }
 
        /**
+        * On-the-fly migration for flex form "TCA"
+        *
+        * @param array $result Result array
+        * @param string $fieldName Currently handled field name
+        * @return array Modified result
+        * @deprecated since TYPO3 CMS 7, will be removed in TYPO3 CMS 8. This can be removed *if* no additional TCA migration is added with CMS 8, see class TcaMigration
+        */
+       protected function migrateFlexformTcaDataStructureElements(array $result, $fieldName) {
+               $modifiedDataStructure = $result['processedTca']['columns'][$fieldName]['config']['ds'];
+               $modifiedDataStructure = $this->migrateFlexformTcaRecursive($modifiedDataStructure, $result['tableName'], $fieldName);
+               $result['processedTca']['columns'][$fieldName]['config']['ds'] = $modifiedDataStructure;
+               return $result;
+       }
+
+       /**
+        * Recursively migrate flex form TCA
+        *
+        * @param array $structure Given hierarchy
+        * @param string $table
+        * @param string $fieldName
+        * @return array Modified hierarchy
+        */
+
+       protected function migrateFlexformTcaRecursive($structure, $table, $fieldName) {
+               $newStructure = [];
+               foreach ($structure as $key => $value) {
+                       if ($key === 'el' && is_array($value)) {
+                               $newSubStructure = [];
+                               $tcaMigration = GeneralUtility::makeInstance(TcaMigration::class);
+                               foreach ($value as $subKey => $subValue) {
+                                       // On-the-fly migration for flex form "TCA"
+                                       // @deprecated since TYPO3 CMS 7, will be removed in TYPO3 CMS 8. This can be removed *if* no additional TCA migration is added with CMS 8, see class TcaMigration
+                                       $dummyTca = array(
+                                               'dummyTable' => array(
+                                                       'columns' => array(
+                                                               'dummyField' => $subValue,
+                                                       ),
+                                               ),
+                                       );
+                                       $migratedTca = $tcaMigration->migrate($dummyTca);
+                                       $messages = $tcaMigration->getMessages();
+                                       if (!empty($messages)) {
+                                               $context = 'FormEngine did an on-the-fly migration of a flex form data structure. This is deprecated and will be removed'
+                                                       . ' with TYPO3 CMS 8. Merge the following changes into the flex form definition of table "' . $table . '"" in field "' . $fieldName . '"":';
+                                               array_unshift($messages, $context);
+                                               GeneralUtility::deprecationLog(implode(LF, $messages));
+                                       }
+                                       $newSubStructure[$subKey] = $migratedTca['dummyTable']['columns']['dummyField'];
+                               }
+                               $value = $newSubStructure;
+                       }
+                       if (is_array($value)) {
+                               $value = $this->migrateFlexformTcaRecursive($value, $table, $fieldName);
+                       }
+                       $newStructure[$key] = $value;
+               }
+               return $newStructure;
+       }
+
+       /**
         * @return BackendUserAuthentication
         */
        protected function getBackendUser() {
index d8038fb..f7284c3 100644 (file)
@@ -2631,4 +2631,250 @@ class TcaFlexTest extends UnitTestCase {
                $this->assertEquals($expected, $this->subject->addData($input));
        }
 
+       /**
+        * @test
+        */
+       public function addDataMigratesFlexformTca() {
+               $input = [
+                       'tableName' => 'aTable',
+                       'databaseRow' => [
+                               'aField' => [
+                                       'data' => [],
+                                       'meta' => [],
+                               ],
+                               'pointerField' => 'aFlex',
+                       ],
+                       'systemLanguageRows' => [
+                               0 => [
+                                       'uid' => 0,
+                                       'iso' => 'DEF',
+                               ],
+                       ],
+                       'processedTca' => [
+                               'columns' => [
+                                       'aField' => [
+                                               'config' => [
+                                                       'type' => 'flex',
+                                                       'ds_pointerField' => 'pointerField',
+                                                       'ds' => [
+                                                               'aFlex' => '
+                                                                       <T3DataStructure>
+                                                                               <ROOT>
+                                                                                       <type>array</type>
+                                                                                       <el>
+                                                                                               <aFlexField>
+                                                                                                       <TCEforms>
+                                                                                                               <label>aFlexFieldLabel</label>
+                                                                                                               <config>
+                                                                                                                       <type>text</type>
+                                                                                                                       <default>defaultValue</default>
+                                                                                                                       <wizards type="array">
+                                                                                                                               <t3editor type="array">
+                                                                                                                                       <type>userFunc</type>
+                                                                                                                                       <userFunc>TYPO3\CMS\T3editor\FormWizard->main</userFunc>
+                                                                                                                                       <title>t3editor</title>
+                                                                                                                                       <icon>EXT:backend/Resources/Public/Images/FormFieldWizard/wizard_table.gif</icon>
+                                                                                                                                       <module type="array">
+                                                                                                                                               <name>wizard_table</name>
+                                                                                                                                       </module>
+                                                                                                                                       <params type="array">
+                                                                                                                                               <format>html</format>
+                                                                                                                                       </params>
+                                                                                                                               </t3editor>
+                                                                                                                       </wizards>
+                                                                                                               </config>
+                                                                                                       </TCEforms>
+                                                                                               </aFlexField>
+                                                                                       </el>
+                                                                               </ROOT>
+                                                                       </T3DataStructure>
+                                                               ',
+                                                       ],
+                                               ],
+                                       ],
+                               ],
+                       ],
+                       'pageTsConfigMerged' => [],
+               ];
+
+               $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['formDataGroup']['flexFormSegment'] = [
+                       \TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseRowDefaultValues::class => [],
+               ];
+
+               /** @var LanguageService|ObjectProphecy $languageService */
+               $languageService = $this->prophesize(LanguageService::class);
+               $GLOBALS['LANG'] = $languageService->reveal();
+               $languageService->sL(Argument::cetera())->willReturnArgument(0);
+
+               $this->backendUserProphecy->isAdmin()->willReturn(TRUE);
+               $this->backendUserProphecy->checkLanguageAccess(Argument::cetera())->willReturn(TRUE);
+
+               $expected = $input;
+               $expected['processedTca']['columns']['aField']['config']['ds'] = [
+                       'meta' => [
+                               'availableLanguageCodes' => [
+                                       0 => 'DEF',
+                               ],
+                       ],
+                       'sheets' => [
+                               'sDEF' => [
+                                       'ROOT' => [
+                                               'type' => 'array',
+                                               'el' => [
+                                                       'aFlexField' => [
+                                                               'label' => 'aFlexFieldLabel',
+                                                               'config' => [
+                                                                       'type' => 'text',
+                                                                       'default' => 'defaultValue',
+                                                                       'renderType' => 't3editor',
+                                                                       'format' => 'html',
+                                                               ],
+                                                       ],
+                                               ],
+                                       ],
+                               ],
+                       ],
+               ];
+
+               $expected['databaseRow']['aField']['data']['sDEF']['lDEF']['aFlexField']['vDEF'] = 'defaultValue';
+
+               $this->assertEquals($expected, $this->subject->addData($input));
+       }
+
+       /**
+        * @test
+        */
+       public function addDataMigratesFlexformTcaInContainer() {
+               $input = [
+                       'tableName' => 'aTable',
+                       'databaseRow' => [
+                               'aField' => [
+                                       'data' => [],
+                                       'meta' => [],
+                               ],
+                               'pointerField' => 'aFlex',
+                       ],
+                       'systemLanguageRows' => [
+                               0 => [
+                                       'uid' => 0,
+                                       'iso' => 'DEF',
+                               ],
+                       ],
+                       'processedTca' => [
+                               'columns' => [
+                                       'aField' => [
+                                               'config' => [
+                                                       'type' => 'flex',
+                                                       'ds_pointerField' => 'pointerField',
+                                                       'ds' => [
+                                                               'aFlex' => '
+                                                                       <T3DataStructure>
+                                                                               <ROOT>
+                                                                                       <type>array</type>
+                                                                                       <el>
+                                                                                               <section_1>
+                                                                                                       <title>section_1</title>
+                                                                                                       <type>array</type>
+                                                                                                       <section>1</section>
+                                                                                                       <el>
+                                                                                                               <aFlexContainer>
+                                                                                                                       <type>array</type>
+                                                                                                                       <title>aFlexContainerLabel</title>
+                                                                                                                       <el>
+                                                                                                                               <aFlexField>
+                                                                                                                                       <TCEforms>
+                                                                                                                                               <label>aFlexFieldLabel</label>
+                                                                                                                                               <config>
+                                                                                                                                                       <type>text</type>
+                                                                                                                                                       <default>defaultValue</default>
+                                                                                                                                                       <wizards type="array">
+                                                                                                                                                               <t3editor type="array">
+                                                                                                                                                                       <type>userFunc</type>
+                                                                                                                                                                       <userFunc>TYPO3\CMS\T3editor\FormWizard->main</userFunc>
+                                                                                                                                                                       <title>t3editor</title>
+                                                                                                                                                                       <icon>EXT:backend/Resources/Public/Images/FormFieldWizard/wizard_table.gif</icon>
+                                                                                                                                                                       <module type="array">
+                                                                                                                                                                               <name>wizard_table</name>
+                                                                                                                                                                       </module>
+                                                                                                                                                                       <params type="array">
+                                                                                                                                                                               <format>html</format>
+                                                                                                                                                                       </params>
+                                                                                                                                                               </t3editor>
+                                                                                                                                                       </wizards>
+                                                                                                                                               </config>
+                                                                                                                                       </TCEforms>
+                                                                                                                               </aFlexField>
+                                                                                                                       </el>
+                                                                                                               </aFlexContainer>
+                                                                                                       </el>
+                                                                                               </section_1>
+                                                                                       </el>
+                                                                               </ROOT>
+                                                                       </T3DataStructure>
+                                                               ',
+                                                       ],
+                                               ],
+                                       ],
+                               ],
+                       ],
+                       'pageTsConfigMerged' => [],
+               ];
+
+               $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['formDataGroup']['flexFormSegment'] = [
+                       \TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseRowDefaultValues::class => [],
+               ];
+
+               /** @var LanguageService|ObjectProphecy $languageService */
+               $languageService = $this->prophesize(LanguageService::class);
+               $GLOBALS['LANG'] = $languageService->reveal();
+               $languageService->sL(Argument::cetera())->willReturnArgument(0);
+
+               $this->backendUserProphecy->isAdmin()->willReturn(TRUE);
+               $this->backendUserProphecy->checkLanguageAccess(Argument::cetera())->willReturn(TRUE);
+
+               $expected = $input;
+               $expected['processedTca']['columns']['aField']['config']['ds'] = [
+                       'meta' => [
+                               'availableLanguageCodes' => [
+                                       0 => 'DEF',
+                               ],
+                       ],
+                       'sheets' => [
+                               'sDEF' => [
+                                       'ROOT' => [
+                                               'type' => 'array',
+                                               'el' => [
+                                                       'section_1' => [
+                                                               'title' => 'section_1',
+                                                               'type' => 'array',
+                                                               'section' => '1',
+                                                               'el' => [
+                                                                       'aFlexContainer' => [
+                                                                               'type' => 'array',
+                                                                               'title' => 'aFlexContainerLabel',
+                                                                               'el' => [
+                                                                                       'aFlexField' => [
+                                                                                               'label' => 'aFlexFieldLabel',
+                                                                                               'config' => [
+                                                                                                       'type' => 'text',
+                                                                                                       'default' => 'defaultValue',
+                                                                                                       'renderType' => 't3editor',
+                                                                                                       'format' => 'html',
+                                                                                               ],
+                                                                                       ],
+                                                                               ],
+                                                                       ],
+                                                               ],
+                                                       ],
+                                               ],
+                                       ],
+                               ],
+                       ],
+               ];
+
+               $expected['databaseRow']['aField']['data']['sDEF']['lDEF']['section_1']['templateRows']['aFlexContainer']['el']['aFlexField']['vDEF'] = 'defaultValue';
+
+               $this->assertEquals($expected, $this->subject->addData($input));
+       }
+
 }