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.
Solved! Go to Solution.
Views
Replies
Total Likes
Hi @JakeCham
First you can inject a Current Resource in your child component sling model as below
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)
| 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/Pa... https://www.tabnine.com/code/java/methods/com.day.cq.wcm.api.Page/getParent
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.
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.
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
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?
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
Hi @JakeCham
First you can inject a Current Resource in your child component sling model as below
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.
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.
To get the grand parent node from the child node, you can use the Resource Util Interface.
This has a method getParent() which can be used as shown below -
Reference URL - https://developer.adobe.com/experience-manager/reference-materials/6-5/javadoc/index.html?org/apache...