Problem/Motivation

PHP 8.1 comes with support for Enums
It makes sense to be able to use them for allowed values for list_* field types

Proposed resolution

Support using an Enum for allowed values

Enhance list_ field items with a allowed_values_enum setting. Available options will be extracted from that enum automatically.

Remaining tasks

  • Update documentation. Figure out how to best document that
  • Introduce change record for this change?.

User interface changes

API changes

Data model changes

Release notes snippet

Issue fork drupal-3249600

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.

andypost’s picture

Version: 9.4.x-dev » 10.0.x-dev
Related issues: +#3264819: Require PHP 8.1 for Drupal 10.0.0-alpha2

10.0 requires 8.1

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

duadua’s picture

Issue summary: View changes
duadua’s picture

Status: Active » Needs review
andypost’s picture

Status: Needs review » Active

There's no patch or MR to review

andypost’s picture

Status: Active » Needs work
duadua’s picture

Issue summary: View changes
duadua’s picture

Version: 10.0.x-dev » 10.1.x-dev
dieterholvoet’s picture

Title: Add support for PHP 8.1 Enums as allowed values for list_* data types » Add support for PHP 8.1 Enums as allowed values for list_* field types

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

dww’s picture

Love this. But curious how it would work in the Field UI. Is the idea that if a field uses this to manage allowed values, that those parts of the UI where you define the values and labels would disappear or something?

claudiu.cristea’s picture

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.

aaronmchale’s picture

PHP 8.3 added Backed Enums, which opens up the possibility of key/value enums.

So, if we supported Backed Enums, then we essentially have the equivalent of a function which returns an array for the keys/values.

Backed Enums can have methods, like classes, the Enum class could define a label() method which returns the enum value using TranslatableMarkup.

This opens up another possibility where the Enum could have methods that the field could make use of for other things (not sure what yet), but there's potential!

Here's an example of a Status enum that I built to use as the allowed values of a list_string base field.


declare(strict_types=1);

namespace Drupal\my_module;

use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * The available states for an edition.
 */
enum State: string {

  case Draft = 'draft';
  case Published = 'published';
  case Archived = 'archived';

  /**
   * Gets the human-readable label for this state.
   */
  public function label(): TranslatableMarkup {
    return match ($this) {
      self::Draft => new TranslatableMarkup('Draft'),
      self::Published => new TranslatableMarkup('Published'),
      self::Archived => new TranslatableMarkup('Archived'),
    };
  }

  /**
   * Gets the states as options suitable for a list field.
   *
   * @return array<string, \Drupal\Core\StringTranslation\TranslatableMarkup>
   *   The state values keyed by their stored value.
   */
  public static function options(): array {
    $options = [];
    foreach (self::cases() as $state) {
      $options[$state->value] = $state->label();
    }
    return $options;
  }

}

If list fields supported Backed Enums, I wouldn't need the options() method.

But you can see the potential here!