Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

How get current selected page path in datasource sling model

Avatar

Level 4

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>
Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Level 4

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();
}

View solution in original post

5 Replies

Avatar

Correct answer by
Level 4

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();
}

Avatar

Community Advisor

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);
}

}
-----------------------------------------------

 



Avatar

Community Advisor

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

http://localhost:4502/mnt/overlay/wcm/core/content/sites/properties.html?item=%2Fcontent%2Fwknd%2Fla...

 



Arun Patidar

Avatar

Administrator

@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.



Kautuk Sahni