|
15 | 15 | * You can provide either a single field name or multiple field names separated by the pipe character (which serves |
16 | 16 | * as a logical OR). |
17 | 17 | * |
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. |
20 | 20 | * |
21 | 21 | * Examples |
22 | 22 | * ======== |
|
56 | 56 | */ |
57 | 57 | class IsFieldEnabledViewHelper extends AbstractConditionViewHelper |
58 | 58 | { |
59 | | - /** |
60 | | - * @var non-empty-string |
61 | | - */ |
62 | | - protected const SETTING_FOR_ENABLED_FIELDS = 'fieldsToShow'; |
63 | | - |
64 | 59 | public function initializeArguments(): void |
65 | 60 | { |
66 | 61 | parent::initializeArguments(); |
67 | 62 | $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'); |
68 | 64 | } |
69 | 65 |
|
70 | 66 | /** |
71 | 67 | * @param array<string, mixed> $arguments |
72 | 68 | */ |
73 | 69 | public static function verdict(array $arguments, RenderingContextInterface $renderingContext): bool |
74 | 70 | { |
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); |
76 | 83 |
|
77 | 84 | $verdict = false; |
78 | 85 | foreach (self::getFieldsToCheck($arguments) as $fieldName) { |
@@ -106,35 +113,35 @@ private static function getFieldsToCheck(array $arguments): array |
106 | 113 | throw new \InvalidArgumentException('The argument "fieldName" must not be empty.', 1_651_155_957); |
107 | 114 | } |
108 | 115 |
|
109 | | - $result = GeneralUtility::trimExplode('|', $fieldsNamesArgument, true); |
110 | | - |
111 | | - return $result; |
| 116 | + return GeneralUtility::trimExplode('|', $fieldsNamesArgument, true); |
112 | 117 | } |
113 | 118 |
|
114 | 119 | /** |
| 120 | + * @param non-empty-string $configurationKey |
| 121 | + * |
115 | 122 | * @return list<non-empty-string> |
116 | 123 | * |
117 | 124 | * @throws \UnexpectedValueException |
118 | 125 | */ |
119 | | - private static function getEnabledFields(RenderingContextInterface $renderingContext): array |
120 | | - { |
| 126 | + private static function getEnabledFields( |
| 127 | + RenderingContextInterface $renderingContext, |
| 128 | + string $configurationKey |
| 129 | + ): array { |
121 | 130 | $settings = $renderingContext->getVariableProvider()->get('settings'); |
122 | 131 | if (!\is_array($settings)) { |
123 | 132 | throw new \UnexpectedValueException('No settings in the variable container found.', 1_651_153_736); |
124 | 133 | } |
125 | | - |
126 | | - $enabledFieldsVariable = self::SETTING_FOR_ENABLED_FIELDS; |
127 | | - if (!isset($settings[$enabledFieldsVariable])) { |
| 134 | + if (!isset($settings[$configurationKey])) { |
128 | 135 | throw new \UnexpectedValueException( |
129 | | - 'No field "' . $enabledFieldsVariable . '" in settings found.', |
| 136 | + 'No field "' . $configurationKey . '" in settings found.', |
130 | 137 | 1_651_154_598 |
131 | 138 | ); |
132 | 139 | } |
133 | 140 |
|
134 | | - $enabledFieldsConfiguration = $settings[$enabledFieldsVariable]; |
| 141 | + $enabledFieldsConfiguration = $settings[$configurationKey]; |
135 | 142 | if (!\is_string($enabledFieldsConfiguration)) { |
136 | 143 | throw new \UnexpectedValueException( |
137 | | - 'The setting "' . $enabledFieldsVariable . '" needs to be a string.', |
| 144 | + 'The setting "' . $configurationKey . '" needs to be a string.', |
138 | 145 | 1_651_155_151 |
139 | 146 | ); |
140 | 147 | } |
|
0 commit comments