[TASK] EXT:form - Use unique ids for multiple forms 50/43750/4
authorRalf Zimmermann <ralf.zimmermann@tritum.de>
Sun, 4 Oct 2015 12:21:17 +0000 (14:21 +0200)
committerBenni Mack <benni@typo3.org>
Tue, 6 Oct 2015 07:27:58 +0000 (09:27 +0200)
Use consecutive ids if more than one form is present on a page.

Resolves: #70211
Releases: master
Change-Id: I2d5f2f27290923ec4c6eaf3ccb06e4ad41815bd3
Reviewed-on: http://review.typo3.org/43750
Reviewed-by: Daniel Goerz <ervaude@gmail.com>
Tested-by: Daniel Goerz <ervaude@gmail.com>
Reviewed-by: Bjoern Jacob <bjoern.jacob@tritum.de>
Tested-by: Bjoern Jacob <bjoern.jacob@tritum.de>
Reviewed-by: Benni Mack <benni@typo3.org>
Tested-by: Benni Mack <benni@typo3.org>
typo3/sysext/form/Classes/Domain/Builder/FormBuilder.php
typo3/sysext/form/Classes/Utility/ElementCounter.php [new file with mode: 0644]

index 6cbb01e..ab1ef00 100644 (file)
@@ -88,9 +88,9 @@ class FormBuilder {
        protected $objectManager;
 
        /**
-        * @var integer
+        * @var \TYPO3\CMS\Form\Utility\ElementCounter
         */
-       protected $elementCounter = 0;
+       protected $elementCounter;
 
        /**
         * @var NULL|\TYPO3\CMS\Extbase\Error\Result
@@ -140,6 +140,14 @@ class FormBuilder {
        }
 
        /**
+        * @param \TYPO3\CMS\Form\Utility\ElementCounter $elementCounter
+        * @return void
+        */
+       public function injectElementCounter(\TYPO3\CMS\Form\Utility\ElementCounter $elementCounter) {
+               $this->elementCounter = $elementCounter;
+       }
+
+       /**
         * @param \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager
         * @return void
         */
@@ -280,7 +288,6 @@ class FormBuilder {
         * @return \TYPO3\CMS\Form\Domain\Model\Element
         */
        protected function createElementObject() {
-               $this->elementCounter++;
                $element = GeneralUtility::makeInstance(Element::class);
                return $element;
        }
@@ -301,7 +308,7 @@ class FormBuilder {
                }
 
                $element->setElementType($elementType);
-               $element->setElementCounter($this->elementCounter);
+               $element->setElementCounter($this->elementCounter->getElementId());
 
                $elementBuilder = ElementBuilder::create($this, $element, $userConfiguredElementTypoScript);
                $elementBuilder->setPartialPaths();
@@ -615,13 +622,4 @@ class FormBuilder {
                return $this->validationErrors;
        }
 
-       /**
-        * Get the current element counter
-        *
-        * @return integer
-        */
-       public function getElementCounter() {
-               return $this->elementCounter;
-       }
-
 }
diff --git a/typo3/sysext/form/Classes/Utility/ElementCounter.php b/typo3/sysext/form/Classes/Utility/ElementCounter.php
new file mode 100644 (file)
index 0000000..86171de
--- /dev/null
@@ -0,0 +1,34 @@
+<?php
+namespace TYPO3\CMS\Form\Utility;
+
+/*
+ * 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!
+ */
+
+class ElementCounter implements \TYPO3\CMS\Core\SingletonInterface {
+
+       /**
+        * @var integer
+        */
+       protected $elementCounter = 0;
+
+       /**
+        * Raise the element counter by one
+        *
+        * @return integer
+        */
+       public function getElementId() {
+               $this->elementCounter++;
+               return $this->elementCounter;
+       }
+
+}