Highlighted

Show/Hide Page Properties Based on Template in AEM | AEM Community Blog Seeding

Avatar

Avatar

kautuk_sahni

Community Manager

Total Posts

5.6K

Likes

961

Correct Answer

1.1K

Avatar

kautuk_sahni

Community Manager

Total Posts

5.6K

Likes

961

Correct Answer

1.1K
kautuk_sahni
Community Manager

03-09-2020

BlogImage.jpg

Show/Hide Page Properties Based on Template in AEM by Bimmisoi

Abstract

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"%>

<%
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;
}
}

Read Full Blog

Show/Hide Page Properties Based on Template in AEM

Q&A

Please use this thread to ask the related questions.

AEM AEMEBlogSeeding Experience Manager