[FEATURE] Add returnCount to split 74/40874/7
authorMichael Oehlhof <typo3@oehlhof.de>
Thu, 2 Jul 2015 20:36:43 +0000 (22:36 +0200)
committerBenjamin Mack <benni@typo3.org>
Wed, 15 Jul 2015 18:35:59 +0000 (20:35 +0200)
returnCount works like returnKey. It interupts
further processing and directly returns the requested
result. In our case the amount of elements after
the splitting.

Resolves: #67880
Releases: master
Change-Id: Icc50d1e0e4a2ce4d7566ce6f390b09f5e0608f95
Reviewed-on: http://review.typo3.org/40874
Reviewed-by: Andreas Fernandez <typo3@scripting-base.de>
Reviewed-by: Daniel Goerz <ervaude@gmail.com>
Tested-by: Daniel Goerz <ervaude@gmail.com>
Reviewed-by: Frederic Gaus <frederic.gaus@flagbit.de>
Tested-by: Frederic Gaus <frederic.gaus@flagbit.de>
Reviewed-by: Benjamin Mack <benni@typo3.org>
Tested-by: Benjamin Mack <benni@typo3.org>
typo3/sysext/core/Documentation/Changelog/master/Feature-67880-AddedCountToListNum.rst [new file with mode: 0644]
typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php
typo3/sysext/frontend/Tests/Unit/ContentObject/ContentObjectRendererTest.php

diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-67880-AddedCountToListNum.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-67880-AddedCountToListNum.rst
new file mode 100644 (file)
index 0000000..9835337
--- /dev/null
@@ -0,0 +1,23 @@
+========================================
+Feature: #67880 - Added count to listNum
+========================================
+
+Description
+===========
+
+A new property ``returnCount`` is added to the stdWrap property ``split``.
+
+When dealing with comma separated values like the content of field:records or similar,
+in some cases we need to know, how many items are present inside the csv.
+
+Example:
+
+.. code-block:: typoscript
+
+       # should return 9
+       1 = TEXT
+       1 {
+               value = x,y,z,1,2,3,a,b,c
+               split.token = ,
+               split.returnCount = 1
+       }
index 9a45fe8..093d2b2 100644 (file)
@@ -4691,46 +4691,56 @@ class ContentObjectRenderer {
                if ($conf['token'] === '') {
                        return $value;
                }
-               $conf['max'] = isset($conf['max.']) ? (int)$this->stdWrap($conf['max'], $conf['max.']) : (int)$conf['max'];
-               $conf['min'] = isset($conf['min.']) ? (int)$this->stdWrap($conf['min'], $conf['min.']) : (int)$conf['min'];
                $valArr = explode($conf['token'], $value);
+
+               // return value directly by returnKey. No further processing
                if (!empty($valArr) && (MathUtility::canBeInterpretedAsInteger($conf['returnKey']) || $conf['returnKey.'])) {
                        $key = isset($conf['returnKey.']) ? (int)$this->stdWrap($conf['returnKey'], $conf['returnKey.']) : (int)$conf['returnKey'];
-                       $content = isset($valArr[$key]) ? $valArr[$key] : '';
-               } else {
-                       // calculate splitCount
-                       $splitCount = count($valArr);
-                       $max = isset($conf['max.']) ? $this->stdWrap($conf['max'], $conf['max.']) : $conf['max'];
-                       if ($max && $splitCount > $max) {
-                               $splitCount = $max;
-                       }
-                       $min = isset($conf['min.']) ? $this->stdWrap($conf['min'], $conf['min.']) : $conf['min'];
-                       if ($min && $splitCount < $min) {
-                               $splitCount = $min;
-                       }
-                       $wrap = isset($conf['wrap.']) ? $this->stdWrap($conf['wrap'], $conf['wrap.']) : $conf['wrap'];
-                       $cObjNum = isset($conf['cObjNum.']) ? $this->stdWrap($conf['cObjNum'], $conf['cObjNum.']) : $conf['cObjNum'];
-                       if ($wrap || $cObjNum) {
-                               $splitArr = array();
-                               $splitArr['wrap'] = $wrap;
-                               $splitArr['cObjNum'] = $cObjNum;
-                               $splitArr = $GLOBALS['TSFE']->tmpl->splitConfArray($splitArr, $splitCount);
-                       }
-                       $content = '';
-                       for ($a = 0; $a < $splitCount; $a++) {
-                               $GLOBALS['TSFE']->register['SPLIT_COUNT'] = $a;
-                               $value = '' . $valArr[$a];
-                               $this->data[$this->currentValKey] = $value;
-                               if ($splitArr[$a]['cObjNum']) {
-                                       $objName = (int)$splitArr[$a]['cObjNum'];
-                                       $value = isset($conf[$objName . '.']) ? $this->stdWrap($this->cObjGet($conf[$objName . '.'], $objName . '.'), $conf[$objName . '.']) : $this->cObjGet($conf[$objName . '.'], $objName . '.');
-                               }
-                               $wrap = isset($splitArr[$a]['wrap.']) ? $this->stdWrap($splitArr[$a]['wrap'], $splitArr[$a]['wrap.']) : $splitArr[$a]['wrap'];
-                               if ($wrap) {
-                                       $value = $this->wrap($value, $wrap);
-                               }
-                               $content .= $value;
-                       }
+                       return isset($valArr[$key]) ? $valArr[$key] : '';
+               }
+
+               // return the amount of elements. No further processing
+               if (!empty($valArr) && ($conf['returnCount'] || $conf['returnCount.'])) {
+                       $returnCount = isset($conf['returnCount.']) ? (bool)$this->stdWrap($conf['returnCount'], $conf['returnCount.']) : (bool)$conf['returnCount'];
+                       return $returnCount ? count($valArr) : 0;
+               }
+
+               // start further processing of the values
+               $conf['max'] = isset($conf['max.']) ? (int)$this->stdWrap($conf['max'], $conf['max.']) : (int)$conf['max'];
+               $conf['min'] = isset($conf['min.']) ? (int)$this->stdWrap($conf['min'], $conf['min.']) : (int)$conf['min'];
+
+               // calculate splitCount
+               $splitCount = count($valArr);
+               $max = isset($conf['max.']) ? $this->stdWrap($conf['max'], $conf['max.']) : $conf['max'];
+               if ($max && $splitCount > $max) {
+                       $splitCount = $max;
+               }
+               $min = isset($conf['min.']) ? $this->stdWrap($conf['min'], $conf['min.']) : $conf['min'];
+               if ($min && $splitCount < $min) {
+                       $splitCount = $min;
+               }
+               $wrap = isset($conf['wrap.']) ? $this->stdWrap($conf['wrap'], $conf['wrap.']) : $conf['wrap'];
+               $cObjNum = isset($conf['cObjNum.']) ? $this->stdWrap($conf['cObjNum'], $conf['cObjNum.']) : $conf['cObjNum'];
+               $splitArr = array();
+               if ($wrap || $cObjNum) {
+                       $splitArr['wrap'] = $wrap;
+                       $splitArr['cObjNum'] = $cObjNum;
+                       $splitArr = $GLOBALS['TSFE']->tmpl->splitConfArray($splitArr, $splitCount);
+               }
+               $content = '';
+               for ($a = 0; $a < $splitCount; $a++) {
+                       $GLOBALS['TSFE']->register['SPLIT_COUNT'] = $a;
+                       $value = '' . $valArr[$a];
+                       $this->data[$this->currentValKey] = $value;
+                       if ($splitArr[$a]['cObjNum']) {
+                               $objName = (int)$splitArr[$a]['cObjNum'];
+                               $value = isset($conf[$objName . '.']) ? $this->stdWrap($this->cObjGet($conf[$objName . '.'], $objName . '.'), $conf[$objName . '.']) : $this->cObjGet($conf[$objName . '.'], $objName . '.');
+                       }
+                       $wrap = isset($splitArr[$a]['wrap.']) ? $this->stdWrap($splitArr[$a]['wrap'], $splitArr[$a]['wrap.']) : $splitArr[$a]['wrap'];
+                       if ($wrap) {
+                               $value = $this->wrap($value, $wrap);
+                       }
+                       $content .= $value;
                }
                return $content;
        }
index d6c021d..8f6b9fd 100644 (file)
@@ -3549,4 +3549,20 @@ class ContentObjectRendererTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                $this->assertEquals($expectedResult, $this->subject->typoLink($linkText, $configuration));
        }
 
+       /**
+        * @test
+        */
+       public function stdWrap_splitObjReturnsCount() {
+               $conf = array(
+                       'token' => ',',
+                       'returnCount' => 1
+               );
+               $expectedResult = 5;
+               $amountOfEntries = $this->subject->splitObj('1, 2, 3, 4, 5', $conf);
+               $this->assertSame(
+                       $expectedResult,
+                       $amountOfEntries
+               );
+       }
+
 }