Leap ahead of #3493070 in core: SDC `enum` props should have human-readable labels: use `meta:enum`

Created on 1 April 2025, about 1 month ago

Overview

Enum vales do not have (translatable) labels.

We need human-readable equivalents, and those equivalents must be translatable (using Drupal's interface translation mechanism).

āš ļø This should not be XB-specific.

āš ļø Out-of-scope here: translatability. Nothing in XB is translatable yet today. This will add the necessary infrastructure to make it feasible later šŸ‘

Proposed resolution

  1. Fix this in core first: implement #3493070-10: SDC `enum` props should have translatable labels: use `meta:enum` → .
  2. Then, port that core MR to XB, with the following changes:
    1. XB should perform the equivalent of these in \Drupal\experience_builder\ComponentMetadataRequirementsChecker:
      1. INFRA: Update \Drupal\Core\Theme\Component\ComponentMetadata::parseSchemaInfo() to trigger a deprecation error when an enum is encountered without a corresponding meta:enum
      2. INFRA: Update \Drupal\Core\Theme\Component\ComponentMetadata::parseSchemaInfo() to trigger a \LogicException when a meta:enum is encountered whose keys do not match (i.e. are a subset or superset) the values listed in the corresponding enum.

      … but rather than deprecating or a logic exception, it will simply become a hard requirement for XB.

    2. XB must support this not only for SDCs, but also for its "in-browser code components" (aka JavaScriptComponent config entities), which use the same metadata structure as SDCs.
    3. Modify \Drupal\Tests\experience_builder\Functional\XbConfigEntityHttpApiTest::testJavaScriptComponent() to also create a enum prop. This should first fail validation (422 response), then upon adding the matching meta:enum, it should pass validation.

      This is XB's equivalent for

      1. TEST: kernel test asserting that a meta:enum not matching the enum triggers a \LogicException
    4. XB must change the logic in \Drupal\experience_builder\JsonSchemaInterpreter\SdcPropJsonSchemaType::toDataTypeShapeRequirements() to do the equivalent of
      1. INFRA: add a getEnumOptions(string $prop): array<string, TranslatableMarkup> method to \Drupal\Core\Theme\Component\ComponentMetadata inspired by ui_patterns' \Drupal\ui_patterns\EnumTrait::getEnumOptions(). (The thing that ui_patterns does not yet do is pass it through Drupal's t().)

      That means changing e.g.

            SdcPropJsonSchemaType::STRING => match (TRUE) {
              array_key_exists('enum', $schema) => new DataTypeShapeRequirement('Choice', [
                'choices' => $schema['enum'],
              ], NULL),
      

      to actually use the meta:enum values.

      (Note that here, they must NOT be translated — translation must happen at runtime — this corresponds to a FieldConfig config entity aka configuration.)

    5. Now update ui/tests/e2e/prop-types.cy.js to prove that the labels for the enum indeed appear in the UI, by updating the it('Enum (select) - string', () => { test case.

      This is XB's equivalent for

      1. TEST: kernel test asserting that 2 identical type: string, enum: […] props can have different translations for the same enum values. For example: a '' enum value results in Same window for the target prop and in None for a rel prop.
        → verifies it works end-to-end, and supports translation contexts

User interface changes

  • Any SDC that has enum without the necessary meta:enum will no longer be available to XB users, and will now appear in the list of disabled XB components at /admin/structure/component/status, with an explanation of why.
  • Any SDC (or code component) that does have both enum and a matching meta:enum will now see those labels in the component inputs form on the right-hand side.
šŸ“Œ Task
Status

Active

Version

0.0

Component

Component sources

Created by

šŸ‡§šŸ‡ŖBelgium wim leers Ghent šŸ‡§šŸ‡ŖšŸ‡ŖšŸ‡ŗ

Live updates comments and jobs are added and updated live.
  • Usability

    Makes Drupal easier to use. Preferred over UX, D7UX, etc.

Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @wim leers
  • šŸ‡§šŸ‡ŖBelgium wim leers Ghent šŸ‡§šŸ‡ŖšŸ‡ŖšŸ‡ŗ

    Config schema changes necessary for XB's JavaScriptComponent config entities attached to help get this going šŸ‘

  • šŸ‡§šŸ‡ŖBelgium wim leers Ghent šŸ‡§šŸ‡ŖšŸ‡ŖšŸ‡ŗ
  • šŸ‡§šŸ‡ŖBelgium wim leers Ghent šŸ‡§šŸ‡ŖšŸ‡ŖšŸ‡ŗ

    Fixing markup.

    And crediting @pdureau, for our collaboration at DrupalCon Atlanta to ensure XB and UI Patterns 2 are aligned 😊

  • First commit to issue fork.
  • Pipeline finished with Failed
    about 1 month ago
    Total: 1766s
    #473001
  • šŸ‡§šŸ‡ŖBelgium wim leers Ghent šŸ‡§šŸ‡ŖšŸ‡ŖšŸ‡ŗ

    Exciting to see ✨ Enum vales do not have translatable labels Active be ready for review! Now let's get this going 😁 (Because XB can't wait for core to ship this in a release.)

  • Pipeline finished with Failed
    29 days ago
    Total: 1870s
    #474743
  • Pipeline finished with Failed
    28 days ago
    Total: 1708s
    #475855
  • Pipeline finished with Failed
    10 days ago
    #488904
  • Pipeline finished with Failed
    10 days ago
    #489079
  • Pipeline finished with Failed
    10 days ago
    #489095
  • Pipeline finished with Failed
    10 days ago
    Total: 612s
    #489351
  • Pipeline finished with Failed
    8 days ago
    #491105
  • Pipeline finished with Running
    8 days ago
    #491232
  • Pipeline finished with Failed
    8 days ago
    Total: 630s
    #491333
  • Pipeline finished with Failed
    8 days ago
    Total: 523s
    #491387
  • Pipeline finished with Failed
    8 days ago
    Total: 602s
    #491424
  • Pipeline finished with Failed
    8 days ago
    Total: 643s
    #491462
  • Pipeline finished with Failed
    7 days ago
    Total: 3222s
    #492201
  • Pipeline finished with Failed
    7 days ago
    Total: 721s
    #492327
  • Pipeline finished with Success
    7 days ago
    Total: 672s
    #492379
  • šŸ‡ŖšŸ‡øSpain penyaskito Seville šŸ’ƒ, Spain šŸ‡ŖšŸ‡ø, UTC+2 šŸ‡ŖšŸ‡ŗ

    This works and has good test coverage.

    • We need a follow-up for when the core issue lands.
    • We might want a follow-up to change the Code Components UI for having 2 fields and store option lists as "value => Human-friendly label" instead of having only the value as of now.
  • šŸ‡ŖšŸ‡øSpain penyaskito Seville šŸ’ƒ, Spain šŸ‡ŖšŸ‡ø, UTC+2 šŸ‡ŖšŸ‡ŗ

    Re-reading IS I think Wim expected to implement the Code Components here too? šŸ¤”šŸ¤”

  • Pipeline finished with Success
    7 days ago
    Total: 761s
    #492591
  • šŸ‡ŖšŸ‡øSpain penyaskito Seville šŸ’ƒ, Spain šŸ‡ŖšŸ‡ø, UTC+2 šŸ‡ŖšŸ‡ŗ

    It took me a while to figure out why we need any change at all for JS Component.
    So documenting it step by step.

    Right now, the site builder will generate their prop, and select "List: string".
    They see a single textfield, named "Value", with placeholder "Enter a text value", where they input their values one by one, and select the default one.

    That generates:

    js_component.enumcodecomponent.yml
    [...]
    props:
      color:
        title: Color
        type: string
        examples:
          - green
        enum:
          - blue
          - green
          - red
          - Orange
          - 'Another weird color'
    

    One we enable the component, that transforms to:

    experience_builder.component.js.enumcodecomponent.yml
    [...]
            allowed_values:
              -
                value: blue
                label: blue
              -
                value: green
                label: green
              -
                value: red
                label: red
              -
                value: Orange
                label: Orange
              -
                value: 'Another weird color'
                label: 'Another weird color'
    

    So if they input a friendly label, that's what they get. For translation, we need to translate that config entity.
    There's no way we can do this right without having some value - label pair on the client, or document how their values will be generated (potentially like prop names already are). In that case we might indicate in the UI that we expect labels, not values, and ideally print the value so they can copypaste that into the code editor.

    If we do that in the client, they will post the pairs (or the value can be easily calculated from the label, as prop names do). For simplicity, we use the same format than SDC (see props in js_component above).

    And when we have that, it's the client who would send the enum and the meta:enum (optionally we might want a x-translation-context, or generate one server side, but that's definitely for another follow-up), and we will have the same info than a "meta:enum" complete component (or an easy way to generate it server-side), which will generate the right config entity allowed_values pairs for e.g. translation.

  • šŸ‡ŖšŸ‡øSpain penyaskito Seville šŸ’ƒ, Spain šŸ‡ŖšŸ‡ø, UTC+2 šŸ‡ŖšŸ‡ŗ

    I think this is ready if we split the code components part, which I moved to šŸ“Œ JsComponents `enum` props should have human-readable labels: use `meta:enum` Active

  • šŸ‡ŖšŸ‡øSpain penyaskito Seville šŸ’ƒ, Spain šŸ‡ŖšŸ‡ø, UTC+2 šŸ‡ŖšŸ‡ŗ

    We'll work on JsComponents here.

  • Pipeline finished with Success
    3 days ago
    Total: 676s
    #495214
  • Pipeline finished with Failed
    2 days ago
    Total: 679s
    #495325
  • Pipeline finished with Success
    1 day ago
    Total: 742s
    #496358
  • šŸ‡ŖšŸ‡øSpain penyaskito Seville šŸ’ƒ, Spain šŸ‡ŖšŸ‡ø, UTC+2 šŸ‡ŖšŸ‡ŗ
Production build 0.71.5 2024