#67880Add count to listNum
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>
}
}