How to read child nodes of multifield control or any node using Sling models and WCMUsePojo class, this is just code snippet’s not going to talk anything theoretically if you want to have conceptual knowledge I suggest you read either docs.day.com or my other article’s, for sling models refer my other aticle custom list collection component using sling models

Node Structure

The below image talks about how the content is stored in CRX
node-structue

SlingModel 

package com.aem.toolkit.core.models;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.Optional;
@Model(adaptables = Resource.class)
public class ModelLink {
	@Inject
	@Named("linkText")
	@Optional
	private String linkText;
	@Inject
	@Named("linkUrl")
	@Optional
	private String linkUrl;
	@PostConstruct
	protected void init() {
	}
	public String getTitle() {
		return linkText;
	}
	public String getUrl() {
		return linkUrl;
	}
	public void setTitle(String title) {
		linkText = title;
	}
	public void setUrl(String url) {
		linkUrl = url;
	}
}

Iterate child node with sling models

package com.aem.toolkit.core.models;
import java.util.ArrayList;
import java.util.Iterator;
import org.apache.sling.api.resource.Resource;
import com.adobe.cq.sightly.WCMUsePojo;
public class ListCollection extends WCMUsePojo {
	public static java.util.List<ModelLink> links;
	public java.util.List<ModelLink> getLinks() {
		Resource childResource = getResource().getChild("items");
		if (childResource != null) {
			links = populateModel(childResource);
		}
		return links;
	}
	public static java.util.List<ModelLink> populateModel(Resource resource) {
		links = new ArrayList<ModelLink>();
		if (resource != null) {
			Iterator<Resource> linkResources = resource.listChildren();
			while (linkResources.hasNext()) {
				Resource childResource = linkResources.next();
				ModelLink link = childResource.adaptTo(ModelLink.class);
				if (link != null)
					links.add(link);
			}
		}
		return links;
	}
	@Override
	public void activate() throws Exception {
	}
}

Iterate child node using Java 8 Lambda expression without sling model’s

package com.aem.toolkit.core.models;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Optional;
import org.apache.sling.api.resource.Resource;
import com.adobe.cq.sightly.WCMUsePojo;
public class ListCollectionGenerics extends WCMUsePojo {
	public static java.util.List<Link> links;
	public java.util.List<Link> getLinks() {
		Resource childResource = getResource().getChild("items");
		if (childResource != null) {
			Iterator<Resource> linkResources = childResource.listChildren();
			linkResources.forEachRemaining(res -> populateModel(res).ifPresent(links::add));
		}
		return links;
	}
	public static Optional<Link> populateModel(Resource resource) {
		links = new ArrayList<Link>();
		Link link = new Link();
		link.setTitle((String) resource.getValueMap().get("linkText"));
		link.setUrl((String) resource.getValueMap().get("linkUrl"));
		return Optional.of(link);
	}
	@Override
	public void activate() throws Exception {
	}
}

Finally, call the ListCollection handler in the sightly template 

<sly data-sly-use.listUse="com.aem.toolkit.core.models.ListCollection">
  <ul data-sly-list="${listUse.links}">
    <li><a href="${item.url}">${item.title}</a></li>
  </ul>
</sly>

 
 

By coderss

One thought on “AEM Code Snippets : How to read child nodes in aem ?”

Leave a Reply

Your email address will not be published. Required fields are marked *