Provide list_string field values and defaults programmatically

Created on 4 February 2025, 21 days ago

Problem/Motivation

Currently, we're providing all the field values of the list_string fields in drowl_media via config. This is problematic, because they are hard to update and could theoretically vary per project.
Furthermore, the fields are locked, so we can't set a default per project.

So for all the list_string fields in drowl_media we should use "allowed_values_function" instead of "allowed values" array:

settings:
  allowed_values:
    -
      value: light
      label: 'Box hell, Text dunkel'
    -
      value: dark
      label: 'Box dunkel, Text hell'
    -
      value: light-glass
      label: 'Box transparent-hell, Text dunkel'
    -
      value: dark-glass
      label: 'Box transparent-dunkel, Text hell'
    -
      value: transparent-light
      label: 'Box transparent (100%), Text dunkel'
    -
      value: transparent-dark
      label: 'Box transparent (100%), Text hell'
    -
      value: primary
      label: 'Highlight / Primärfarbe'
    -
      value: secondary
      label: Sekundärfarbe
  allowed_values_function: ''

(field.storage.*)

Same for "default_value" and "default_value_fallback":

default_value:
  -
    value: disabled
default_value_callback: ''

(field.field.*)

https://chromatichq.com/insights/dynamic-default-and-allowed-values-list...

Steps to reproduce

Proposed resolution

Remaining tasks

  1. Add a class with static methods to return the drowl media entity form values arrays and default value. Note that some of them are reused for multiple fields, so the names should be general!
  2. Write an update hook to update the existing configurations, setting allowed_values_function and default_value_callback and resetting the old values to their empty default (take care with the notation!)
  3. Add a DROWL Media Settings page where the defaults can be selected for each field. The field names are hard-coded.

Start with a simple try first to ensure it works as expected.

These are the relevant fields:

  • field.field.media.slide.field_image_animation.yml:field_type: list_string
  • field.field.media.slide.field_overlay_button_color.yml:field_type: list_string
  • field.field.media.slide.field_overlay_button_style.yml:field_type: list_string
  • field.field.media.slide.field_overlay_display.yml:field_type: list_string
  • field.field.media.slide.field_overlay_position_lg.yml:field_type: list_string
  • field.field.media.slide.field_overlay_position_md.yml:field_type: list_string
  • field.field.media.slide.field_overlay_position.yml:field_type: list_string
  • field.field.media.slide.field_overlay_sizing_lg.yml:field_type: list_string
  • field.field.media.slide.field_overlay_sizing_md.yml:field_type: list_string
  • field.field.media.slide.field_overlay_sizing.yml:field_type: list_string
  • field.field.media.slideshow.field_slide_arrows.yml:field_type: list_string
  • field.field.media.slideshow.field_slide_autoplay.yml:field_type: list_string
  • field.field.media.slideshow.field_slide_dots.yml:field_type: list_string
  • field.field.media.slideshow.field_slide_height.yml:field_type: list_string
  • field.field.media.slideshow.field_slide_infinite.yml:field_type: list_string
  • field.storage.media.field_image_animation.yml:type: list_string
  • field.storage.media.field_overlay_button_color.yml:type: list_string
  • field.storage.media.field_overlay_button_style.yml:type: list_string
  • field.storage.media.field_overlay_display.yml:type: list_string
  • field.storage.media.field_overlay_position_lg.yml:type: list_string
  • field.storage.media.field_overlay_position_md.yml:type: list_string
  • field.storage.media.field_overlay_position.yml:type: list_string
  • field.storage.media.field_overlay_sizing_lg.yml:type: list_string
  • field.storage.media.field_overlay_sizing_md.yml:type: list_string
  • field.storage.media.field_overlay_sizing.yml:type: list_string
  • field.storage.media.field_slide_arrows.yml:type: list_string
  • field.storage.media.field_slide_autoplay.yml:type: list_string
  • field.storage.media.field_slide_dots.yml:type: list_string
  • field.storage.media.field_slide_height.yml:type: list_string
  • field.storage.media.field_slide_infinite.yml:type: list_string

User interface changes

A new settings page where the defaults can be selected (and thereby also show the available values)

API changes

None

Data model changes

None

Feature request
Status

Active

Version

4.0

Component

Code

Created by

🇩🇪Germany Anybody Porta Westfalica

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

Merge Requests

Comments & Activities

  • Issue created by @Anybody
  • 🇩🇪Germany Anybody Porta Westfalica
  • 🇩🇪Germany lrwebks Porta Westfalica
  • 🇩🇪Germany Anybody Porta Westfalica

    @lrwebks one more thing: On the settings page the settings should be grouped in open details as follows

    DROWL Media Settings [=Page title]
    - DROWL Media defaults
    -- Slide
    -- Slideshow
    -- [No other entity types needed yet, so for now only these two]

    So we have room for future settings!

  • 🇩🇪Germany Anybody Porta Westfalica

    GREAT work @lrwebks I'm impressed!!! 🚀🎉

    Left some comments!
    Maybe @thomas.frobieter can take a short look at the namings used, if it's all fine?

  • 🇩🇪Germany lrwebks Porta Westfalica

    @thomas.frobieter: Is this “Default” option redundant now? If it was just there to programmatically use a “default” styling that is consistent across the whole page, then it is no longer needed, as the new settings form does exactly that (but better :P). Or is this a regular setting just as the “Yes” and “No” options and has to stay?

  • 🇩🇪Germany lrwebks Porta Westfalica
  • 🇩🇪Germany Anybody Porta Westfalica

    @lrwebks I agree it would make sense to remove that unclear "Default" and instead choose one of them as install / update hook default!
    Let's see what @thomas.frobieter thinks.

    @thomas.frobieter please define the default then.

  • I think the "default" value will use the value set in the Slick optionset. If this is the case - and I have no idea what else is supposed to happen - we should leave it as it is.

  • 🇩🇪Germany Anybody Porta Westfalica

    Could someone please verify this in code?

    And if it's the case, we should either add a description to explain that or improve the value label. Currently it's not clear enough / confusing!
    @lrwebks any helping description in the media entity form already?

  • Maybe @thomas.frobieter can take a short look at the namings used, if it's all fine?

    The names look good (:

  • 🇩🇪Germany Anybody Porta Westfalica

    @thomas.frobieter once #13 is clarified, I think you can merge and test this! 🎉

  • 🇩🇪Germany lrwebks Porta Westfalica

    The description of the fields in question indeed provides a link "Change default value" which leads to a slick optionset edit form. So we could just rename it to something more obvious like "Inherit from Optionset", so that everyone is in the know.

  • 🇩🇪Germany Anybody Porta Westfalica

    Okay, please

    1. Use "Slick Optionset default" as value instead of "Default"
    2. Add a useful description. sth. like: "Slick default uses the Slick defaults configured in the Slick Optionset. These defaults *can be changed here*" (link)

    Feel free to further improve!

  • 🇩🇪Germany Anybody Porta Westfalica

    Merging, go for final testing @thomas.frobieter - if all is fine, please set this fixed and tag a new release.

  • Pipeline finished with Skipped
    18 days ago
    #417660
    • anybody committed 5d97c337 on 4.x
      Issue #3504256 by lrwebks, anybody, thomas.frobieter: Provide...
    1. field_image_animation needs a "none" value (which should be the default in the global settings)
    2. The overlay_display fields missing the "no overlay" value

    That's it!

    @anybody we had considered adding ‘Standard’ to the fields in the media entity, have you discarded this? As I said, it's just a bonus for me. But if it's easy, I'd be happy to do it.

  • 🇩🇪Germany Anybody Porta Westfalica

    @thomas.frobieter who should implement #20? I'll assign @lrwebks for now, feel free to pick it up yourself.

  • 🇩🇪Germany lrwebks Porta Westfalica

    I have added the none value to the image_animation field now in a new branch, but as far as I'm aware, there seems to be no option named “no overlay” for the overlay_display field, even without our new changes. @thomas.frobieter, could you clarify?

  • 🇩🇪Germany lrwebks Porta Westfalica

    Found it: @thomas.frobieter, I think you meant the overlay_position fields? They do have a “no overlay” option, which is already present in the default settings form.

  • 🇩🇪Germany Anybody Porta Westfalica
  • @thomas.frobieter, I think you meant the overlay_position fields? They do have a “no overlay” option, which is already present in the default settings form.

    Right, this is correct now. One last thing: the animation select now has two "None" options:

    <select data-drupal-selector="edit-field-image-animation" aria-describedby="edit-field-image-animation--description" id="edit-field-image-animation" name="field_image_animation" class="form-select form-element form-element--type-select" data-once="field-group-tab-validation field-group-tabs-validation">
                <option value="_none">- Nicht festgelegt/ausgewählt -</option>
                    <option value="none">Keine</option>
                    <option value="ken-burns">Ken-Burns-Effekt</option>
          </select>

    Lets stick with Drupals default "_none" option here. In the version I tested, I only had:

    <select data-drupal-selector="edit-field-image-animation" aria-describedby="edit-field-image-animation--description" id="edit-field-image-animation" name="field_image_animation" class="form-select form-element form-element--type-select" data-once="field-group-tab-validation field-group-tabs-validation">
                    <option value="ken-burns">Ken-Burns-Effekt</option>
          </select>
  • 🇩🇪Germany lrwebks Porta Westfalica
  • 🇩🇪Germany lrwebks Porta Westfalica

    Okay, we're using the built-in default empty option now, just like the actual field in the slide config form does.

    Heads-up why I am using this line here:

    '#empty_value' => '_none',
    

    If I do not provide a specific value for the empty option, it will disappear forever once the form is saved with a different value (e.g., “Ken Burns Effect”). But since we need it to stay, so I just passed its own default value to it.

Production build 0.71.5 2024