Uploaded image for project: 'Magnolia UI'
  1. Magnolia UI
  2. MGNLUI-5855

CodeField does not respect defaultValue due to default transformer

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • Neutral
    • None
    • 6.2
    • None
    • None

    Description

      When using a CodeField, setting a default in the yaml configuration for the field does not take effect. Take the following example:

          js:
            label: Javascript
            fields:
              js:
                class: info.magnolia.ui.form.field.definition.CodeFieldDefinition
                language: javascript
                label: Javascript
                defaultValue: "console.log('hello!');"
      

      With the above configuration, a default value will not be supplied to the field when creating a new version of the component.

      After some digging, I determined this is due to AbstractFieldFactory#setPropertyDataSourceAndDefaultValue. In this method, the current property is checked for its value being null - if its value is null, and it's a new item, it fills in the default value.

      However, due to the fact that the CodeField uses the NotNullInitialStringValueTransformer, the value is never null, therefore never triggering that default value being set.

      To resolve this issue locally, we created an extended version of the CodeField that changed that null check to an empty / null check, like so:

      if ((item instanceof ItemAdapter && ((ItemAdapter) item).isNew() && StringUtils.isEmpty(propertyValue)) || (!(item instanceof ItemAdapter) && StringUtils.isEmpty(propertyValue))) {
                  setPropertyDataSourceDefaultValue(property);
      }
      

      This will accept not only null values, but empty values as well, and will set the default value properly.

      Checklists

        Acceptance criteria

        Attachments

          Activity

            People

              Unassigned Unassigned
              jspence James Spence
              Florian Fuchs
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:

                Checklists

                  Bug DoR
                  Task DoD