Reference component using page properties of page where it is included | Community
Skip to main content
Level 3
June 8, 2017
Solved

Reference component using page properties of page where it is included

  • June 8, 2017
  • 4 replies
  • 4025 views

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?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Eakambaram

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)

     }

4 replies

Feike_Visser1
Adobe Employee
Adobe Employee
June 8, 2017

Try using resourcePage-object, that should do it.

Level 3
June 8, 2017

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.

kautuk_sahni
Community Manager
Community Manager
June 12, 2017

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
EakambaramAuthorAccepted solution
Level 3
June 21, 2017

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)

     }