From: Michael Oehlhof Date: Thu, 2 Jul 2015 20:36:43 +0000 (+0200) Subject: [FEATURE] Add returnCount to split X-Git-Tag: 7.4.0~331 X-Git-Url: http://git.typo3.org/Packages/TYPO3.CMS.git/commitdiff_plain/585b18a73dede903eff1f2bc9a683cd0914f2a68 [FEATURE] Add returnCount to split 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 Reviewed-by: Daniel Goerz Tested-by: Daniel Goerz Reviewed-by: Frederic Gaus Tested-by: Frederic Gaus Reviewed-by: Benjamin Mack Tested-by: Benjamin Mack --- 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 index 000000000000..983533704076 --- /dev/null +++ b/typo3/sysext/core/Documentation/Changelog/master/Feature-67880-AddedCountToListNum.rst @@ -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 + } diff --git a/typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php b/typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php index 9a45fe834034..093d2b250861 100644 --- a/typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php +++ b/typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php @@ -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; } diff --git a/typo3/sysext/frontend/Tests/Unit/ContentObject/ContentObjectRendererTest.php b/typo3/sysext/frontend/Tests/Unit/ContentObject/ContentObjectRendererTest.php index d6c021d5b0e7..8f6b9fdafd1f 100644 --- a/typo3/sysext/frontend/Tests/Unit/ContentObject/ContentObjectRendererTest.php +++ b/typo3/sysext/frontend/Tests/Unit/ContentObject/ContentObjectRendererTest.php @@ -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 + ); + } + }