April 9, 2020
Estimated Post Reading Time ~

Hierarchical Page Property Sling Models Injector

Purpose
Allows for Sling Models to inject an inherited page property (jcr:content of the page) into your sling model. This works whether you have your sling model on the page itself, or if you have a resource that resides under a page. Used 
PageManager.getContainingPage to retrieve the page.

If the property is not on the current page, it will use InheritanceValueMap to attempt to retrieve property from parent pages. Notice: Please consider if using context aware configuration is better for your use case.

Apart from above, behaves exactly like @ValueMapValue, which supports a single primitive value, or a list/collection / array of primitives. Injections are available when adapting either a Resource or SlingHttpServletRequest object. 

Note: Out of performance reasons, only works with @HierarchicalPageProperty as annotation, not @Inject.

Example
//we set our design property (cq:designPath) on our language root page.
//now we want to use that property in a component that is somewhere down in the hierarchy.

//design property located on: /content/acs-commons/en/jcr:content
//injected resource path: /content/acs-commons/en/page/jcr:content/mycomponent

@Model(adaptables = { SlingHttpServletRequest.class, Resource.class })
public class TestModel {

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

public String getDesignPath() {
//outputs design property set on our language root page with path: /content/acs-commons/en
return designPath;
}
}


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.