Feature #67880
closedAdd count to listNum
100%
Description
I would like to extend listnum by the following function: count
Quiet often we are dealing with comma separated things like the content of field:records or similiar values.
In some cases we need to know how many items are present inside the csv. Until now I used a simple REGISTER:counter inside a split to determine the amount or sql with LENGTH and REPLACE when extracting from database.
If listNum could just return the amount of items, this wolud be awesome!
I would like to propose the following changes to typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php
I only added those three lines to public function listNum. Maybe it its enough to achieve count functionality.
// Return the amout of items, if requested if ($listNum === 'count') { return count($temp); }
Here is the complete part of public function listNum:
public function listNum($content, $listNum, $char) { $char = $char ?: ','; if (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($char)) { $char = chr($char); } $temp = explode($char, $content); $last = '' . (count($temp) - 1); // Take a random item if requested if ($listNum === 'rand') { $listNum = rand(0, count($temp) - 1); } $index = $this->calc(str_ireplace('last', $last, $listNum)); // Return the amout of items, if requested if ($listNum === 'count') { return count($temp); } else { return $temp[$index]; } }
Here is a typoscript example how to use/test it. After patch is set the output should be 9 in the first row. All follwing outputs should contain a true().
testListnum = PAGE testListnum { # should return 9 1 = TEXT 1 { value = x,y,z,1,2,3,a,b,c listNum = count wrap = |<br> } # should be c 3 = TEXT 3 { current = 1 setCurrent = x,y,z,1,2,3,a,b,c setCurrent { listNum = last } override = true (listNum = last) override { if { value.data = CURRENT equals = c } } wrap = |<br> } # should be b 4 = TEXT 4 { current = 1 setCurrent = x,y,z,1,2,3,a,b,c setCurrent { listNum = last - 1 } override = true (listNum = last - 1) override { if { value.data = CURRENT equals = b } } wrap = |<br> } # should be x 5 = TEXT 5 { current = 1 setCurrent = x,y,z,1,2,3,a,b,c setCurrent { listNum = 0 } override = true (listNum = 0) override { if { value.data = CURRENT equals = x } } wrap = |<br> } # should be 2 6 = TEXT 6 { current = 1 setCurrent = x,y,z,1,2,3,a,b,c setCurrent { listNum = 4 } override = true (listNum = 4) override { if { value.data = CURRENT equals = 2 } } wrap = |<br> } # should be inside list 7 = TEXT 7 { current = 1 setCurrent = x,y,z,1,2,3,a,b,c setCurrent { listNum = rand } override = true (listNum = rand) override { if { value = x,y,z,1,2,3,a,b,c isInList.data = CURRENT } } wrap = |<br> } }
Updated by Christian Stern over 9 years ago
I just found out, that retruning count($temp) is not enough. $content could be empty, thus count($temp) will retun 1 which would be wrong.
if ($listNum === 'count') { if (!empty($content)) { return count($temp); } else { return 0; } }
Updated by Gerrit Code Review over 9 years ago
- Status changed from New to Under Review
Patch set 1 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at http://review.typo3.org/40874
Updated by Gerrit Code Review over 9 years ago
Patch set 2 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at http://review.typo3.org/40874
Updated by Gerrit Code Review over 9 years ago
Patch set 3 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at http://review.typo3.org/40874
Updated by Alexander Opitz over 9 years ago
- Target version changed from 6.2.14 to 7.4 (Backend)
Not for 6.2 branch
Updated by Gerrit Code Review over 9 years ago
Patch set 4 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at http://review.typo3.org/40874
Updated by Gerrit Code Review over 9 years ago
Patch set 5 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at http://review.typo3.org/40874
Updated by Gerrit Code Review over 9 years ago
Patch set 6 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at http://review.typo3.org/40874
Updated by Michael Oehlhof over 9 years ago
- Status changed from Under Review to Resolved
- % Done changed from 0 to 100
Applied in changeset 585b18a73dede903eff1f2bc9a683cd0914f2a68.
Updated by Riccardo De Contardi over 7 years ago
- Status changed from Resolved to Closed