Tuesday 2 July 2019

AEM - Granite Custom Render Conditions using Servlet

Granite Render Condition

The granite render condition allows you to show(render)/hide(not render) granite resources based on some condition.
To understand and know more about Granite Render Condition please check below articles:


Custom Granite Render Condition

The OOTB render Render Condition resources/components are JSP based and above blogs also explained how to create and use custom render condition using JSP but if you want to do it 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:

  1. Create a servlet and registered with sling resourceType
  2. 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));

Example

This 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 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 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 which can be reduced to 2 renders (App and site/content path(ACS Path Render))  and can be used with Or/And rendercondition.

2 comments:

  1. Thanks for this post Aru. Is it okay to use this code as is?

    ReplyDelete
    Replies
    1. Anyone can use this code, feel free to use, improve :D

      Delete

AEMaaCS - Core Component's Children Editor

If you are using core components, you should be familiar with the children-editor for Tabs, Accordion, and Carousel. These components are th...

About Me

My photo
https://www.linkedin.com/in/arunpatidar26/ https://experienceleaguecommunities.adobe.com/t5/user/viewprofilepage/user-id/6786635 https://community.adobe.com/t5/user/viewprofilepage/user-id/12372253 https://forums.adobe.com/people/Arun+Patidar