Hello, I have to fetch the childpages using a pathbrowser dynamically. How to do it without mentioning the page path in the sling model?
Solved! Go to Solution.
Views
Replies
Total Likes
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.
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.
Hi @Sanjana12
Please see the below Model .
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
how to get the path from pathbrowser? Can you explain that?
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
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.
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.
Views
Likes
Replies
Views
Likes
Replies