[MGNLUI-6740] Combobox with custom datasource will not show selected value Created: 09/Jun/21  Updated: 23/Jun/21  Resolved: 23/Jun/21

Status: Closed
Project: Magnolia UI
Component/s: forms
Affects Version/s: 6.2.7
Fix Version/s: None

Type: Bug Priority: Neutral
Reporter: Marty Glaubitz Assignee: Unassigned
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Ubuntu


Attachments: PNG File image-2021-06-09-17-46-38-314.png    
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:

 Description   

Steps to reproduce

  1.  Create a custom data source for the combobox options like so
@DatasourceType("anchorsDatasource")
public class AnchorLinkSelectFieldSupport extends OptionSelectFieldSupport {

    private final LinkService linkService;

    @Inject
    public AnchorLinkSelectFieldSupport(final FieldDefinition fieldDefinition, final OptionListDefinition optionListDefinition, final LinkService linkService) {
        super(fieldDefinition, optionListDefinition);
        this.linkService = linkService;
    }

    @Override
    public DataProvider<Option, ?> getDataProvider() {
        final var noneOption = new Option();
        noneOption.setValue("");
        noneOption.setLabel("None");

        // one may use a set of hard coded items to reproduce this issue
        final var options = Stream.concat(
            Stream.of(noneOption),
            linkService.getAnchorsDefinedInPage(null).stream()
                .map(anchor -> OptionUtil.of(anchor, anchor))
        );

        return DataProvider.fromStream(options);
    }
}
  1. Register it:
<components>
    <id>datasource-anchors</id>
    <component>
        <type>info.magnolia.ui.field.SelectFieldSupport</type>
        <implementation>my.project.dialog.field.AnchorLinkSelectFieldSupport</implementation>
    </component>
</components>
  1.  Then use it in a dialog:
anchorTarget:
  label: fields.anchorTarget
  $type: comboBoxField
  datasource:
    name: anchors

Expected results

When creating a component which uses this field, the dialog should show the selected value.

Actual results

No value is shown even though it is correctly stored:

 



 Comments   
Comment by Roman Kovařík [ 10/Jun/21 ]

Hi martyglaubitz,

 

OptionToModelTypeConverter searches configured options to select persisted value.

You would have to create the options/OptionListDefinition already in the constructor or implement a custom converter.

 

Hope that helps 

Roman

Comment by Marty Glaubitz [ 16/Jun/21 ]

@Roman archaic solution, but works. Maybe you guys can elaborate on that in the documentation of https://docs.magnolia-cms.com/product-docs/6.2/Templating/Dialog-definition/Field-definition/List-of-fields/Combobox-field.html 

Comment by Roman Kovařík [ 16/Jun/21 ]

Hi,

I'm glad that it works.

If you are looking for a cleaner solution:

  • you could use directly the objects from your services as options
    • just make sure those objects implement equals/hashcode or you implement DataProvider#getId
  • None option should be replaceable with emptySelectionAllowed

Roman
 

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