[BUGFIX] Repair overrideVals for FormEngine 04/43304/5
authorAnja Leichsenring <aleichsenring@ab-softlab.de>
Mon, 14 Sep 2015 15:17:19 +0000 (17:17 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Mon, 14 Sep 2015 15:55:44 +0000 (17:55 +0200)
While passing values via GET or POST, those values have been lost
to the form. The handling has passed to a dedicated element class,
that handles them correctly.

Change-Id: If41ae361e8e4fd4012a13bf888dc62d94d5e079c
Resolves: #69784
Resolves: #69661
Resolves: #69664
Releases: master
Reviewed-on: http://review.typo3.org/43304
Reviewed-by: Susanne Moog <typo3@susannemoog.de>
Tested-by: Susanne Moog <typo3@susannemoog.de>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
typo3/sysext/backend/Classes/Controller/EditDocumentController.php
typo3/sysext/backend/Classes/Form/Container/SingleFieldContainer.php
typo3/sysext/backend/Classes/Form/Element/InputHiddenElement.php [new file with mode: 0644]
typo3/sysext/backend/Classes/Form/Element/InputTextElement.php
typo3/sysext/backend/Classes/Form/FormDataCompiler.php
typo3/sysext/backend/Classes/Form/NodeFactory.php
typo3/sysext/backend/Tests/Unit/Form/Element/InputHiddenElementTest.php [new file with mode: 0644]

index d530621..dff00df 100644 (file)
@@ -1011,7 +1011,7 @@ class EditDocumentController implements \TYPO3\CMS\Core\Http\ControllerInterface
                                                                // Set additional FormData
                                                                // @todo: This is a hack and should be done differently
                                                                if (is_array($this->overrideVals) && is_array($this->overrideVals[$table])) {
-                                                                       $formData['hiddenFieldListArray'] = array_keys($this->overrideVals[$table]);
+                                                                       $formData['overrideValues'] = $this->overrideVals[$table];
                                                                }
 
                                                                if ($command !== 'new') {
index ef3e94c..f2c0a9e 100644 (file)
@@ -15,13 +15,11 @@ namespace TYPO3\CMS\Backend\Form\Container;
  */
 
 use TYPO3\CMS\Backend\Form\ElementConditionMatcher;
-use TYPO3\CMS\Backend\Form\NodeFactory;
 use TYPO3\CMS\Core\Imaging\Icon;
 use TYPO3\CMS\Core\Imaging\IconFactory;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
 use TYPO3\CMS\Backend\Form\Utility\FormEngineUtility;
-use TYPO3\CMS\Core\Utility\ArrayUtility;
 use TYPO3\CMS\Lang\LanguageService;
 use TYPO3\CMS\Backend\Utility\BackendUtility;
 use TYPO3\CMS\Core\Utility\DiffUtility;
@@ -31,11 +29,11 @@ use TYPO3\CMS\Core\Database\RelationHandler;
 /**
  * Container around a "single field".
  *
- * This container is the last one in the chain before processing is hand over to single element classes.
+ * This container is the last one in the chain before processing is handed over to single element classes.
  * If a single field is of type flex or inline, it however creates FlexFormLanguageContainer or InlineControlContainer.
  *
  * The container does various checks and processing for a given single fields, for example it resolves
- * display conditions and the HTML to compare compare different languages.
+ * display conditions and the HTML to compare different languages.
  */
 class SingleFieldContainer extends AbstractContainer {
 
@@ -135,16 +133,15 @@ class SingleFieldContainer extends AbstractContainer {
                        $alertMsgOnChange = '';
                }
 
-               if (in_array($fieldName, $this->data['hiddenFieldListArray'], TRUE)) {
-                       // Render as a hidden field if this field had a forced value in overrideVals
-                       // @todo: This is an ugly concept ... search for overrideVals and defVals for a full picture of this madness
-                       $resultArray = $this->initializeResultArray();
-                       // This hidden field can not just be returned as casual html since upper containers will then render a label and wrapping stuff - this is not wanted here
-                       $value = $parameterArray['itemFormElValue'];
-                       if (is_array($value)) {
-                               $value = array_shift($value);
-                       }
-                       $resultArray['additionalHiddenFields'][] = '<input type="hidden" name="' . $parameterArray['itemFormElName'] . '" value="' . htmlspecialchars($value) . '" />';
+               if (array_key_exists($fieldName, $this->data['overrideValues'])) {
+                       $options = [
+                               'parameterArray' => [
+                                               'itemFormElName' => 'data[' . $table . '][' . $row['uid'] . '][' . $fieldName . ']',
+                                               'itemFormElValue' => $this->data['overrideValues'][$fieldName]
+                               ],
+                               'renderType' => 'hidden'
+                       ];
+                       $resultArray = $this->nodeFactory->create($options)->render();
                } else {
                        // JavaScript code for event handlers:
                        $parameterArray['fieldChangeFunc'] = array();
diff --git a/typo3/sysext/backend/Classes/Form/Element/InputHiddenElement.php b/typo3/sysext/backend/Classes/Form/Element/InputHiddenElement.php
new file mode 100644 (file)
index 0000000..ba95e04
--- /dev/null
@@ -0,0 +1,42 @@
+<?php
+namespace TYPO3\CMS\Backend\Form\Element;
+
+/*
+ * 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\Utility\BackendUtility;
+use TYPO3\CMS\Core\Imaging\Icon;
+use TYPO3\CMS\Core\Imaging\IconFactory;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Core\Utility\MathUtility;
+use TYPO3\CMS\Core\Utility\StringUtility;
+
+/**
+ * Generation of TCEform elements of the type "input type=hidden"
+ */
+class InputHiddenElement extends AbstractFormElement {
+
+       /**
+        * This will render an input type="hidden" form field
+        * @return array As defined in initializeResultArray() of AbstractNode
+        */
+       public function render() {
+               $parameterArray = $this->data['parameterArray'];
+               $resultArray = $this->initializeResultArray();
+
+               $resultArray['additionalHiddenFields'][] = '<input type="hidden" name="' . $parameterArray['itemFormElName'] . '" value="' . htmlspecialchars($parameterArray['itemFormElValue']) . '" />';
+
+               return $resultArray;
+       }
+
+}
index 072bb5b..44bd740 100644 (file)
@@ -22,7 +22,7 @@ use TYPO3\CMS\Core\Utility\MathUtility;
 use TYPO3\CMS\Core\Utility\StringUtility;
 
 /**
- * Generation of TCEform elements of the type "input"
+ * Generation of TCEform elements of the type "input type=text"
  */
 class InputTextElement extends AbstractFormElement {
 
index fecb324..8f4f70d 100644 (file)
@@ -176,8 +176,7 @@ class FormDataCompiler {
                        'inlineFirstPid' => NULL,
                        // This array of fields will be set as hidden-fields instead of rendered normally!
                        // This is used by EditDocumentController to force some field values if set as "overrideVals" in _GP
-                       // @todo: This is more a hack than a clean solution - controllers should take care of that on their own
-                       'hiddenFieldListArray' => [],
+                       'overrideValues' => [],
 
                        // @todo: must be handled / further defined
                        'elementBaseName' => '',
index 718211b..28b1895 100644 (file)
@@ -49,6 +49,7 @@ class NodeFactory {
                'check' => Element\CheckboxElement::class,
                'group' => Element\GroupElement::class,
                'input' => Element\InputTextElement::class,
+               'hidden' => Element\InputHiddenElement::class,
                // rsaInput is defined with a fallback so extensions can use it even if ext:rsaauth is not loaded
                'rsaInput' => Element\InputTextElement::class,
                'imageManipulation' => Element\ImageManipulationElement::class,
@@ -240,4 +241,4 @@ class NodeFactory {
                return GeneralUtility::makeInstance($className, $this, $data);
        }
 
-}
+}
\ No newline at end of file
diff --git a/typo3/sysext/backend/Tests/Unit/Form/Element/InputHiddenElementTest.php b/typo3/sysext/backend/Tests/Unit/Form/Element/InputHiddenElementTest.php
new file mode 100644 (file)
index 0000000..71404eb
--- /dev/null
@@ -0,0 +1,41 @@
+<?php
+namespace typo3\sysext\backend\Tests\Unit\Form\Element;
+/*
+ * 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\Element\InputHiddenElement;
+use TYPO3\CMS\Core\Tests\UnitTestCase;
+
+/**
+ * Tests for InputHiddenElement Form
+ */
+class InputHiddenElementTest extends UnitTestCase {
+
+       /**
+        * @test
+        */
+       public function renderReturnsElementsAsAdditionalHiddenFields() {
+               $data = [
+                               'parameterArray' => [
+                               'itemFormElName' => 'foo',
+                               'itemFormElValue' => 'bar'
+                       ]
+               ];
+               $subject = $this->getAccessibleMock(InputHiddenElement::class, array('dummy'), array(), '', FALSE);
+               $subject->_set('data', $data);
+               $result = $subject->render();
+               $additionalHiddenFieldsResult = array_pop($result['additionalHiddenFields']);
+               $this->assertContains('name="foo"', $additionalHiddenFieldsResult);
+               $this->assertContains('value="bar"', $additionalHiddenFieldsResult);
+               $this->assertContains('type="hidden"', $additionalHiddenFieldsResult);
+       }
+}