Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX] Use ScopedVariableProvider in CycleViewHelper #830

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/ViewHelpers/CycleViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
namespace TYPO3Fluid\Fluid\ViewHelpers;

use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\Variables\ScopedVariableProvider;
use TYPO3Fluid\Fluid\Core\Variables\StandardVariableProvider;
use TYPO3Fluid\Fluid\Core\ViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
Expand Down Expand Up @@ -98,9 +100,15 @@ public static function renderStatic(array $arguments, \Closure $renderChildrenCl

$currentValue = isset($values[$index]) ? $values[$index] : null;

$renderingContext->getVariableProvider()->add($as, $currentValue);
$scopedVariableProvider = new ScopedVariableProvider(
$renderingContext->getVariableProvider(),
new StandardVariableProvider([$as => $currentValue]),
);
$renderingContext->setVariableProvider($scopedVariableProvider);

$output = $renderChildrenClosure();
$renderingContext->getVariableProvider()->remove($as);

$renderingContext->setVariableProvider($scopedVariableProvider->getGlobalVariableProvider());

$index++;
if (!isset($values[$index])) {
Expand Down
12 changes: 12 additions & 0 deletions tests/Functional/ViewHelpers/CycleViewHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ public static function renderDataProvider(): \Generator
['value' => $value],
'child node content ',
];
$value = ['foo', 'bar', 'baz'];
yield 'variables are restored correctly' => [
'<f:cycle values="{value}" as="cycle"></f:cycle>{cycle}',
['value' => $value, 'cycle' => 'initial'],
'initial',
];
$value = ['foo', 'bar', 'baz'];
yield 'existing variables can be modified and retain the value' => [
'<f:cycle values="{value}" as="cycle"><f:variable name="cycle" value="overwritten" /></f:cycle>{cycle}',
['value' => $value, 'cycle' => 'initial'],
'overwritten',
];
}

/**
Expand Down