Problem/Motivation

PHP 8.1 comes with support for Backed Enums
It makes sense to be able to use them for select, radio and checkboxes elements as the value for #options

Proposed resolution

Support using an Backed Enum for #options

Remaining tasks

  • Update element documentation for select, checkboxes and radios
    Change record

User interface changes

API changes

Data model changes

Release notes snippet

Issue fork drupal-3249599

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

larowlan created an issue. See original summary.

larowlan’s picture

Issue tags: +PHP 8.1
larowlan’s picture

Version: 9.3.x-dev » 9.4.x-dev
larowlan’s picture

Version: 9.4.x-dev » 10.0.x-dev
dpi’s picture

Title: Add support for PHP 8.1 Enums in Select, Checkboxes, Radio elements » Add support for PHP 8.1 Backed Enums in Select, Checkboxes, Radio elements
Version: 10.0.x-dev » 11.x-dev
dpi’s picture

Component: forms system » options.module

duadua made their first commit to this issue’s fork.

duadua’s picture

Status: Active » Needs work
duadua’s picture

Issue summary: View changes
duadua’s picture

Issue summary: View changes
dpi’s picture

Status: Needs work » Active

@duadua do you have any narrative to add or is your MR pure experimentation?

I'd suggest creating a personal patch issue if its the latter, for example #3099942: [ignore] @dpi's patch testing issue

duadua’s picture

@dpi
That's a great recommendation. I'll do that and experiment a bit more first, before posting a MR into this issue.

duadua’s picture

@dpi

To be honest I am confused whether to create MRs against 10.1.x or 11.x at this point.

larowlan’s picture

Status: Active » Needs work

Left some comments on the MR

We have some failing custom commands here (likely linting)

dieterholvoet’s picture

Issue summary: View changes

ankithashetty made their first commit to this issue’s fork.

acbramley’s picture

In #3538277: Add NodePreview enum and replace usage of DRUPAL_DISABLED, DRUPAL_OPTIONAL and DRUPAL_REQUIRED I'm looking to add a trait that allows you to do this very easily (although not quite like what is being proposed in the current MR).

The OptionsEnumTrait adds ::asOptions to an enum and forces a label method to be implemented. You can then use MyEnum::asOptions in the FAPI #options key, and also for things like field storage definitions with the allowed_values_function key

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

joachim’s picture

We need to make sure this is going to be compatible with #3468351: add enum support to Option field types.