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.
Views
Replies
Total Likes
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;
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.
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(); }
Thank you so much @Lokesh_Vajrala Both of your answers were very helpful. We are using Sling Models.
Views
Like
Replies
Views
Likes
Replies