Expand my Community achievements bar.

SOLVED

How to dynamically fetch child pages ?

Avatar

Level 7

Hello, I have to fetch the childpages using a pathbrowser dynamically. How to do it without mentioning the page path in the sling model?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @Sanjana12,

 

Path browser is basically a cq:dialog widget.

example:

<pagepath
        jcr:primaryType="nt:unstructured"
        sling:resourceType="granite/ui/components/foundation/form/pathbrowser"
        fieldLabel="Page"
        name="./pagePath"
        required="{Boolean}true"
        rootPath="/content/project-name"/>

which creates a dialog field widget like this. Screenshot 2022-09-13 at 10.26.48 PM.png

whenever a value is authored in that field, it gets saved as the "name" property mentioned in the dialog.

Now to get this value to your sling model, you can use the following ways:
1. if you sling model is of SlingHttpServletRequest.class, then 

@ValueMapValue
private String pagePath;

or 

@Inject @Via("resource")
private String pagePath;

2. If your sling model is of resource type, then

@Inject
private String pagePath;

Once you get your path from the path browser, you now require a Resource Resolver, to track down to your resource and get the child pages.

 

you can use the way how @ksh_ingole7 mentioned, but I would like to improvise the injection of resource resolver  there 

@SlingObject
ResourceResolver resourceResolver;

Now here comes the question what is a resource resolver?

Answer:
In simple words, a Resource Resolver is a basically an interface between the objects in the repository (Sling considers them as resources) and the code you write. Since the interface api helps to access the resources whilst having all the access related factors (resolution), it is hence named Resource Resolver.

Correct syntax of using Resource resolver:

In a Sling Servlet, resource resolver is available to the request processing servlet through the SlingHttpServletRequest.getResourceResolver()  method. 

In a Sling Model, you can have the instance of resource resolver through the injector specific annotation "@SlingObject"

@SlingObject
    private ResourceResolver resourceResolver;

In other way, you can also get the resource resolver through ResourceResolverFactory.

@Reference 
private transient ResourceResolverFactory resourceResolverFactory; 

ResourceResolver resourceResolver = resourceResolverFactory.getServiceResourceResolver(ResourceResolverFactory.SUBSERVICE, "service-user-name"); 

In the above code, the parameters passed in the getServiceResourceResolver are sub service constant and service-user-name, which is the system user id who have access to the resources.

to know more on how to create a system/service user, please refer this official document or follow this simple video.


I hope this helps.
Thank you.

View solution in original post

4 Replies

Avatar

Community Advisor

Hi @Sanjana12 

 

Please see the below Model .

 

ksh_ingole7_0-1663086904279.png

 

Here instead of hard coding the path, you can get the Path from dialog through the Path browser filed and pass it to the getResource method.  for e.g resourceResolver.getResource(path-from-pathbrowser);

 

Now adapt to Page.class and then iterate over the parent page to fetch the children as shown. 

 

Thanks

Avatar

Level 7

how to get the path from pathbrowser? Can you explain that?

Avatar

Community Advisor

Hi @Sanjana12 

 

Dialog
<fromList
    jcr:primaryType="nt:unstructured"
    sling:resourceType="granite/ui/components/coral/foundation/form/pathbrowser"
    fieldDescription="Path of the image."
    fieldLabel="Path"
    name="./pathImage"
    required="{Boolean}true"
    rootPath="/content/project"/>

Model 
You can use a @Inject or @ValueMapValue to get the value from Dialog and use the same in resource path

Avatar

Correct answer by
Community Advisor

Hi @Sanjana12,

 

Path browser is basically a cq:dialog widget.

example:

<pagepath
        jcr:primaryType="nt:unstructured"
        sling:resourceType="granite/ui/components/foundation/form/pathbrowser"
        fieldLabel="Page"
        name="./pagePath"
        required="{Boolean}true"
        rootPath="/content/project-name"/>

which creates a dialog field widget like this. Screenshot 2022-09-13 at 10.26.48 PM.png

whenever a value is authored in that field, it gets saved as the "name" property mentioned in the dialog.

Now to get this value to your sling model, you can use the following ways:
1. if you sling model is of SlingHttpServletRequest.class, then 

@ValueMapValue
private String pagePath;

or 

@Inject @Via("resource")
private String pagePath;

2. If your sling model is of resource type, then

@Inject
private String pagePath;

Once you get your path from the path browser, you now require a Resource Resolver, to track down to your resource and get the child pages.

 

you can use the way how @ksh_ingole7 mentioned, but I would like to improvise the injection of resource resolver  there 

@SlingObject
ResourceResolver resourceResolver;

Now here comes the question what is a resource resolver?

Answer:
In simple words, a Resource Resolver is a basically an interface between the objects in the repository (Sling considers them as resources) and the code you write. Since the interface api helps to access the resources whilst having all the access related factors (resolution), it is hence named Resource Resolver.

Correct syntax of using Resource resolver:

In a Sling Servlet, resource resolver is available to the request processing servlet through the SlingHttpServletRequest.getResourceResolver()  method. 

In a Sling Model, you can have the instance of resource resolver through the injector specific annotation "@SlingObject"

@SlingObject
    private ResourceResolver resourceResolver;

In other way, you can also get the resource resolver through ResourceResolverFactory.

@Reference 
private transient ResourceResolverFactory resourceResolverFactory; 

ResourceResolver resourceResolver = resourceResolverFactory.getServiceResourceResolver(ResourceResolverFactory.SUBSERVICE, "service-user-name"); 

In the above code, the parameters passed in the getServiceResourceResolver are sub service constant and service-user-name, which is the system user id who have access to the resources.

to know more on how to create a system/service user, please refer this official document or follow this simple video.


I hope this helps.
Thank you.