Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

Reference component using page properties of page where it is included

Avatar

Level 4

Hi Team,

We have a component using page properties in side it. When we use that component via reference component it is using the page properties of the page where that component dragged and dropped (It is working as per the Resource principles). 

But we want that component to use the page properties of the page where it is included via reference component. We have a business use case to use the page properties of the page where it got included instead of the page where it got dragged and dropped.

We are using AEM 6.1 reference component.

Any inputs?

1 Accepted Solution

Avatar

Correct answer by
Level 4

Thank you all for responding. We have solved this in below way.

We changed the reference component  code of sling include  a little bit by adding a selector to it as below.

<sling:include path="<%= target %>" addSelectors="getCurrentPageProps"/></div>

As we added this selector. We are looking for this selector in our wcm use pojo class. If this selector presents then we are getting the page properties of the page where reference component is dragged and dropped other wise getting the page properties of where the actual component dragged and dropped.

  String inhetitedProp1  = "";

  if (getRequest().getRequestPathInfo().getSelectors().length > 0) {

//From current page props

  InheritanceValueMap iProperties = new HierarchyNodeInheritanceValueMap(getCurrentPage().getContentResource());

     inhetitedProp1 = iProperties.getInherited(property, null)

  } else {

     //from page props of where the component is included

           inhetitedProp1 =  getInheritedProperties().get(property)

     }

View solution in original post

4 Replies

Avatar

Employee

Try using resourcePage-object, that should do it.

Avatar

Level 4

Thank you for responding.

Can you please elaborate that.  How we can pass on the current page resource to the actual component via the reference component.

In reference component we are just sling inluding the content path.

Avatar

Administrator

ResourcePage refers to the page containing your resource and is an instance of com.day.cq.wcm.api.Page, and resourceDesign is the design object for the resource page.

The resourcePage can be obtained using PageManager's #getContainingPage() as shown here.

import org.apache.sling.api.servlets.SlingAllMethodsServlet;

...

@Override

protected void doGet(SlingHttpServletRequest slingRequest,

    SlingHttpServletResponse slingResponse) throws ServletException, IOException {

    ResourceResolver resourceResolver = slingRequest.getResourceResolver();

    PageManager pageManager = resourceResolver.adaptTo(PageManager.class);

    Page resourcePage = pageManager.getContainingPage(slingRequest.getResource());

    Designer designer = resourceResolver.adaptTo(Designer.class);

    Design resourceDesign = designer.getDesign(resourcePage);

Reference Post:- servlets - Finding resourceDesign and resourcePage in Java for Adobe CQ - Stack Overflow

~kautuk



Kautuk Sahni

Avatar

Correct answer by
Level 4

Thank you all for responding. We have solved this in below way.

We changed the reference component  code of sling include  a little bit by adding a selector to it as below.

<sling:include path="<%= target %>" addSelectors="getCurrentPageProps"/></div>

As we added this selector. We are looking for this selector in our wcm use pojo class. If this selector presents then we are getting the page properties of the page where reference component is dragged and dropped other wise getting the page properties of where the actual component dragged and dropped.

  String inhetitedProp1  = "";

  if (getRequest().getRequestPathInfo().getSelectors().length > 0) {

//From current page props

  InheritanceValueMap iProperties = new HierarchyNodeInheritanceValueMap(getCurrentPage().getContentResource());

     inhetitedProp1 = iProperties.getInherited(property, null)

  } else {

     //from page props of where the component is included

           inhetitedProp1 =  getInheritedProperties().get(property)

     }