Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

Adobe Summit 2023 [19th to 23rd March, Las Vegas and Virtual] | Complete AEM Session & Lab list
SOLVED

How do we write nested multifield using sling mapper?

Avatar

Level 3

Is there a way to include nested multifield using sling mapper in a single file?

    @Inject
protected Resource resource;

List<Map<String, String>> marketData = new ArrayList<>();

List<Map<String, String>> marketContactData = new ArrayList<>();


@ChildResource(name="listItems", injectionStrategy = InjectionStrategy.OPTIONAL)
Resource listItemsResource;



@PostConstruct
protected void init() {
if(listItemsResource.hasChildren()){

Iterable<Resource> listMarket = listItemsResource.getChildren();

Iterator<Resource> listItemsIterator = listMarket.iterator();

while(listItemsIterator.hasNext()){
Resource listItemResource = listItemsIterator.next();
ValueMap listItemResourceValueMap = listItemResource.adaptTo(ValueMap.class);
Map<String, String> marketItem = new HashMap<>();
marketItem.put("marketName", listItemResource.getValueMap().get("marketName", String.class));
marketItem.put("countryFlag", listItemResource.getValueMap().get("countryFlag", String.class));



Resource childItemsResource = listItemResource.getChild("listContacts");
if(null != childItemsResource){
Iterable<Resource> iterable = childItemsResource.getChildren();

Iterator<Resource> iterator = iterable.iterator();

while(iterator.hasNext()){
Resource res = iterator.next();
Map<String, String> marketContact = new HashMap<>();

ValueMap childResourceValueMap = res.adaptTo(ValueMap.class);
marketContact.put("name", res.getValueMap().get("name", String.class));
marketContact.put("email", res.getValueMap().get("email", String.class));
marketContactData.add(marketContact);


}

}
marketData.add(marketItem);
}
}
}

public List<Map<String, String>> getMarketData() {
return marketData;
}

public void setMarketData(List<Map<String, String>> marketData) {
this.marketData = marketData;
}
public List<Map<String, String>> getMarketContactData() {
return marketContactData;
}

public void setMarketContactData(List<Map<String, String>> marketContactData) {
this.marketContactData = marketContactData;
}
}

and here's the sightly

<sly data-sly-use.model="com.......models.Model">
<p>Contacts</p>

<!--/* Iterate over contacts */-->
<ul data-sly-list.market="${model.marketData}">
<li>Market Name: ${market.marketName}</li>
<li>MarketFlag: ${market.countryFlag}</li>

<ul data-sly-list.contact="${market.marketContactData}">
<li>Contact Name: ${contact.name}</li>
<li>Email: ${contact.email}</li>

 

</ul>

It doesn't print the child nodes

1 Accepted Solution

Avatar

Correct answer by
Community Advisor
0 Replies

Avatar

Community Advisor

You can try below in nested way.

https://github.com/arunpatidar02/aem63app-repo/blob/master/demo/test.txt

 

Create model class for Parent, Child and GrandChild and adapt to this models and create a list(child) of lists(grand children).

Avatar

Correct answer by
Community Advisor