January 3, 2021
Estimated Post Reading Time ~

Granite Custom Render Conditions using Servlet

Granite Render ConditionThe granite renders condition allows you to show(render)/hide(not render) granite resources based on some condition.

Custom Granite Render ConditionThe OOTB render Render Condition resources/components are JSP based and the above blogs also explained how to create and use custom render conditions using JSP but if you want to do it the same using servlet then follow this post. This post is only about how to create servlet based custom render condition resources.

Steps are as follows:
Create a servlet and registered with sling resourceType
Set Render Condition.class.getName() attribute with new SimpleRenderCondition(flag), where flag is true for render field and false to not render field.
request.setAttribute(RenderCondition.class.getName(), new SimpleRenderCondition(flag));
ExampleThis example same as ACS Commons's JSP based Path Render Condition . For demo I modified it little bit. Following are the changes :

Accept multiple paths.
It does not render the field for specified path instead of render for ACS path render.
It can be used for the component's dialog field to render a field based on-site and/or apps. This use case may come when there are common dialog fields and included in the component dialog using granite includes like OOTB page property dialog fields in AEM 6.4 and for example if some of the fields should not be rendered for other apps.
How : Created a granite:rendercondition node as a child node of the field which wanted to render/not render based on condition and set the following properties.

sling:resourceType - The resource type to which servlet is registered i.e. utils/granite/rendercondition/simple/sites-apps
hiddenSitePaths - Multifield property contains paths regex for which field will be hidden based on the current path, example values /contet/we-retail/.*
This is an optional property if hiddenAppPaths property specified.
hiddenAppPaths - Multifield property contains paths regex for which field will be hidden based on the component path, example values /apps/AEM63Apps/.*
This is an optional property if hiddenSitePaths property specified
and - true to not rendered field based on both App and Content path regex, false otherwise, default is false. This is an optional property.

Either hiddenSitePaths or hiddenAppPaths properties should be used based on scenarios. Both can be used as well.

granite:rendercondition node's properties

Example Servlet code
https://github.com/arunpatidar02/aem63app-repo/blob/master/java/CustomRenderConditionsServlet.java

Conclusion
  • The granite render condition is a powerful feature of the granite framework and can be used to render conditional fields.
  • The condition can be simple or complex. In this post, the example custom render is a complex type that can be reduced to 2 renders (App and site/content path(ACS Path Render)) and can be used with Or/And rendercondition.


By aem4beginner

No comments:

Post a Comment

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