March 14, 2020
Estimated Post Reading Time ~

Customization of parsys to restrict number of components in AEM

1. Copy the parsys component from(/libs/foundation/components/parsys) to your poroject(/apps/<project nmae>/components/).

2. In the design, dialog adds number filed configuration to enter the number of components.

Crate node noofcomp of type cq:widget under /apps/<project nmae>/components/parsys
/design_dialog/items.
Add the following properties

xtype : numberfield
name : noOfComp
maxValue : 20
fieldLabel : No of components

3. Create ajax.jsp and write the below logic.
<%@page import="com.day.cq.wcm.foundation.ParagraphSystem"%>
<%@include file="/libs/foundation/global.jsp"%>
<%
ParagraphSystem parSys = ParagraphSystem.create(resource, slingRequest);
int totalComponents=parSys.paragraphs().size();
int restrictCompoNo=Integer.parseInt(currentStyle.get("noOfComp","20"));

if(totalComponents >= restrictCompoNo){
out.println("true");
}else{
out.println("false");
}
%>

4. You need to overlay the EditBase.js and Sidekick.js
Create the following structure /apps/cq/ui/widgets/source/widgets/wcm

then copy the EditBase.js and Sidekick.js form libs . Place under /wcm /

In Sidekic.js at line 4098 in the set timeout method add this snippet.

var parentPath=editComponent.path;
var ajaxUrl=parentPath.substr(0,parentPath.lastIndexOf("/"))+".ajax";
var notAllowToCreate = CQ.HTTP.eval(ajaxUrl);

if(!notAllowToCreate){
editComponent.createParagraph(definition);

}else{
CQ.Ext.Msg.show({
msg: 'You reached the maximum limit. You can set the limit in design mode.',
buttons: CQ.Ext.Msg.OK,
icon: CQ.Ext.MessageBox.WARNING
});

}
dropTarget.editComponent.hideTarget();

In EditBase.js at line 1181 in the set, timeout method add this snippet.

var parentPath=e.path;
var ajaxUrl=parentPath.substr(0,parentPath.lastIndexOf("/"))+".ajax";
var notAllowToCreate = CQ.HTTP.eval(ajaxUrl);
if(!notAllowToCreate){
e.createParagraph(definition);

}else{
CQ.Ext.Msg.show({
msg: 'You reached the maximum limit. You can set the limit in design mode.',
buttons: CQ.Ext.Msg.OK,
icon: CQ.Ext.MessageBox.WARNING
});

}
e.dialogs[CQ.wcm.EditBase.INSERT].hide();
e.insertDialogMask.hide();


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.