Skip to content

Commit bbc15e1

Browse files
authored
[FEATURE] Make IsFieldEnabledViewHelper configurable (#2059)
1 parent c240af9 commit bbc15e1

File tree

3 files changed

+97
-19
lines changed

3 files changed

+97
-19
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
77

88
### Added
99

10+
- Make configuration key for `IsFieldEnabledViewHelper` configurable (#2059)
11+
1012
### Changed
1113

1214
### Deprecated

Classes/ViewHelpers/IsFieldEnabledViewHelper.php

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
* You can provide either a single field name or multiple field names separated by the pipe character (which serves
1616
* as a logical OR).
1717
*
18-
* The name of the fields in the settings carrying the list of enabled fields needs to be provided in the
19-
* :php:`SETTING_FOR_ENABLED_FIELDS` constant.
18+
* The name of the fields in the settings carrying the list of enabled fields can be changed with the
19+
* `configurationKey` argument.
2020
*
2121
* Examples
2222
* ========
@@ -56,23 +56,30 @@
5656
*/
5757
class IsFieldEnabledViewHelper extends AbstractConditionViewHelper
5858
{
59-
/**
60-
* @var non-empty-string
61-
*/
62-
protected const SETTING_FOR_ENABLED_FIELDS = 'fieldsToShow';
63-
6459
public function initializeArguments(): void
6560
{
6661
parent::initializeArguments();
6762
$this->registerArgument('fieldName', 'string', 'The name(s) of the fields to check, separated by |.', true);
63+
$this->registerArgument('configurationKey', 'string', 'The configuration key to use.', false, 'fieldsToShow');
6864
}
6965

7066
/**
7167
* @param array<string, mixed> $arguments
7268
*/
7369
public static function verdict(array $arguments, RenderingContextInterface $renderingContext): bool
7470
{
75-
$enabledFields = self::getEnabledFields($renderingContext);
71+
$configurationKey = $arguments['configurationKey'] ?? null;
72+
if (!\is_string($configurationKey)) {
73+
throw new \UnexpectedValueException(
74+
'The variable "configurationKey" must be a string, but was: ' . \gettype($configurationKey),
75+
1743708980
76+
);
77+
}
78+
if ($configurationKey === '') {
79+
throw new \UnexpectedValueException('The variable "configurationKey" must not be empty.', 1743709004);
80+
}
81+
82+
$enabledFields = self::getEnabledFields($renderingContext, $configurationKey);
7683

7784
$verdict = false;
7885
foreach (self::getFieldsToCheck($arguments) as $fieldName) {
@@ -106,35 +113,35 @@ private static function getFieldsToCheck(array $arguments): array
106113
throw new \InvalidArgumentException('The argument "fieldName" must not be empty.', 1_651_155_957);
107114
}
108115

109-
$result = GeneralUtility::trimExplode('|', $fieldsNamesArgument, true);
110-
111-
return $result;
116+
return GeneralUtility::trimExplode('|', $fieldsNamesArgument, true);
112117
}
113118

114119
/**
120+
* @param non-empty-string $configurationKey
121+
*
115122
* @return list<non-empty-string>
116123
*
117124
* @throws \UnexpectedValueException
118125
*/
119-
private static function getEnabledFields(RenderingContextInterface $renderingContext): array
120-
{
126+
private static function getEnabledFields(
127+
RenderingContextInterface $renderingContext,
128+
string $configurationKey
129+
): array {
121130
$settings = $renderingContext->getVariableProvider()->get('settings');
122131
if (!\is_array($settings)) {
123132
throw new \UnexpectedValueException('No settings in the variable container found.', 1_651_153_736);
124133
}
125-
126-
$enabledFieldsVariable = self::SETTING_FOR_ENABLED_FIELDS;
127-
if (!isset($settings[$enabledFieldsVariable])) {
134+
if (!isset($settings[$configurationKey])) {
128135
throw new \UnexpectedValueException(
129-
'No field "' . $enabledFieldsVariable . '" in settings found.',
136+
'No field "' . $configurationKey . '" in settings found.',
130137
1_651_154_598
131138
);
132139
}
133140

134-
$enabledFieldsConfiguration = $settings[$enabledFieldsVariable];
141+
$enabledFieldsConfiguration = $settings[$configurationKey];
135142
if (!\is_string($enabledFieldsConfiguration)) {
136143
throw new \UnexpectedValueException(
137-
'The setting "' . $enabledFieldsVariable . '" needs to be a string.',
144+
'The setting "' . $configurationKey . '" needs to be a string.',
138145
1_651_155_151
139146
);
140147
}

Tests/Functional/ViewHelpers/IsFieldEnabledViewHelperTest.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,4 +246,73 @@ public function renderEscapesChildren(): void
246246

247247
self::assertStringContainsString('a&amp;b', $result);
248248
}
249+
250+
/**
251+
* @test
252+
*/
253+
public function renderForBothRequestedConfiguredFieldsEnabledRendersThenChild(): void
254+
{
255+
$this->variableProvider->add('settings', ['columns' => 'company,name']);
256+
257+
$html = '<oelib:isFieldEnabled configurationKey="columns" fieldName="company|name" then="THEN" else="ELSE"/>';
258+
$result = $this->renderViewHelper($html);
259+
260+
self::assertStringContainsString('THEN', $result);
261+
}
262+
263+
/**
264+
* @test
265+
*/
266+
public function renderForSingleRequestedConfiguredFieldEnabledRendersThenChild(): void
267+
{
268+
$this->variableProvider->add('settings', ['columns' => 'company']);
269+
270+
$html = '<oelib:isFieldEnabled configurationKey="columns" fieldName="company" then="THEN" else="ELSE"/>';
271+
$result = $this->renderViewHelper($html);
272+
273+
self::assertStringContainsString('THEN', $result);
274+
}
275+
276+
/**
277+
* @test
278+
*/
279+
public function renderForRequestedConfiguredFieldNotEnabledRendersElseChild(): void
280+
{
281+
$this->variableProvider->add('settings', ['columns' => 'company']);
282+
283+
$html = '<oelib:isFieldEnabled configurationKey="columns" fieldName="name" then="THEN" else="ELSE"/>';
284+
$result = $this->renderViewHelper($html);
285+
286+
self::assertStringContainsString('ELSE', $result);
287+
}
288+
289+
/**
290+
* @test
291+
*/
292+
public function renderForNonStringConfigurationKeyThrowsException(): void
293+
{
294+
$this->expectException(\UnexpectedValueException::class);
295+
$this->expectExceptionMessage('The variable "configurationKey" must be a string, but was: array');
296+
$this->expectExceptionCode(1743708980);
297+
298+
$this->variableProvider->add('settings', ['fieldsToShow' => 'name']);
299+
300+
$html = '<oelib:isFieldEnabled configurationKey="{0: 1}" fieldName="name" then="THEN" else="ELSE"/>';
301+
$this->renderViewHelper($html);
302+
}
303+
304+
/**
305+
* @test
306+
*/
307+
public function renderForEmptyConfigurationKeyThrowsException(): void
308+
{
309+
$this->expectException(\UnexpectedValueException::class);
310+
$this->expectExceptionMessage('The variable "configurationKey" must not be empty.');
311+
$this->expectExceptionCode(1743709004);
312+
313+
$this->variableProvider->add('settings', ['fieldsToShow' => 'name']);
314+
315+
$html = '<oelib:isFieldEnabled configurationKey="" fieldName="name" then="THEN" else="ELSE"/>';
316+
$this->renderViewHelper($html);
317+
}
249318
}

0 commit comments

Comments
 (0)