Problem:
AEM will generate page name depend on the page title when the author click new page button to create a page without specified the name of the page. However sometimes the generated page name is not perfect. E.g. if title is “Is this a title with & and more space” then the generated name will be “is-this-a-title-with—and-more—space”. There are more dashes in the name which I don’t like.
Solution:
1. UI changes: customize the default save behavior of the creating page dialog.
There are 2 solutions on UI changes.
Solution 1 steps: (This the one I used)
1) create folder (nt:folder) /apps/createpagecustomprop
2) Create clientlib (type cq:ClientLibraryFolder) with categories set to “cq.widgets”: /apps/mytest/clientlib
3) Add custom-title.js to /apps/mytest/clientlib/js.txt,
4) Create file custom-title.js /apps/createpagecustomprop/clientlib/custom-title.js (can use /libs/cq/ui/widgets/source/widgets/wcm/Page.Actions.js as refrence)
(function(){
//the original create page dialog fn
var cqCreatePageDialog = CQ.wcm.Page.getCreatePageDialog;
//override function
CQ.wcm.Page.getCreatePageDialog = function(parentPath){
//create dialog by executing the product function
var dialog = cqCreatePageDialog(parentPath);
dialog.params.cmd = “customizedPageCommand”;
var cmdField = dialog.formPanel.findBy(function(comp){
return comp[“name”] == “cmd”;
}, dialog.formPanel);
cmdField[0].setValue(“customizedPageCommand”);
return dialog;
}
})();
Solution 2 steps:
This solution uses the overlay architecture of CQ to extent create page dialog(CQ.wcm.Page.getCreatePageDialog) via overriding /libs/cq/ui/widgets/source/widgets/wcm/Page.Actions.js. However this is not a preferred way to do, because of the scripts won’t be minified.
1) The source of page create dialog is file /libs/cq/ui/widgets/source/widgets/wcm/Page.Actions.js. So, to overlay the actions available in this file, create file /apps/cq/ui/widgets/source/widgets/wcm/Page.Actions.js
$.getScript(“/libs/cq/ui/widgets/source/widgets/wcm/Page.Actions.js”, function(){
var cqCreatePageDialog = CQ.wcm.Page.getCreatePageDialog;
//override function
CQ.wcm.Page.getCreatePageDialog = function(parentPath){
//create dialog by executing the product function
var dialog = cqCreatePageDialog(parentPath);
dialog.params.cmd = “customizedPageCommand”;
var cmdField = dialog.formPanel.findBy(function(comp){
return comp[“name”] == “cmd”;
}, dialog.formPanel);
cmdField[0].setValue(“customizedPageCommand”);
return dialog;
}
});
2. Create customize command to support the new create page
Create java class CustomizedPageCommand implementing WCMCommand and returning the custom action name in getCommandName() which is executed this command by /bin/wcmcommand servlet if a request is sent with the cmd param. And the return value of getCommandName() is used in the UI changes(javascript changes(in my case, it is “customizedPageCommand”).
import com.day.cq.commons.servlets.HtmlStatusResponseHelper;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.commands.WCMCommand;
import com.day.cq.wcm.api.commands.WCMCommandContext;
import org.apache.commons.collections.ListUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.HtmlResponse;
import javax.jcr.Node;
import javax.jcr.Session;
import java.util.Arrays;
import java.util.List;
@Component(immediate = true, metatype = true, inherit = true)
@Service(value = WCMCommand.class)
public class CustomizedPageCommand implements WCMCommand {
@Override
public HtmlResponse performCommand(WCMCommandContext wcmCommandContext, SlingHttpServletRequest slingHttpServletRequest, SlingHttpServletResponse slingHttpServletResponse, PageManager pageManager) {
HtmlResponse resp = null;
try {
String parentPath = slingHttpServletRequest.getParameter(PARENT_PATH_PARAM);
String template = slingHttpServletRequest.getParameter(TEMPLATE_PARAM);
String pageTitle = slingHttpServletRequest.getParameter(PAGE_TITLE_PARAM);
String pageLabel = slingHttpServletRequest.getParameter(PAGE_LABEL_PARAM);
if (StringUtils.isBlank(pageLabel)) {
String regex = “([^a-zA-Z0-9′]+)’*\\1*”;
String[] split = pageTitle.split(regex);
List<String> titles = Arrays.asList(split);
pageLabel = String.join(“-“, titles);
}
Page page = pageManager.create(parentPath, pageLabel, template, pageTitle);
Session session = slingHttpServletRequest.getResourceResolver().adaptTo(Session.class);
session.save();
resp = HtmlStatusResponseHelper.createStatusResponse(true, “pages iscreated”, page.getPath());
} catch (Exception e) {
resp = HtmlStatusResponseHelper.createStatusResponse(false, e.getMessage());
}
return resp;
}
@Override
public String getCommandName() {
return “customizedPageCommand”;
}
}
Conclusion:
By doing this way we also can customize elements in creating page dialog. For the 2 UI change solutions I personally prefer solution 1