Getting grand parent node path from a child | Community
Skip to main content
JakeCham
May 31, 2023
Solved

Getting grand parent node path from a child

  • May 31, 2023
  • 4 replies
  • 4423 views

Hi team,

 

I have a node structure like below where I need to get the Parent node path from the child node that meant I need get the grand parent path from a grand child. Like getting the current node path using ${currentNode.ideentifier} , Can be done using HTL or sling models. Please guide me in both ways

 

Thanks.

 

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 iamnjain

Hi @jakecham 

 

First you can inject a Current Resource in your child component sling model as below

 

@SlingObject
private Resource currentResource;
 
Then you can use above object to access grandparent valueMap properties. for example, in your case It will be 

 

String[] arr = currentResource.getParent().getParent().getValueMap();

It will return grandParent node properties valuemap in array.

Please post here if you face any issue. 🙂

4 replies

Siva_Sogalapalli
Community Advisor
Community Advisor
May 31, 2023

@jakecham 

It's better to write all your business logic in sling models/BE services instead of Sightly. 

 

You can use below method to get appropriate parent page details in sling models or services. 

 

Page getParent(int level)
Returns the relative parent page. If no page exists at that level, null is returned. Example (this path == /content/geometrixx/en/products)
 | level | returned                        |
 |     0 | /content/geometrixx/en/products |
 |     1 | /content/geometrixx/en          |
 |     2 | /content/geometrixx             |
 |     3 | /content                        |
 |     4 | null                            |
 

Parameters: level - hierarchy level of the parent page to retrieve Returns: the respective parent page or null Additional info: https://developer.adobe.com/experience-manager/reference-materials/6-5/javadoc/com/day/cq/wcm/api/Page.html#getParent-int- https://www.tabnine.com/code/java/methods/com.day.cq.wcm.api.Page/getParent

JakeCham
JakeChamAuthor
May 31, 2023

Hi @siva_sogalapalli 

Thanks for the reply.

 

You suggested about currentPage.getParent(); This is not going to help me cas here I'm using a model class and that model class is calling from the grand child and I want to get the grand parent from grand child.

Siva_Sogalapalli
Community Advisor
Community Advisor
May 31, 2023

we can call getParent() on any page object and not necessarily on current page only.

so In your case, Get the page object using child page path/resource and then call getParent() method.

You have multiple options to convert it page object based on your child page path or object type.

 

For example:  If you just have child page path as string obj then you can try something like below.

public Page getPage(ResourceResolver resolver, String path) {
Resource pageResource = resolver.getResource(path);
if(pageResource!=null){
return pageResource.adaptTo(Page.class);
}
return null;
}

OR If you have child page info of type resource Object, you can simply adapt to Page.class.

 

Hope this helps. 

iamnjain
Community Advisor
Community Advisor
May 31, 2023

Hi @jakecham 

Can you please tell about the type of node?
Is child node a page, where you will drop a component and want to access grand parent page and read it's properties?

JakeCham
JakeChamAuthor
May 31, 2023

No Im putting a child component inside parent component where in jcr there is a parsys also so it become grand relationship instead parent child.

I'm calling my model class inside child component where I need to access the parent node properties 

iamnjain
Community Advisor
iamnjainCommunity AdvisorAccepted solution
Community Advisor
May 31, 2023

Hi @jakecham 

 

First you can inject a Current Resource in your child component sling model as below

 

@SlingObject
private Resource currentResource;
 
Then you can use above object to access grandparent valueMap properties. for example, in your case It will be 

 

String[] arr = currentResource.getParent().getParent().getValueMap();

It will return grandParent node properties valuemap in array.

Please post here if you face any issue. 🙂
EstebanBustamante
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
May 31, 2023

Check the page API, if you are looking to get the grandparent page, I think that you could use

 

page.getAbsoluteParent(int)

 

OR as @siva_sogalapalli  mentioned use 

 

page.getParent(int)

 

Make sure you pass a parameter "level" (level - hierarchy level of the parent page to retrieve), please read carefully the documentation about how to use those methods.


https://developer.adobe.com/experience-manager/reference-materials/6-5/javadoc/com/day/cq/wcm/api/Page.html#getAbsoluteParent-int-

Esteban Bustamante
JakeCham
JakeChamAuthor
May 31, 2023

Hi,

Im putting a child component inside parent component where in jcr there is a parsys also so it become grand relationship instead parent child.

I'm calling my model class inside child component where I need to access the parent node properties.

Rohan_Garg
Community Advisor
Community Advisor
June 1, 2023