Expand my Community achievements bar.

July 31st AEM Gems Webinar: Elevate your AEM development to master the integration of private GitHub repositories within AEM Cloud Manager.
SOLVED

SOLVED: Find all the components on the current page in AEM?

Avatar

Level 7

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? 

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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}"
    }
}

View solution in original post

7 Replies

Avatar

Correct answer by
Community Advisor

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}"
    }
}

Avatar

Community Advisor

Hi @anasustic ,

 

Try to get it through Resource Resolver

ResourceResolver resourceResolver = request.getResourceResolver();
ComponentManager componentManager = resourceResolver.adaptTo(ComponentManager.class);

Avatar

Community Advisor

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

Avatar

Level 2

@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.

Avatar

Level 7

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

 

Avatar

Level 2

@anasustic is there any custom method in your ResourceModelUtil ?

 

Avatar

Level 7

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.