Hi,
I have a custom dropdown in page properties Basic Tab, This dropdown show while creating new page. I want take the current selected page and get values from OSGi service to show the drop.
How I can get the current page path in datasource sling model?
Below is my code.
1. DatasourceModel.java
@Model(adaptables = SlingHttpServletRequest.class)
public class DatasourceModel {
private static final Logger LOG = LoggerFactory.getLogger(DatasourceModel.class);
@Self
protected SlingHttpServletRequest request;
@inject
@required
@SlingObject
private ResourceResolver resourceResolver;
@PostConstruct
public void init(){
// get List of values from OSGi service by passing current page(parent page) path
ValueMap valueMap = new ValueMapDecorator(new HashMap<>());
valueMap.put("text", "textOne");
valueMap.put("value", "valueOne");
List<Resource> resourceList = new ArrayList<>();
resourceList.add(new ValueMapResource(resourceResolver,
new ResourceMetadata(), "nt:unstructured", valueMap));
DataSource ds = new SimpleDataSource(resourceList.iterator());
request.setAttribute(DataSource.class.getName(),ds);
}
}
2. datasource.html
<sly data-sly-use="com.mysite.core.models.DatasourceModel"/>
3. dialog.xml
<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:granite="http://www.adobe.com/jcr/granite/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0"
jcr:primaryType="nt:unstructured"
jcr:title="Page"
sling:resourceType="cq/gui/components/authoring/dialog"
extraClientlibs="[cq.common.wcm,core.wcm.components.page.v3.editor,cq.wcm.msm.properties,granite.contexthub.configuration,cq.siteadmin.admin.properties,core.wcm.components.image.v3.editor]"
helpPath="https://www.adobe.com/go/aem_cmp_page_v3"
mode="edit"
trackingFeature="core-components:page:v3">
<content
granite:class="cq-dialog-content-page"
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/container">
<items jcr:primaryType="nt:unstructured">
<tabs
granite:class="cq-siteadmin-admin-properties-tabs"
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/tabs"
size="L">
<items jcr:primaryType="nt:unstructured">
<basic
jcr:primaryType="nt:unstructured"
jcr:title="Basic"
sling:resourceType="granite/ui/components/coral/foundation/fixedcolumns">
<items jcr:primaryType="nt:unstructured">
<column
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/container">
<items jcr:primaryType="nt:unstructured">
<mydropdown
cq:showOnCreate="{Boolean}true"
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/select"
fieldLabel="My Dropdown"
name="./mydropdown">
<datasource
jcr:primaryType="nt:unstructured"
sling:resourceType="/apps/mysite/components/datasource"/>
</mydropdown>
</items>
</column>
</items>
</basic>
</items>
</tabs>
</items>
</content>
</jcr:root>
Solved! Go to Solution.
Topics help categorize Community content and increase your ability to discover relevant content.
Views
Replies
Total Likes
Hi @codingStar
You can try below snippet to get current page path in the java class.
String requestPagePath = StringUtils.EMPTY;
String pagePath = StringUtils.EMPTY;
Enumeration<String> values = request.getHeaders("Referer");
if (values != null){
while (values.hasMoreElements()) {
requestPagePath= values.nextElement();
}
}
// to get current page path while creating a new page
if (requestPagePath.contains("createpagewizard.html/")) {
pagePath = requestPagePath.split("createpagewizard.html")[1];
}
// to get current page path while opening properties of existing page
RequestParameter resourceParameter = request.getRequestParameter("item");
if(resourceParameter != null && StringUtils.isNotEmpty(resourceParameter.getString())) {
pagePath = resourceParameter.getString();
}
Use below approach to have dynamic dialog dropdown using datasource using servlet.
https://unlocklearning.in/dynamic-dropdown-in-aem/
http://www.sgaemsolutions.com/2019/01/dynamically-populate-drop-down-values.html
This should be path of servlet -> sling:resourceType="/apps/mysite/components/datasource"
Hi @codingStar
You can try below snippet to get current page path in the java class.
String requestPagePath = StringUtils.EMPTY;
String pagePath = StringUtils.EMPTY;
Enumeration<String> values = request.getHeaders("Referer");
if (values != null){
while (values.hasMoreElements()) {
requestPagePath= values.nextElement();
}
}
// to get current page path while creating a new page
if (requestPagePath.contains("createpagewizard.html/")) {
pagePath = requestPagePath.split("createpagewizard.html")[1];
}
// to get current page path while opening properties of existing page
RequestParameter resourceParameter = request.getRequestParameter("item");
if(resourceParameter != null && StringUtils.isNotEmpty(resourceParameter.getString())) {
pagePath = resourceParameter.getString();
}
Hi @codingStar
To get the current page path in your `DatasourceModel.java` class, you can use the `Resource` object available in the `SlingHttpServletRequest` object. You can get the current page's `Resource` object using the `getResource()` method of the `SlingHttpServletRequest` object. Once you have the `Resource` object, you can get its path using the `getPath()` method.
---------------------
@Model(adaptables = SlingHttpServletRequest.class)
public class DatasourceModel {
private static final Logger LOG = LoggerFactory.getLogger(DatasourceModel.class);
@Self
protected SlingHttpServletRequest request;
@Inject
@Required
@SlingObject
private ResourceResolver resourceResolver;
@PostConstruct
public void init(){
// get current page path
Resource resource = request.getResource();
String currentPagePath = resource.getPath();
// get List of values from OSGi service by passing current page(parent page) path
ValueMap valueMap = new ValueMapDecorator(new HashMap<>());
valueMap.put("text", "textOne");
valueMap.put("value", "valueOne");
List<Resource> resourceList = new ArrayList<>();
resourceList.add(new ValueMapResource(resourceResolver,
new ResourceMetadata(), "nt:unstructured", valueMap));
DataSource ds = new SimpleDataSource(resourceList.iterator());
request.setAttribute(DataSource.class.getName(),ds);
}
}
-----------------------------------------------
Hi @codingStar
you can get the curent page path from the URL.
example : if you decode item query parem you will get the page path
@codingStar Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies