Skip to main content
manikanthar1295
Level 5
Sep 27, 2023 3:43 AM UTC
Solved

Page properties in sling model

Hi Team,

 

I Need to get page properties in the sling model if page property is null or empty then we need to get from its parent if that also null or empty then we need to get from its parent the like inherited page properties.

if any one did like these please share sling model login so that it will be usefull 

 

Thanks and Regards

Manikantha R

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
raushan123Accepted solution
Level 4
Sep 27, 2023 4:14 AM UTC

 

HI @manikanthar1295  if you want to read page properties you have to inject the page, with that you can able to read page properties. And if you want to read inherited properties you have to use HierarchyNodeInheritanceValueMap.

 

 

Here is the piece of code.

 

@Model(adaptables = {SlingHttpServletRequest.class,
Resource. class}, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class TitleModel {


private String showDynamicMediaImage;

@Inject
private Page currentPage;

@PostConstruct
public void init() {
final HierarchyNodeInheritanceValueMap inheritanceMap = new HierarchyNodeInheritanceValueMap(currentPage.getContentResource());
showDynamicMediaImage = Objects.nonNull(currentPage.getProperties().get("showDynamicMediaImage", String.class))
? inheritanceMap.getInherited("showDynamicMediaImage", String.class)
: FALSE;
}
 
}

 

 

5 replies

raushan123Accepted solution
Level 4
Sep 27, 2023 4:14 AM UTC

 

HI @manikanthar1295  if you want to read page properties you have to inject the page, with that you can able to read page properties. And if you want to read inherited properties you have to use HierarchyNodeInheritanceValueMap.

 

 

Here is the piece of code.

 

@Model(adaptables = {SlingHttpServletRequest.class,
Resource. class}, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class TitleModel {


private String showDynamicMediaImage;

@Inject
private Page currentPage;

@PostConstruct
public void init() {
final HierarchyNodeInheritanceValueMap inheritanceMap = new HierarchyNodeInheritanceValueMap(currentPage.getContentResource());
showDynamicMediaImage = Objects.nonNull(currentPage.getProperties().get("showDynamicMediaImage", String.class))
? inheritanceMap.getInherited("showDynamicMediaImage", String.class)
: FALSE;
}
 
}

 

 

Translate
Mahedi_Sabuj
Community Advisor
Community Advisor
Sep 27, 2023 5:02 AM UTC

If you already have acs-commons installed, you can use @HierarchicalPageProperty Sling Models Injector. 

@HierarchicalPageProperty("cq:designPath") private String designPath;

https://adobe-consulting-services.github.io/acs-aem-commons/features/sling-model-injectors/hierarchical-page-property/index.html  

Translate
Mahedi Sabuj
kautuk_sahni
Level 9
Sep 28, 2023 9:22 AM UTC

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

Translate
Kautuk Sahni
Level 2
Mar 26, 2024 4:00 AM UTC

here is my context.xml

<?xml version="1.0" encoding="UTF-8"?> <jcr:root xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0" xmlns:granite="http://www.adobe.com/jcr/granite/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:sling="http://sling.apache.org/jcr/sling/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"> <newtab cq:showOnCreate="{Boolean}true" jcr:primaryType="nt:unstructured" jcr:title="New Tab" 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"> <section jcr:primaryType="nt:unstructured" jcr:title="New Custom tab" sling:resourceType="granite/ui/components/coral/foundation/form/fieldset"> <items jcr:primaryType="nt:unstructured"> <textfield jcr:primaryType="nt:unstructured" sling:resourceType="granite/ui/components/coral/foundation/form/textfield" name="./testfield"/> </items> </section> </items> </column> </items> </newtab> </items> </tabs> </items> </content> </jcr:root>

 

here is my custom properties 

 

how to get this page properties

Translate
Mahedi_Sabuj
Community Advisor
Community Advisor
Apr 4, 2024 6:58 PM UTC

You can retrieve page properties values in HTL using the following syntax.

${pageProperties.testfield}
Translate
Mahedi Sabuj