Iterates trough child resources looking for the all resources from two specific resourceType | Community
Skip to main content
Level 6
October 20, 2022

Iterates trough child resources looking for the all resources from two specific resourceType

  • October 20, 2022
  • 1 reply
  • 1134 views

Hi,

I need to find two different resource types in the content and then render these in the correct order as they are stored in the content.

 

private List<???how to define it???> getRelevantResources(Resource r) {
return ResourceModelUtil.getAllResourcesFromResourceType(r, ???here I would have two resource types???)
.stream()
.map(resource-> event.adaptTo(??? not sure how to adapt here to two resource types???))
.filter(Objects::nonNull)
.collect(Collectors.toList());

 

I wrote a conceptual code but I am missing some building blocks that I marked with questionmarks. The idea is that I get a list that I can then render in a template.

 

Thanks in advance. 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

1 reply

Lokesh_Vajrala
Community Advisor
Community Advisor
October 20, 2022

Is this what you looking for? 

 

Return as generic resources 

List<Resource> resourceList = new ArrayList<>();
for (Resource resource : resource.getChildren()) {
    if (resource.isResourceType("resourceType1") || resource.isResourceType("resourceType2")) {
        resourceList.add(resource);
    }
}
return resourceList;

In case, if you want them as the actual component Sling Models  (assuming your sling models extend a generic Component class or use java.util.Object instead)

    List<Component> resourceList = new ArrayList<>();
    for (Resource resource : viewConfigsParent.getChildren()) {
        if (resource.isResourceType("resourceType1")){
           resource.adaptTo(Model1.class);
        }
        if (resource.isResourceType("resourceType2")){
            resource.adaptTo(Model2.class);
       }
    }
    return componentList;

 

anasusticAuthor
Level 6
October 20, 2022

Hi @lokesh_vajrala 

Yes, thank you very much. The first solution is what I was looking for.

I have an additional question if you do not mind.

 

How do I render a generic resource in the sightly template?

<ul data-sly-list.myList="${model.resourceList}">
<sly data-sly-resource="${???here I need to point to somehow differentiate between the two possible resources???.resource @ resourceType='xxx/components/content/???specific resource???'}"></sly>
</ul>

 

I wrote above a conceptual code and left the questionmarks where I am not sure. 

Lokesh_Vajrala
Community Advisor
Community Advisor
October 20, 2022

Well, that's a good question @anasustic

Are you using Sling Models with ModelExporters? If yes, your models may also be implementing ComponentExporter.getExporterType(), in this case, you can call resource.exporterType() which returns the component resourceType.

If you're not using ModelExporters, then with Sling Models, you can add a method getType() to the Model to return the component resourceType to use in HTL/Sightly.

 

public String getType() {
        return resource.getResourceType();
}