Hi all,
I want to write a sling models for multifield for touch ui component. If any one having idea to write the sling model for multifield with minimal code please suggest some examples.
Thanks
Solved! Go to Solution.
Views
Replies
Total Likes
public List<String> getCqTags() {
List<String> parentTags = new ArrayList<>();
Page currentPage = pageManager.getContainingPage(getResource());
ValueMap props = currentPage.getProperties();
String[] cqTags = props.get("cq:tags", String[].class);
for (String tag : cqTags) {
//check if the tag has child tags or not
if (!hasChildTags(tag)) {
//if the tag is leaf node, add its parent tag
parentTags.add(getParentTag(tag));
}
}
logger.info("Parent cq:tags property of leaf tags: {}", parentTags);
return parentTags;
}
Hi ,
Please check this below it might be helpful.
https://blogs.perficient.com/2021/04/02/writing-less-java-code-in-aem-with-sling-models/
https://bimmisoi.blogspot.com/2020/07/aem-fetching-multifield-values-using.html
Thanks
The best way is to go with a sling model.
more on this: https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/multifield-using-sling-mod...
Thank you,
Sravan
Hi @Nandheswara
Please find my blog here which explains all the steps to create a MF: https://allaembykiran.wordpress.com/2021/01/13/nested-multi-field/
Hope this helps!
Thanks,
Kiran Vedantam.
public List<String> getCqTags() {
List<String> level2Tags = new ArrayList<>();
Page currentPage = pageManager.getContainingPage(getResource());
ValueMap props = currentPage.getProperties();
String[] cqTags = props.get("cq:tags", String[].class);
for (String tag : cqTags) {
if(tag.split("/").length == 2) {
level2Tags.add(tag);
}
}
logger.info("Level 2 cq:tags property: {}", level2Tags);
return level2Tags;
}
public List<String> getCqTags() {
List<String> parentTags = new ArrayList<>();
Page currentPage = pageManager.getContainingPage(getResource());
ValueMap props = currentPage.getProperties();
String[] cqTags = props.get("cq:tags", String[].class);
for (String tag : cqTags) {
//check if the tag has child tags or not
if (!hasChildTags(tag)) {
//if the tag is leaf node, add its parent tag
parentTags.add(getParentTag(tag));
}
}
logger.info("Parent cq:tags property of leaf tags: {}", parentTags);
return parentTags;
}
Hi @Nandheswara , It all depend on complexity on your use case/Multifield. I am sharing a pseudo code having 3 fields in multifield and in sling model it handling using MAP. Update field names as per need. I will also add links to complete files
Dialog(only multifield pseudo code. Will leave link to complete dialog file)
<bookdetailswithmap
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/multifield"
composite="{Boolean}true"
fieldDescription="Books Details"
fieldLabel="Book Details"
required="{Boolean}false">
<field
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/container"
emptyText="Books Details"
name="./bookdetailswithmap">
<items jcr:primaryType="nt:unstructured">
<book
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/textfield"
emptyText="Book Name"
fieldLabel="Book Name"
name="./bookname"/>
<subject
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/textfield"
emptyText="Book Subject"
fieldLabel="Book Subject"
name="./booksubject"/>
<published
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/textfield"
emptyText="Publish Year"
fieldLabel="Publish Year"
name="./publishyear"/>
</items>
</field>
</bookdetailswithmap>
Sling Model(Only method which return multifield values. get resource using annotation)
public List<Map<String, String>> getBookDetailsWithMap() {
List<Map<String, String>> bookDetailsMap=new ArrayList<>();
try {
Resource bookDetail=componentResource.getChild("bookdetailswithmap");
if(bookDetail!=null){
for (Resource book : bookDetail.getChildren()) {
Map<String,String> bookMap=new HashMap<>();
bookMap.put("bookname",book.getValueMap().get("bookname",String.class));
bookMap.put("booksubject",book.getValueMap().get("booksubject",String.class));
bookMap.put("publishyear",book.getValueMap().get("publishyear",String.class));
bookDetailsMap.add(bookMap);
}
}
}catch (Exception e){
LOG.info("\n ERROR while getting Book Details {} ",e.getMessage());
}
LOG.info("\n SIZE {} ",bookDetailsMap.size());
return bookDetailsMap;
}
Sightly (pseudo code) to print multifield values. Update as per your need.
<div data-sly-list.item="${books.bookDetailsWithMap}">
---------------Book : ${itemList.count}--------------
<p> Book : <b>${item.bookname} </b></p>
<p> Subject : <b>${item.booksubject}</b> </p>
<p> Publish : <b>${item.publishyear}</b> </p>
</div>
Sling Model - https://github.com/aemgeeks1212/aemgeeks/blob/master/core/src/main/java/com/aem/geeks/core/models/im...
Component - https://github.com/aemgeeks1212/aemgeeks/tree/master/ui.apps/src/main/content/jcr_root/apps/aemgeeks...
If you are interested. Links to video tutorials.
Multifield - https://youtu.be/TJaAdbkyPJQ
Nested Multifield - https://youtu.be/Lm6o5JDNiqs
Validation for Multifield - https://youtu.be/tsd8s8UMBRY
In Sling Model annotate your multifiled with @ChildResource with reference List<T>
Write a Sling Model for Multi Field item. Ex: MultiFieldItem is model for multifield item which will have all properties of each item.
@ChildResource
private List<MultiFieldItem> myMultiField;
Now you can iterate the myMultiField list it HTL or in Sling Model to get data.