how to store a specific property values of multifield items as string array | Community
Skip to main content
Level 3
April 7, 2022
Solved

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

  • April 7, 2022
  • 2 replies
  • 3861 views

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.
Best answer by PhaniRajuTanneru

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

}

}

2 replies

Anish-Sinha
Adobe Employee
Adobe Employee
April 7, 2022

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>

 

 

 

DollyAuthor
Level 3
April 7, 2022

Hi @anish-sinha 

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

Anish-Sinha
Adobe Employee
Adobe Employee
April 7, 2022

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>