how to store a specific property values of multifield items as string array | Community
Skip to main content
Level 3
Apr 7, 2022 12:51 AM UTC
Solved

how to store a specific property values of multifield items as string array

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?

 

@kautuk_sahni @Arun_Patidar 

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
PhaniRajuTanneru
PhaniRajuTanneruAccepted solution
Level 2
Apr 7, 2022 5:46 AM UTC

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);

}

}

7 replies

Anish-Sinha
Employee
Apr 7, 2022 1:13 AM UTC

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>

 

 

 

Translate
DollyAuthor
Level 3
Apr 7, 2022 1:49 AM UTC

Hi @anish-sinha 

I want to get string array in sling model and not store the values as an array in node level

Translate
Anish-Sinha
Employee
Apr 7, 2022 4:33 AM UTC

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>
Translate