0

I have an AEM site. My front-end content.xml has a select list of different color options to pick from:

<items jcr:primaryType="nt:unstructured">
    <colors
        jcr:primaryType="nt:unstructured"
        sling:resourceType="granite/ui/components/coral/foundation/form/select"
        fieldLabel="Select a Color"
        name="./colors">
            <items jcr:primaryType="nt:unstructured">
                <blue
                    jcr:primaryType="nt:unstructured"
                    text="Blue"
                    value="bl blue"/>
                <green
                    jcr:primaryType="nt:unstructured"
                    text="Green"
                    value="gr green"/>....

My model looks something like:

@Model(adaptables=Resource.class)
public class Color{

    @Inject @Named("colors") @Optional
    private String cssClass ;

    @Inject @Named("colors.text") @Optional //This is not working
    private String label;

    public String getCssClass() {
        return cssClass;
    }

    public String getLabel() {
        return label;
    }

    public void setCssClass(String cssClass) {
        this.cssClass = cssClass;
    }

    public void setLabel(String label) {
        this.label = label;
    }
}

This code will return the cssClass string as either "bl blue" or "gr green" depending on what the user selected.

My question is how do I get the label string to return "Blue" or "Green" (aka the text attribute of the selected color item)?

Thanks!

West
  • 1
  • It is not possible to get the label, as only the value of the dropdown gets saved in CRX when the author selects a color. The label is only used when rendering the dialog and not part of the actual values saved. You may need to find a way to either save the label as well when the option is selected, or create a Map of labels and use the selected color the key to find the corresponding label. – rakhi4110 Feb 01 '19 at 00:32
  • 1
    @rakhi4110 Thanks - you saved a ton of wasted time! – West Feb 01 '19 at 00:36

2 Answers2

0

Per @rakhi4110:

It is not possible to get the label, as only the value of the dropdown gets saved in CRX when the author selects a color.

West
  • 1
0

While what you are asking is not possible, because the value is the only thing that will be stored in JCR (as @rakhi4110 mentioned), you could still add the text value in the value attribute and parse it in the sling model.

Here is one way you can do it:

<items jcr:primaryType="nt:unstructured">
    <colors
        jcr:primaryType="nt:unstructured"
        sling:resourceType="granite/ui/components/coral/foundation/form/select"
        fieldLabel="Select a Color"
        name="./colors">
            <items jcr:primaryType="nt:unstructured">
                <blue
                    jcr:primaryType="nt:unstructured"
                    text="Blue"
                    value="Blue:bl blue"/>
                <green
                    jcr:primaryType="nt:unstructured"
                    text="Green"
                    value="Green:gr green"/>....

Notice value="Blue:bl blue" and value="Green:gr green"

Now in your sling model, you can do:

@Model(adaptables=Resource.class)
public class Color{

    @Inject @Named("colors") @Optional
    private String colorValue ;

    private cssClass;
    private label;

    @PostConstruct
    protected void init() {
        // This is a very rudimentary way to illustrate the point
        // you can do this in many other ways/data structures to get the same result
        String[] parts = colorValue.split(":");
        label = parts[0];
        cssClass = parts[1];
    }

    public String getCssClass() {
        return cssClass;
    }

    public String getLabel() {
        return label;
    }
}
Ahmed Musallam
  • 9,523
  • 4
  • 27
  • 47