[MGNLUI-5855] CodeField does not respect defaultValue due to default transformer Created: 18/Sep/17  Updated: 22/Jul/20

Status: Open
Project: Magnolia UI
Component/s: None
Affects Version/s: 6.2
Fix Version/s: None

Type: Bug Priority: Neutral
Reporter: James Spence Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Template:
Acceptance criteria:
Empty
Task DoD:
[ ]* Doc/release notes changes? Comment present?
[ ]* Downstream builds green?
[ ]* Solution information and context easily available?
[ ]* Tests
[ ]* FixVersion filled and not yet released
[ ]  Architecture Decision Record (ADR)
Bug DoR:
[ ]* Steps to reproduce, expected, and actual results filled
[ ]* Affected version filled
Date of First Response:
Visible to:
Florian Fuchs

 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.


Generated at Mon Feb 12 09:30:43 CET 2024 using Jira 9.4.2#940002-sha1:46d1a51de284217efdcb32434eab47a99af2938b.