Problem/Motivation

If a enum with numeric values are mapped with SelectWidget value the component validations failed because the value is a string.

Steps to reproduce

Map a component with following property:

props:
  type: object
  properties:
    heading_level:
      title: "Heading level"
      $ref: "ui-patterns://enum"
      enum:
        - 2
        - 3
        - 4
        - 5
        - 6
      "meta:enum":
        2: "h2 (Default)"
        3: h3
        4: h4
        5: h5
        6: h6

Proposed resolution

Not sure where to fix that. In the widget or a Proptype adapter?

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

christian.wiedemann created an issue. See original summary.

g4mbini’s picture

Looks like a UI Suite DaisyUI yml definiton :)))

A friend told me this same issue on another project (not UI Suite DaisyUI)... But when I tested with UI Suite DaisyUI, I didn't have that problem on my D11 setup ...

I'll ping him @b.khouy !

b.khouy’s picture

Assigned: Unassigned » b.khouy
Status: Active » Needs work

@g4mbini
I encountered the same issue. As a temporary solution, I had to change my enum values to strings (e.g., use '1' instead of 1). In general, it seems that Drupal select widgets always return strings, so the UI Patterns module should account for this. I'll take some time to reproduce the issue and work on it.

pdureau’s picture

Title: [BETA-2] Number Enums which are mapped with SelectWidget » [2.0.0-beta2] Number Enums which are mapped with SelectWidget

Let's be careful with this one.

  • A new proptype adapter? It may be overkill
  • In SelectWidget? the best place to do the change. It may be the select form element which is casting the integer value as a string
  • in EnumPropType::normalize()? Look like a good place too, but the method is static and doesn't have access to the prop definition

So, what can we do in SelectWidget ? a #process callback?

pdureau’s picture

Maybe we can leverage SourceInterface::getPropValue()

Proposal:

  public function getPropValue(): mixed {
    $value = $this->getSetting('value');
    $enum = $this->propDefinition['enum'];
    // Select form element is always storing values as strings, but we want
    // a value with the same typing as the related enum value.
    return match (TRUE) {
      in_array($value, $enum, TRUE) => $value,
      in_array((int) $value, $enum, TRUE)  => (int) $value,
      in_array((float) $value, $enum, TRUE) => (float) $value,
      default => $value,
    };
  }

christian.wiedemann’s picture

But we need that for all enums independent of the source. Why not move that logic to an proptype adapter for all enums of type integer

pdureau’s picture

Because it is the source which is making a mess here.

What would be the prop type adapter you are thinking about?

christian.wiedemann’s picture

Or a EnumSourceBase class with getOptions and and we overwrite the getValue method and we do the cast there.

b.khouy’s picture

Assigned: b.khouy » Unassigned
Status: Needs work » Closed (cannot reproduce)

I took the time to reproduce this with the latest updates in 2.0.x but couldn't reproduce the issue.
It seems everything is working as expected, so I believe we can close this issue with the status "cannot reproduce"
@christian.wiedemann, you may want to test again with the latest 2.0.x and feel free to reopen in case the problem persists

pdureau’s picture

Assigned: Unassigned » christian.wiedemann
Status: Closed (cannot reproduce) » Needs work

This is surprising, too goof to be to true :)

Let's check again.

pdureau’s picture

So, the proposal:

  • let's use SourceInterface::getPropValue() with the Pierre's proposal in #5
  • but let's do it in a EnumSourceBase as proposed by Christian. SelectWidget is the only plugin extending EnumSourceBase for now

christian.wiedemann’s picture

Assigned: christian.wiedemann » pdureau
Status: Needs work » Needs review
pdureau’s picture

Assigned: pdureau » Unassigned
Status: Needs review » Fixed

pdureau’s picture

Status: Fixed » Closed (fixed)
christian.wiedemann’s picture