How to create custom Sling Model for Pages that inherits from Core Component model? | Community
Skip to main content
Level 4
February 2, 2023

How to create custom Sling Model for Pages that inherits from Core Component model?

  • February 2, 2023
  • 1 reply
  • 1788 views

All of the pages on my site have their own sling types set up, and the sling:resourceSuperType inheritance is:

 

myapp/components/pages/page-type --> sling:resourceSuperType --> myapp/components/page --> core/wcm/components/page/v3/page

 

Now, I am trying to create a model for myapp/components/page called BasePageModel.  I am using queryBuilder to retrieve a list of resources, and I want to adapt each one to an instance of BasePageModel to simplify the logic around reading properties.

 

What model can I inherit from in Java to get some base functionality for core/wcm/components/page/v3/page?

 

For instance, I don't want to rewrite the logic around Vanity URLs or retrieving the page's featured image.  How can I simply inherit more base functionality from the core components and extend it with my own custom properties in Java?

 

I've found this page interface: aem-core-wcm-components/Page.java at main · adobe/aem-core-wcm-components (github.com) ... this is basically the functionality I want to inherit on my BasePageModel (not override).

 

Here's the corresponding model in Core components for page v3 (ultimately what I am inheriting from in sling): aem-core-wcm-components/PageImpl.java at main · adobe/aem-core-wcm-components (github.com) ... but this is marked internal.  It's also only adaptable from a SlingHttpServletRequest, where as I need to adapt to this model from a Resource returned by a com.day.cq.search.Query.getResult() call.

 

How can I re-use the functionality of PageImpl from core components?

 

I tried injecting like this:

 

@Model(adaptables = { SlingHttpServletRequest.class, Resource.class }, adapters = BasePage.class, // a simple interface resourceType = BasePageImpl.RESOURCE_TYPE, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL) public class BasePageImpl implements BasePage { final public static String RESOURCE_TYPE = "myapp/components/page"; @Self private com.adobe.cq.wcm.core.components.models.Page page; // null at this point }

 

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

1 reply

arunpatidar
Community Advisor
Community Advisor
February 3, 2023
Level 4
February 3, 2023

I tried that, but the page object is null.  I suspect it's because I'm adapting from a resource and not a servlet request?

 

e.g.,

 

    @Self
    
    private com.adobe.cq.wcm.core.components.models.Page page; // null at this point