Expand my Community achievements bar.

Applications for the 2024-2025 Adobe Experience Manager Champion Program are open!

Show/Hide Page Properties/tabs based on Template paths in AEM | AEM Community Blog Seeding

Avatar

Administrator

BlogImage.jpg

Show/Hide Page Properties/tabs based on Template paths in AEM by Shaji Ahmed

Abstract

This blog is an extension to show/hide page properties for multiple templates from my ex-colleague Ahmed Musallam’s post How to show/hide page properties based on a single template path.

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 paths.

Create a servlet
package com.taqalytics.core.servlets;

import java.io.IOException;
import java.util.Arrays;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.adobe.granite.ui.components.rendercondition.RenderCondition;
import com.adobe.granite.ui.components.rendercondition.SimpleRenderCondition;
import com.day.cq.wcm.api.Page;

/**
* Servlet that use to Show/Hide Page Properties Based on allowed Templates in AEM
*
* A condition that renders page properties based on page template path
* add a granite:rendercondition node under any of your properties to show/hide
* them based on template path.
*
*/

@Component(service = Servlet.class, property = {
Constants.SERVICE_DESCRIPTION + "=Custom Path RenderConditions Servlet",
"sling.servlet.methods=" + HttpConstants.METHOD_GET,
"sling.servlet.resourceTypes=" + "utils/granite/rendercondition/simple/taqalytics-apps" })
public class TemplateBasedPropertiesRenderer extends SlingSafeMethodsServlet {

private static final long serialVersionUID = 1L;
private static final String PAGE_PROPERTIES = "wcm/core/content/sites/properties";

Logger logger = LoggerFactory.getLogger(this.getClass());

@Override
protected void doGet(final SlingHttpServletRequest req, final SlingHttpServletResponse resp)
throws ServletException, IOException {

final SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) req;

ValueMap cfg = ResourceUtil.getValueMap(req.getResource());

String[] paths = cfg.get("allowedPaths", new String[] {});

boolean vote = isTemplate(slingRequest, req, paths);
req.setAttribute(RenderCondition.class.getName(), new SimpleRenderCondition(vote));

}

public static boolean isTemplate(SlingHttpServletRequest slingHttpServletRequest,
HttpServletRequest httpServletRequest, String[] allowedPaths) {
// error if any of the passed params is null.
if (slingHttpServletRequest == null || httpServletRequest == null || allowedPaths ==null ) {
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
ResourceResolver resourceResolver=slingHttpServletRequest.getResourceResolver();
Resource resource = resourceResolver.getResource(pagePath);
Page page = resource.adaptTo(Page.class);
String template = page.getContentResource().getValueMap().get("cq:template", "");
boolean doesContain = Arrays.stream(allowedPaths)
.anyMatch(template::equals);

return doesContain;

}
return false;
}


}

Read Full Blog

Show/Hide Page Properties/tabs based on Template paths in AEM

Q&A

Please use this thread to ask the related questions.



Kautuk Sahni
0 Replies