[2.0.0-beta2] Number Enums which are mapped with SelectWidget

Created on 10 September 2024, 4 months ago
Updated 15 September 2024, 4 months ago

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?

πŸ› Bug report
Status

Fixed

Version

2.0

Component

Code

Created by

πŸ‡©πŸ‡ͺGermany Christian.wiedemann

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @Christian.wiedemann
  • πŸ‡«πŸ‡·France G4MBINI BΓ¨gles

    Looks like a DaisyUI yml definiton :)))

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

  • Assigned to b.khouy
  • Status changed to Needs work 4 months ago
  • πŸ‡²πŸ‡¦Morocco b.khouy πŸ‡²πŸ‡¦ Morocco

    @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.

  • πŸ‡«πŸ‡·France pdureau Paris

    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?

  • πŸ‡«πŸ‡·France pdureau Paris

    Maybe we can leverage SourceInterface::getPropValue()

    Proposal:

      public function getPropValue(): mixed {
        $value = $this->getSetting('value');
        $enum = $this->propDefinition['enum'];
        // Select form element is alwyas 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,
        };
      }
    
    
  • πŸ‡©πŸ‡ͺGermany Christian.wiedemann

    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

  • πŸ‡«πŸ‡·France pdureau Paris

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

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

  • πŸ‡©πŸ‡ͺGermany Christian.wiedemann

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

  • Issue was unassigned.
  • Status changed to Closed: cannot reproduce 4 months ago
  • πŸ‡²πŸ‡¦Morocco b.khouy πŸ‡²πŸ‡¦ Morocco

    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

  • Assigned to Christian.wiedemann
  • Status changed to Needs work 4 months ago
  • πŸ‡«πŸ‡·France pdureau Paris

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

    Let's check again.

  • πŸ‡«πŸ‡·France pdureau Paris

    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
  • Merge request !205Handling converting β†’ (Merged) created by Christian.wiedemann
  • Assigned to pdureau
  • Status changed to Needs review 4 months ago
  • Issue was unassigned.
  • Status changed to Fixed 4 months ago
  • πŸ‡«πŸ‡·France pdureau Paris
  • Status changed to Fixed 4 months ago
  • πŸ‡«πŸ‡·France pdureau Paris
Production build 0.71.5 2024