Show/Hide Page Properties Based on Template in AEM


 

In AEM , editable templates usually share the same page component, which means the same page properties dialog.

Below are the steps to show/hide page properties based on the template.

1. Create a jsp under apps “/apps/project-name/granite/rendercondition/template/template.jsp” with below code.

        <%@include file="/libs/foundation/global.jsp"%>
  <%@page session="false"
  import="com.adobe.granite.ui.components.Config,
        com.adobe.granite.ui.components.rendercondition.RenderCondition,
        com.adobe.granite.ui.components.rendercondition.SimpleRenderCondition,
        com.core.utils.TemplateRenderConditionUtil,
        org.apache.sling.api.resource.Resource,
        org.apache.sling.api.resource.ValueMap,
        com.adobe.granite.ui.components.ComponentHelper,
  com.adobe.granite.xss.XSSAPI,
  com.day.cq.i18n.I18n"%>   
        <sling:defineObjects/>
            <%
            final ComponentHelper cmp = new ComponentHelper(pageContext);
            Config cfg = cmp.getConfig();
            String path = cfg.get("templatePath", "");
            boolean vote = TemplateRenderConditionUtil.isTemplate(slingRequest, request, path);
            request.setAttribute(RenderCondition.class.getName(), new SimpleRenderCondition(vote));    
      %>

    2.   Now create a java file named TemplateRenderConditionUtil with below code as you can see we have used this java class in the jsp code.

    import com.day.cq.wcm.api.Page;
    import com.day.cq.wcm.api.Template;
    import java.util.Optional;
    import javax.servlet.http.HttpServletRequest;
    import org.apache.commons.lang3.StringUtils;
    import org.apache.sling.api.SlingHttpServletRequest; 
    public class TemplateRenderConditionUtil {
    private static final String PAGE_PROPERTIES = "wcm/core/content/sites/properties";
    public static boolean isTemplate(
    SlingHttpServletRequest slingHttpServletRequest,
    HttpServletRequest httpServletRequest,
    String templatePath) {
    // error if any of the passed params is null.
    if (slingHttpServletRequest == null || httpServletRequest == null
    || StringUtils.isBlank(templatePath)) {
      throw new IllegalArgumentException("One of the passed parameters is null.");
    }
    // the dialog is a page properties dialog
    if (StringUtils.contains(httpServletRequest.getPathInfo(), PAGE_PROPERTIES)) {
    // get the actual page path
    String pagePath = httpServletRequest.getParameter("item");
    // get page template path and check it
    return Optional.ofNullable(slingHttpServletRequest.getResourceResolver())
          .map(resourceResolver -> resourceResolver.getResource(pagePath))
          .map(pageResource -> pageResource.adaptTo(Page.class))
          .map(Page::getTemplate)
          .map(Template::getPath)
          .map(path -> StringUtils.contains(path, templatePath))
          .orElse(false);
        }
        return false;
      }
    }

3.  Now you can add a granite:rendercondition node under any of your properties to show/hide them based on template path.
<errorpages
  jcr:primaryType="nt:unstructured"
 sling:resourceType="granite/ui/components/coral/foundation/form/pathfield"
 cq:showOnCreate="{Boolean}true"
 fieldDescription="Error pages for this content tree"
 fieldLabel="Error Pages"
 name="./errorPages"
 <granite:rendercondition
            jcr:primaryType="nt:unstructured"
            sling:resourceType="project-name/granite/rendercondition/template"
            templatePath="/conf/project-name/settings/wcm/templates/template-name"/>
</errorpages>

Reference : Blog

Hope this helps!!

Happy Coding 🙏

If you like my post and find it helpful, you can buy me a coffee.






Comments

  1. Hi,

    Is there any possibilty to provide mutiple template paths for "templatePath". I want to achieve same feature for more than 1 template. Thank you in advance.

    ReplyDelete
    Replies
    1. Yes, customize it by adding your template paths as string array and update the code accordingly

      Delete
  2. Does this work in AEM 6.5? I am unable to get the class in JSP. getting below error.
    An error occurred at line: 22 in the generated java file
    Only a type can be imported. com.bsca.core.render.utils.TemplateRenderConditionUtil resolves to a package

    An error occurred at line: 17 in the jsp file: /apps/bsca/granite/rendercondition/template/template.jsp
    TemplateRenderConditionUtil cannot be resolved
    14: final ComponentHelper cmp = new ComponentHelper(pageContext);
    15: Config cfg = cmp.getConfig();
    16: String path = cfg.get("templatePath", "");
    17: boolean vote = TemplateRenderConditionUtil.isTemplate(slingRequest, request, path);
    18: request.setAttribute(RenderCondition.class.getName(), new SimpleRenderCondition(vote));
    19: %>

    I have added the java code in side core module of maven project.

    ReplyDelete
    Replies
    1. As suggested in the error import the package --- com.bsca.core.render.utils.*

      Delete
    2. I had built and deployed the code. Imported the same class in jsp. Still error persists

      Delete
    3. Have the same error, did you solve it?

      Delete
    4. Forgot about package. solved

      Delete
    5. I am getting same error. Does it work with AEM 6.5?
      ERROR-
      org.apache.sling.api.scripting.ScriptEvaluationException: org.apache.sling.scripting.jsp.jasper.JasperException: Unable to compile class for JSP:

      An error occurred at line: 25 in the jsp file: /apps/kf/granite/rendercondition/template/template.jsp
      TemplateRenderConditionUtil cannot be resolved
      22: final ComponentHelper cmp = new ComponentHelper(pageContext);
      23: Config cfg = cmp.getConfig();
      24: String path = cfg.get("templatePath", "");
      25: boolean vote = TemplateRenderConditionUtil.isTemplate(slingRequest, request, path);
      26: request.setAttribute(RenderCondition.class.getName(), new SimpleRenderCondition(vote));
      27:

      Could you please suggest me solutions.

      Thanks in advance.

      Delete

Post a Comment