Skip to content

Commit 3c55772

Browse files
committed
[BUGFIX] Fix ForViewHelper iteration total for traversables
Use iterator conversion when calculating the total iteration count so non-countable traversable values can be used with the iteration argument. Add functional coverage for an IteratorAggregate that does not implement Countable. related: https://forge.typo3.org/issues/110129
1 parent ef902a5 commit 3c55772

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

src/ViewHelpers/ForViewHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public function render(): string
106106
$iterationData = [
107107
'index' => 0,
108108
'cycle' => 1,
109-
'total' => count($this->arguments['each']),
109+
'total' => count(is_countable($this->arguments['each']) ? $this->arguments['each'] : iterator_to_array($this->arguments['each'])),
110110
];
111111
}
112112
$globalVariableProvider = $this->renderingContext->getVariableProvider();

tests/Functional/ViewHelpers/ForViewHelperTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,19 @@ public static function renderDataProvider(): \Generator
107107
. '</ul>',
108108
];
109109

110+
$value = new class () implements \IteratorAggregate {
111+
public function getIterator(): \Traversable
112+
{
113+
yield 'first' => 'foo';
114+
yield 'second' => 'bar';
115+
}
116+
};
117+
yield 'iterator total works for non countable traversable' => [
118+
'<f:for each="{value}" as="item" iteration="iterator">{item}: {iterator.total}, </f:for>',
119+
['value' => $value],
120+
'foo: 2, bar: 2, ',
121+
];
122+
110123
$value = ['item'];
111124
yield 'iterator not available if not requested' => [
112125
'<f:for each="{value}" as="item">Total: {iterator.total}</f:for>',

0 commit comments

Comments
 (0)