Hi,
I need to find all components of a certain type on the current page in AEM?
I tried to do it using
ComponentManager componentManager = request.adaptTo(ComponentManager.class);
Collection<Component> components = componentManager.getComponents();
but my componentManager is null?
Solved! Go to Solution.
Views
Replies
Total Likes
check this out - groovy script -
import com.day.cq.wcm.api.Page
import com.day.cq.wcm.api.PageManager
def pagePath = "/content/mysite/mypage"
def pageManager = sling.getService(PageManager.class)
def page = pageManager.getPage(pagePath)
def componentType = "mysite/components/mycomponent"
def components = page.contentResource?.children?.iterator()
// Iterate over each component and check if it's of the specified type
while (components?.hasNext()) {
def component = components.next()
if (component.resourceType == componentType) {
println "Found component of type ${componentType} at path ${component.path}"
}
}
check this out - groovy script -
import com.day.cq.wcm.api.Page
import com.day.cq.wcm.api.PageManager
def pagePath = "/content/mysite/mypage"
def pageManager = sling.getService(PageManager.class)
def page = pageManager.getPage(pagePath)
def componentType = "mysite/components/mycomponent"
def components = page.contentResource?.children?.iterator()
// Iterate over each component and check if it's of the specified type
while (components?.hasNext()) {
def component = components.next()
if (component.resourceType == componentType) {
println "Found component of type ${componentType} at path ${component.path}"
}
}
Hi @anasustic ,
Try to get it through Resource Resolver
ResourceResolver resourceResolver = request.getResourceResolver(); ComponentManager componentManager = resourceResolver.adaptTo(ComponentManager.class);
You can not directly adapt it from request. you should first get the resolver then from resolver you can adapt it to like in below code:
ResourceResolver resourceResolver = request.getResourceResolver(); ComponentManager componentManager = resourceResolver.adaptTo(ComponentManager.class);
but components returned by this manage might not be visible to the underlying resource resolver. Consumers of this manager should call Component.isAccessible()
prior of using it.
like:
com.day.cq.wcm.api.components.Component delegate = componentManager.getComponent("Name_or_property_of_the_component"); if (delegate != null && delegate.isAccessible()) { //your code }
Hope this helps
Umesh Thakur
@anasustic did you found the solution for what you are looking? Even I need to fetch all the component from current page. Appreciate if you share the solution here.
This is how I solved it and it works:
Optional.ofNullable(request.getResourceResolver().getResource(currentPage.getPath()))
.map(r -> ResourceModelUtil.getAllResourcesFromResourceType(r, Constants.ResourceTypes.YOUR_RESOURCE_TYPE))
.stream()
.flatMap(Collection::stream)
.continue_chaining_or_collect
@anasustic is there any custom method in your ResourceModelUtil ?
Yes there are several proprietery methods I used. The methods essentially return a list of resources that match a particular resource type. If the given resource is of the same type, it returns that resource, otherwise, it navigates through the descendants of the given resource to find all resources that match the given resource type.