Expand my Community achievements bar.

String[] type for Multivalued Properties - AEM

Avatar

Level 3

[Thread Edited By Adobe]

/*Don’t forget to meet and greet your fellow peers virtually by telling them about yourself here

Go ahead and to it now: https://adobe.ly/3eDnB4v */

 

Actual Post:

Hi,

I have a question regarding multivalued property of type granite/ui/components/foundation/form/multifield in AEM.
My understanding is that it will always be stored in AEM with String[] Multi Array type for a given node's property, no matter if user provides 1 or greater than 1 property values.

But I have observed that some values are getting stored with Single String data type if I provide one value And stored as String[] multi value type in crxde, if I enter more than 1 values.

Why AEM does not store all values in unique way? i.e. By setting data type as; String[] multi value, if it's sling:resourceType is defined as multifield.

Is it a defect?

Thanks.

desktop_exl_promo_600x100_weempoweryou.png

24 Replies

Avatar

Level 2

Hi, I am looking for a similar logic for Keywords - dc:subject[]. How did you handle the single and multivalue string ?

Avatar

Level 4

Hi Meenakshi,

We have also face the same problem but we have one option here to ask AEM to save data in single array for all the fields present in multi field on the component or not. We can use the key property for cq:widget. If we did not set the key property we will not able to see the single array contains all the widgets inside multifieldpanel.

We have use this in AEM 6.1

Please find the below screenshot

answer.PNG

Multifield is using the key to save the data in single array.

Thanks

Avatar

Level 2

From DAYCARE:

I'd like to provide you a quick summary of the resolution of this issue :

Problem : Multifield with one entry saves data in non array attribute

Root cause : Default behavior, but can be overridden using a typeHint

Solution : Used typeHint on multifield node with proper field name & type parameter syntax (fieldName@String[])

References:

Apache Sling :: Manipulating Content - The SlingPostServlet (servlets.post)

Extending Page Properties in AEM Sites

Sample Code from DayCare with relevant code in bold.

The key was understanding that the multifield needed the typeHint and it does not use the dot (.) symbol like the nested field does.

This quote is taken from the first article referenced above:

"If the @TypeHint value ends with [], it indicates a multi-value property. A multi-value property is usually auto-detected if there are multiple values for the property (i.e. request parameter). But if only a single value is present in the request, the desired property type needs to be explicitly defined as multi-value by stating @TypeHint=<type>[]."

So most examples showed the typeHint being used within an attribute such as name="width@TypeHint

but in this case, since we want to force it to an array even when there is only one element, the attribute typeHint needed to be used.

<?xml version="1.0" encoding="UTF-8"?>

<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:granite="http://www.adobe.com/jcr/granite/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0"

    jcr:primaryType="nt:unstructured">

    <content jcr:primaryType="nt:unstructured">

        <items jcr:primaryType="nt:unstructured">

            <tabs jcr:primaryType="nt:unstructured">

                <items jcr:primaryType="nt:unstructured">

                    <cug

                        jcr:primaryType="nt:unstructured"

                        jcr:title="CUG"

                        sling:orderBefore="advanced"

                        sling:resourceType="granite/ui/components/coral/foundation/fixedcolumns">

                        <items jcr:primaryType="nt:unstructured">

                            <column

                                jcr:primaryType="nt:unstructured"

                                sling:resourceType="granite/ui/components/coral/foundation/container">

                                <items jcr:primaryType="nt:unstructured">

                                    <cugPrincipals

                                        granite:class="foundation-layout-util-maximized-alt"

                                        jcr:primaryType="nt:unstructured"

                                        sling:resourceType="granite/ui/components/coral/foundation/form/multifield"

                                        fieldDescription="Click 'Add Field' to add a CUG Permission."

                                        fieldLabel="Access is restricted to the following groups:"

                                        required="{Boolean}true"

                                        typeHint="cq:cugPrincipals@String[]">

                                        <field

                                            jcr:primaryType="nt:unstructured"

                                            sling:resourceType="granite/ui/components/coral/foundation/form/textfield"

                                            name="./cq:cugPrincipals"/>

                                    </cugPrincipals>

                                </items>

                            </column>

                        </items>

                    </cug>

                </items>

            </tabs>

        </items>

    </content>

</jcr:root>