95134[BUGFIX] Allow PHP enums as `<f:form.select>` options
Commit message
Test this change
Run a command in your clone of the TYPO3 Core. Then try what the change does and vote on Verified in Gerrit. How to review a patch
- Check out patch set 11
-
git fetch https://review.typo3.org/Packages/TYPO3.CMS refs/changes/34/95134/11 && git checkout FETCH_HEAD - Cherry-pick onto main
-
git fetch https://review.typo3.org/Packages/TYPO3.CMS refs/changes/34/95134/11 && git cherry-pick FETCH_HEAD
Review
-
Code review CR 0 Needs a vote
-
Verified V 0 Needs a vote
- core-ci +1 CI
Issues 2
Files 2
Review log 3142
Show 1217 earlier messages
-
Christian Spoo uploaded patch set 1 08/06/26, 19:31
-
Christian Spoo uploaded patch set 2 08/06/26, 19:43
-
core-ci voted on patch set 1 V -1 08/06/26, 19:56 Core CI is not happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/106030
-
Christian Spoo uploaded patch set 3 08/06/26, 20:01
-
core-ci voted on patch set 2 V -1 08/06/26, 20:04 Core CI is not happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/106035
-
core-ci voted on patch set 3 V +1 08/06/26, 20:14 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/106041
-
Oli Bartsch commented on patch set 3 08/06/26, 20:21
-
Andreas Wolf voted on patch set 3 CR +1 08/06/26, 20:29
-
Christian Spoo commented on patch set 3 08/06/26, 20:55
SelectViewHelper.php:153ReplyNo specific reason other than readability. 😊 Moving the null check to the beginning in combination with an early bailout seemed logical to me, because the whole method can be skipped in that case. -
Simon Schaufelberger commented on patch set 3 08/06/26, 21:37
SelectViewHelper.php:153ReplyPlease don't use is_null but === null -
Christian Spoo uploaded patch set 4
CR +1V +108/06/26, 21:57 -
Christian Spoo commented on patch set 4 08/06/26, 22:00
SelectViewHelper.php:153ReplyDone -
core-ci voted on patch set 4 V +1 08/06/26, 22:07 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/106077
-
Benni Mack updated the commit message in patch set 5
V +108/07/26, 07:29 -
core-ci voted on patch set 5 V +1 08/07/26, 07:37 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/106100
-
Andreas Wolf voted on patch set 5 CR +1 08/09/26, 23:47
-
Stefan Bürk commented on patch set 5 CR -1 08/10/26, 23:49
SelectViewHelper.php:163This if-block does not make sense for me. If $optionsArgument (`arguments['options']`) is a enum `is_string()` validates to false. And in case it is really a string, calling `cases()` on it would fail - a string does not have methods attached to them. What should be checked/archieved with this block ? Due to missing comment(s) that is not really clear and making this something between deadcode and dangerous code.
-
Stefan Bürk commented on patch set 5 08/10/26, 23:50
SelectViewHelper.php:163ReplySee -> https://onlinephp.io/c/01f63 -
Radostin Milenov uploaded patch set 6
CR +1V +109/13/26, 06:59 -
core-ci voted on patch set 6 V -1 09/13/26, 07:07 Core CI is not happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/108443
-
Radostin Milenov uploaded patch set 7
V -109/13/26, 07:15 -
core-ci voted on patch set 7 V -1 09/13/26, 07:22 Core CI is not happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/108444
-
Radostin Milenov uploaded patch set 8
V -109/13/26, 08:00 -
core-ci voted on patch set 8 V +1 09/13/26, 08:07 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/108445
-
Radostin Milenov commented on patch set 8 CR +1 V +1 09/13/26, 08:20
Code does the job, however i missed an exception in case the enum class couldn't be resolved or was malformed. Empty array wasn't really pointing much even though it would prevend the crash, so i added the exception for that particular case. -
Radostin Milenov commented on patch set 8 09/13/26, 08:26
SelectViewHelper.php:163Replythe cases call on string is correct. Enums are returned as array. -
Stefan Bürk commented on patch set 8 CR -1 09/13/26, 18:36
Thanks @mail@christian-spoo.info for the inital change and also @r.milenov@icloud.com for picking this up and working on this change. I tested this on current and took now the time to put some time on reviewing this change now and for me there are some points needing another round in my eyes: 1. Backed enums get the case name as the option value and backing value as the label - from my side and thinking that's exactly the oposite I would expect it to work and we should invert this for backed enums. Current behaviour also contradicts how TYPO3 core treats backed enums everywhere else. 2. Accepting an enum FQCN for `options` is strictly spoken a new `FEATURE` requiring a `FEATURE` restfile and limits this change to main only. However, I'm not sure if we shouldn't take this as a bugfix instead based on the fact that the introduction of native php enum support missed that here. I guess that we need to discuss this with some other mergers how to take this - so do not change that now. 3. Patchset 6, 7 and 8 are based on the unrelated and unmerged change 95260, which I would take as a missed cleanup of the working environment and we should rebase this away from that parent change to avoid an unrelated relation chain.SelectViewHelper.php:45I'd say we should describe the resulting mapping here: ``` unit-enum: value and label = case name; backed enum: value = backing value, label = case name ``` The reason I think we should add this is, that this ends up in the ViewHelper reference. An enum example in the class docblock would be also help.SelectViewHelper.php:153This early return is redundant - null is not an array (`!is_array()`), traversal nor a string and the next early return would also simply return an empty array. Can be removed. I'd suggest following for the whole block: ```php $optionsArgument = $this->arguments['options']; if (is_string($optionsArgument)) { if (!enum_exists($optionsArgument)) { throw new InvalidArgumentValueException('The enum class "' . $optionsArgument . '" does not exist or is not a valid enum.', <new timestamp>); } $optionsArgument = $optionsArgument::cases(); } if (!is_iterable($optionsArgument)) { return []; } ```SelectViewHelper.php:157This is quite a heavy chain of negated condition and can easily miss-read. I guess we should refactor this to a more easier readable version.SelectViewHelper.php:161The exception code should be the current unix timestamp (`date +%s`) at the time of writing, not an simply incremented code of a near-by code fragment.SelectViewHelper.php:163ReplyI must confess, you are right here, `$string::cases()` works for an enum class-name string, and with the guard in patchset 8 it is safe. My concern was misplaced here. What still remains is the readability of this piece of code, and I made a suggesting to change the code for that.SelectViewHelper.php:167Missing space after `//` - however, that commend does not add anything really usefull.SelectViewHelper.php:200The identifiying value for `BackedEnum's` should be the backing value, not the name. `getOptionValueScalar()` below (line ~297) returns `->value` for the selected value, so option value and selected value never match and a bound backed-enum property is not preselected. `EnumConverter`, `DataMapper` and `UriBuilder` does the same in that case and use the backing value as well. ```suggestion } elseif ($value instanceof \BackedEnum) { $key = $value->value; } elseif ($value instanceof \UnitEnum) { ```SelectViewHelper.php:201The string cast on the name is superflous, it's always a string. ```suggestion $key = $value->name; ```SelectViewHelper.php:217The backing value is a machine value, not a label: an int-backed enum renders `1`, `2`, `3` as labels. I'd use `->name` as the default label for all enums. Custom or translated labels remain possible via `optionLabelField`. ```suggestion $value = $value->name; ```SelectViewHelper.php:219```suggestion $value = $value->name; ``` The string cast on the name is superflous, it's always a string.GenderBackedEnum.php:23Do we really need a new enum here ? There are alreaedy `UserRoleEnum` and `UserRoleBackedEnum` (int) fixtures for exactly this purpose, wired to `UserDomainClass` via `role`/`roleBacked`. I would prefer to reuse them and drop the two new fixtures. An int-backed enum is also the more tellung case for the value/label mapping anyway.GenderEnum.php:23See comment on `GenderBackedEnum` and reuse `UserRoleBackedEnum` instead.SelectViewHelperTest.php:66For me it seams, that `value="value"` has been simply copied from the array test and matches no option, so no selection is tested here, which is exactly where the backed-enum mapping breaks. Please add tests for: * preselection of a bound unit and backed enum property (FQCN and `{cases}`) * `options="{Enum::cases()}" (the case from the forge issue) * the new exception for a non-enum string * an int-backed enumSelectViewHelperTest.php:76Here we have the above critized mapping expectation. For backed enums the value **should** be the value and not the `BackedEnum->name`. -
Stefan Bürk commented on patch set 8 09/13/26, 18:38
Working on it ... stay tuned. -
Stefan Bürk uploaded patch set 9
CR +1V +109/13/26, 18:44 -
Stefan Bürk updated the commit message in patch set 10 09/13/26, 18:45
-
core-ci voted on patch set 9 V +1 09/13/26, 18:53 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/108459
-
core-ci voted on patch set 10 V +1 09/13/26, 18:57 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/108460
-
Stefan Bürk commented on patch set 8 09/13/26, 18:58
Reply
DoneSelectViewHelper.php:45ReplyDoneSelectViewHelper.php:153ReplyDoneSelectViewHelper.php:157ReplyDoneSelectViewHelper.php:161ReplyDoneSelectViewHelper.php:167ReplyDoneSelectViewHelper.php:200ReplyDoneSelectViewHelper.php:201ReplyDoneSelectViewHelper.php:217ReplyDoneSelectViewHelper.php:219ReplyDoneGenderBackedEnum.php:23ReplyDoneGenderEnum.php:23ReplyDoneSelectViewHelperTest.php:66ReplyDoneSelectViewHelperTest.php:76ReplyDone -
Stefan Bürk commented on patch set 10 09/13/26, 19:02
Reply
@mail@christian-spoo.info can you please test/verify that this is still working for you and if you can live with the changes ? (Feel free to leave a +1 testing in that case). @r.milenov@icloud.com I want to invite you to test this change again after my modifications and have a look (review). Thanks on working on this change before.SelectViewHelper.php:161ReplyDoneSelectViewHelper.php:201ReplyDoneSelectViewHelper.php:217ReplyDone -
Radostin Milenov commented on patch set 10 09/14/26, 03:47
Reply
Just did. Works like expected.As you mentioned we need to update the docs and i agree we can pass it as bugfix.FQCN should've been taken into account in the first place and it's strictly spoken not a breaking change. But it is important one so we would need that restfile updated to reflect the current behavior. -
Christian Spoo voted on patch set 10 V +1 09/14/26, 19:28
-
Christian Spoo commented on patch set 10 09/14/26, 19:28
Reply
Fine for me. Thanks @r.milenov@icloud.com! -
Christian Spoo commented on patch set 10 09/14/26, 19:28
Reply
Done -
Radostin Milenov voted on patch set 10 CR +1 V +1 09/15/26, 04:06
-
Christian Spoo uploaded patch set 11
CR +1V +110/07/26, 20:21 -
core-ci voted on patch set 11 V +1 10/07/26, 20:46 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/110405
-
Christian Spoo commented on patch set 11 10/07/26, 20:49
Rebased to fix merge conflicts
SelectViewHelper.php:153