Expand my Community achievements bar.

SOLVED

Fetch site page path in AEM component

Avatar

Level 2

I'm trying to fetch the site page path that my AEM component resides in. However when I try it with this following method, I get the top-level path returned (i.e. /content/my-site/us/en) instead of the actual page path. Any suggestions on how to adjust this method? Thanks.

            PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
            Page currentPage = pageManager.getContainingPage(request.getResource());
            String currentPagePath = currentPage.getPath();

 

 

1 Accepted Solution

Avatar

Correct answer by
Level 2

To get this to work, I ended up using a utility method that retrieves the current page from a SlingHttpServletRequest. It does this by checking several possible sources for the page information:


1. It first checks if the suffix resource of the request is not null and if its path starts with CONTENT_ROOT. If so, it uses a getContainingPage method to get the page containing the suffix resource.
2. If the first check doesn't yield a page, it then checks if there is a parameter named ITEM_PARAMETER in the request. If there is, it uses the PageManager.getContainingPage method to get the page containing the resource specified by the ITEM_PARAMETER.
3. If the second check doesn't yield a page, it then checks the REFERER header of the request. If the header is not blank, it extracts the page path from the referer URL and uses the PageManager.getPage method to get the page at that path. 

View solution in original post

5 Replies

Avatar

Community Advisor

@stiegjo You can inject the current page object in your sling model. 

 

Something like this should do : 

 

@ScriptVariable
protected Page currentPage;

 

public string getCurrenPath() {

String currentPagePath = currentPage.getPath();

return currentPagePath;

}

Avatar

Level 2

I'm seeing the same results:

 

 /content/my-site/us/en

Avatar

Community Advisor

@stiegjo , then please check the request object and the resource object that you are deriving out of the request.

Can you please share the sling model code ?

 I am suspecting your problem is similar to this one - https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/aem-6-3-get-the-current-pa...

 

 

Avatar

Level 2

I did make my class adaptable from SlingHttpServletRequest:

 

 

@Model(adaptables = {Resource.class, SlingHttpServletRequest.class}, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class SiteBannerModel

 

Also here is the request object:

 

   
    private SlingHttpServletRequest request;

 

Also the component is a structual component and resides at the top-level: 

"/content/my-site/us/en/jcr:content/siteBanner

 

Avatar

Correct answer by
Level 2

To get this to work, I ended up using a utility method that retrieves the current page from a SlingHttpServletRequest. It does this by checking several possible sources for the page information:


1. It first checks if the suffix resource of the request is not null and if its path starts with CONTENT_ROOT. If so, it uses a getContainingPage method to get the page containing the suffix resource.
2. If the first check doesn't yield a page, it then checks if there is a parameter named ITEM_PARAMETER in the request. If there is, it uses the PageManager.getContainingPage method to get the page containing the resource specified by the ITEM_PARAMETER.
3. If the second check doesn't yield a page, it then checks the REFERER header of the request. If the header is not blank, it extracts the page path from the referer URL and uses the PageManager.getPage method to get the page at that path.