Is it possible to access pageProperties from a datasource?

Avatar

Avatar

alvarof55440963

Avatar

alvarof55440963

alvarof55440963

11-10-2018

Hi folks. Hope you are doing great.

So this is my problem. I need to dynamically load the options for a select item in a component dialog based on a page property. So I was planning to create a datasource that would read the page properties and then set the select options based on the value of the property. The problem is that if I try to access pageProperties or currentPage it is always null.

Is there a way to actually read page properties from a datasource?

I have my select in the component dialog like this:

<options

           jcr:primaryType="nt:unstructured"

           sling:resourceType="granite/ui/components/coral/foundation/form/select"

           fieldLabel="Options"

           name="./options">

           <datasource

                  jcr:primaryType="nt:unstructured"

                  sling:resourceType="examples/datasources/test"/>

</options>

And my datasource:

<%@page session="false" import="

                  com.day.cq.commons.inherit.HierarchyNodeInheritanceValueMap,

                  com.day.cq.commons.inherit.InheritanceValueMap,

                  com.adobe.granite.ui.components.ds.DataSource,

                  com.adobe.granite.ui.components.ds.EmptyDataSource,

                  com.adobe.granite.ui.components.ds.SimpleDataSource,

                  com.adobe.granite.ui.components.ds.ValueMapResource,

                  org.apache.sling.api.resource.Resource,

                  org.apache.sling.api.resource.ResourceMetadata,

                  org.apache.sling.api.resource.ResourceResolver,

                  org.apache.sling.api.resource.ValueMap,

                  org.apache.sling.api.wrappers.ValueMapDecorator,

                  java.util.ArrayList,

                  java.util.HashMap,

                  java.util.Iterator,

                  java.util.List"%>

<%

%><%@taglib prefix="cq" uri="http://www.day.com/taglibs/cq/1.0" %><%

%><cq:defineObjects/><%

     //pageProperties always null here

%>

I'm using AEM 6.4

Thanks

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar

Arun_Patidar

MVP

Total Posts

3.0K

Likes

1.1K

Correct Reply

840

Avatar

Arun_Patidar

MVP

Total Posts

3.0K

Likes

1.1K

Correct Reply

840
Arun_Patidar
MVP

11-10-2018

Hi,

You can get the Page Property by getting page path and adapt to Node

Example :

  ResourceResolver resolver = request.getResourceResolver();

  String pagePath = request.getRequestPathInfo().getSuffix().replaceAll("/jcr:content/.*$", "");

  Node pageNode = resolver.getResource(pagePath).adaptTo(Node.class);

  String pageTitle= pageNode.getProperty("jcr:title").getString();

Answers (1)

Answers (1)

Avatar

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Reply

2.3K

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Reply

2.3K
smacdonald2008

11-10-2018

I would use Java and HTL. You can read the page props using Page Manager API.

Using a Page object - you can obtain a lot of information about a given page.

Page ("The Adobe AEM Quickstart and Web Application.")

Then you can put that into a MAP on which a DataSource is based.

See this to learn how to use Java to create a DataSource to populate a Select:

Adobe Experience Manager Help | Using an WCMUsePojo class to populate an Experience Manager Touch UI...