AEM 6550 - Move Render Condition for Assets with References | AEM Community Blog Seeding

Avatar

Avatar

kautuk_sahni

Community Manager

Total Posts

5.7K

Likes

1.0K

Correct Reply

1.1K

Avatar

kautuk_sahni

Community Manager

Total Posts

5.7K

Likes

1.0K

Correct Reply

1.1K
kautuk_sahni
Community Manager

19-08-2020

BlogImage.jpg

AEM 6550 - Move Render Condition for Assets with References by Sreekanth Choudry Nalabotu

Abstract

A sample render condition to show Move action bar button in Asset console only if one (or some) of the assets in a folder have references (say they are part of a Dynamic Media Image Set)

1) Overlay /libs/dam/gui/content/assets/jcr:content/actions/selection/moveasset/granite:rendercondition into /apps/dam/gui/content/assets/jcr:content/actions/selection/moveasset/granite:rendercondition

2) Set /apps/dam/gui/content/assets/jcr:content/actions/selection/moveasset/granite:rendercondition@sling:resourceType to /apps/eaem-move-references-rendercondition

3) Create the Render condition /apps/eaem-move-references-rendercondition/eaem-move-references-rendercondition.jsp with following code

<%@page session="false"
import="com.adobe.granite.ui.components.ComponentHelper,
com.adobe.granite.ui.components.Config,
org.apache.sling.api.resource.Resource,
org.apache.sling.api.resource.ValueMap,
com.adobe.granite.ui.components.rendercondition.RenderCondition,
com.adobe.granite.ui.components.rendercondition.SimpleRenderCondition,
javax.jcr.Node,
com.day.cq.dam.commons.util.UIHelper,
com.day.cq.dam.api.DamConstants" %>
<%@ page import="javax.jcr.Session" %>
<%@ page import="javax.jcr.query.QueryManager" %>
<%@ page import="javax.jcr.query.Query" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="javax.jcr.NodeIterator" %>
<%@taglib prefix="sling" uri="http://sling.apache.org/taglibs/sling/1.2" %>
<%@taglib prefix="cq" uri="http://www.day.com/taglibs/cq/1.0" %>




<%

ComponentHelper cmp = new ComponentHelper(pageContext);
Config cfg = cmp.getConfig();
String path = cmp.getExpressionHelper().getString(cfg.get("path", String.class));
Resource contentRes = null;

if (path != null) {
contentRes = slingRequest.getResourceResolver().getResource(path);
} else {
contentRes = UIHelper.getCurrentSuffixResource(slingRequest);
}

if (contentRes == null) {
return;
}

Iterator itr = contentRes.listChildren();
StringBuilder strBuilder = new StringBuilder();

strBuilder.append("//element(*, nt:unstructured)[");

while(itr.hasNext()){
strBuilder.append("@sling:resource = '").append(itr.next().getPath()).append("' or");
}

String queryStmt = strBuilder.toString();

queryStmt = queryStmt.substring(0, queryStmt.lastIndexOf("or")) + "]";

Session session = resourceResolver.adaptTo(Session.class);
QueryManager qm = session.getWorkspace().getQueryManager();

Query query = qm.createQuery(queryStmt, Query.XPATH);

NodeIterator results = query.execute().getNodes();

boolean showMove = results.hasNext();

if(showMove){
%>

<%
}else{
request.setAttribute(RenderCondition.class.getName(), new SimpleRenderCondition(false));
}
%>

Read Full Blog

AEM 6550 - Move Render Condition for Assets with References

Q&A

Please use this thread to ask the related questions.

AEM AEMEBlogSeeding Experience Manager