I have a multifield which contains a textfield. I want to store a specific value of that textfield has an array of strings.
Below is the content structure
nameDetails (multifield)
---------item0
-----------Property name--> name and Property Value--> adobe (textfield1)
---------item1
-----------Property name--> name and Property Value--> league (textfield2)
---------item2
-----------Property name--> name and Property Value--> communities (textfield3)
I want to store the property values as a string array. Like ["adobe", "league", "communities"].
Please help me out here, how I can achieve this?
Solved! Go to Solution.
Views
Replies
Total Likes
Use this snippet to return the list as array.
private List<Items> multiitems = new ArrayList<Items>();
@PostConstruct
private void init(){
//Resource multifield = getResource().getChild("multifield");
Iterator<Resource> resource = multifield.listChildren();
String item1;
String item2;
String item3;
while(resource.hasNext()){
Resource childNode = resource.next();
item1 = childNode.getValueMap().get("item1", String.class);
item2 = childNode.getValueMap().get("item2", String.class);
item3 = childNode.getValueMap().get("item3", String.class);
Items item = new Items();
item.setItem1(item1);
item.setItem2(item2);
item.setItem3(item3);
multiitems.add(item);
}
}
Please try the sample code structure, it does store the value as an array. Screenshot attached, You can check the sample for vanity url in page properties: - /libs/wcm/foundation/components/basicpage/v1/basicpage/tabs/basic/items/column/items/vanityurl
Sample code extracted from here:
<?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"
jcr:title="Basic"
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">
<title/>
<moretitles/>
<onofftime/>
<vanityurl
cq:showOnCreate="{Boolean}false"
jcr:primaryType="nt:unstructured"
jcr:title="Vanity URL"
sling:resourceType="granite/ui/components/coral/foundation/form/fieldset">
<items jcr:primaryType="nt:unstructured">
<vanitypath
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/multifield"
cq-msm-lockable="sling:vanityPath"
fieldLabel="Vanity URL"
renderReadOnly="{Boolean}true">
<granite:data
jcr:primaryType="nt:unstructured"
cq-msm-lockable="sling:vanityPath"/>
<field
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/textfield"
name="./sling:vanityPath"
required="{Boolean}true"/>
</vanitypath>
<redirectVanityURL
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/checkbox"
name="./sling:redirect"
renderReadOnly="{Boolean}true"
text="Redirect Vanity URL"
value="true">
<granite:data
jcr:primaryType="nt:unstructured"
cq-msm-lockable="./sling:redirect"/>
</redirectVanityURL>
</items>
</vanityurl>
</items>
</column>
</items>
</jcr:root>
Hi @Anish-Sinha
I want to get string array in sling model and not store the values as an array in node level
ohh okay, try this sample class. This reads the multifield values and convert it to string:
package com.mysite.core.models; import java.util.List; import javax.annotation.PostConstruct; import javax.inject.Inject; import org.apache.sling.api.resource.Resource; import org.apache.sling.models.annotations.Model; @Model(adaptables = Resource.class) public class Test { @Inject private List<String> testValues; @PostConstruct protected void init() { String[] array = testValues.toArray(new String[0]); System.out.println(Arrays.toString(array)); } public List<String> getTestValues() { return testValues; } }
In sightly you can ereturn this as
<sly data-sly-use.test="com.mysite.core.models.Test"> <sly data-sly-list.testClass= "${test.testValues}"> ${testClass} </sly> </sly>
Use this snippet to return the list as array.
private List<Items> multiitems = new ArrayList<Items>();
@PostConstruct
private void init(){
//Resource multifield = getResource().getChild("multifield");
Iterator<Resource> resource = multifield.listChildren();
String item1;
String item2;
String item3;
while(resource.hasNext()){
Resource childNode = resource.next();
item1 = childNode.getValueMap().get("item1", String.class);
item2 = childNode.getValueMap().get("item2", String.class);
item3 = childNode.getValueMap().get("item3", String.class);
Items item = new Items();
item.setItem1(item1);
item.setItem2(item2);
item.setItem3(item3);
multiitems.add(item);
}
}
This helped. Thank you @PhaniRajuTanneru
You can use the below sample code to adapt to multifield in any type and get a list, In your case type is String and then convert the list to Array or Json object.
https://github.com/arunpatidar02/aem63app-repo/blob/master/demo/test.txt