AEM 6 - Classic UI Restrict User from Creating Pages with Same Title


Extend Classic UI Create Page Dialog to restrict user from creating pages with same title in a path. Create button is disabled when user opens the dialog and enabled only when user enters a unique title

Demo | Package Install


1) Login to CRXDE Lite (http://localhost:4502/crx/de) and create folder /apps/classic-ui-no-page-duplication

2) Create node /apps/classic-ui-no-page-duplication/clientlib of type cq:ClientLibraryFolder and add a String property categories with value cq.widgets

3) Create file (nt:file) /apps/classic-ui-no-page-duplication/clientlib/js.txt and add


4) Create file (nt:file) /apps/classic-ui-no-page-duplication/clientlib/check-page-name.js and add the following code.

    //the original create page dialog fn
    var cqCreatePageDialog = CQ.wcm.Page.getCreatePageDialog;
    //override ootb function
    CQ.wcm.Page.getCreatePageDialog = function(parentPath){
        //create dialog by executing the product function
        var dialog = cqCreatePageDialog(parentPath);

            //disable create until page title gets validated
            var createButton = dialog.buttons[0];

            //make necessary UI changes to the dialog created above
            var panel = dialog.findBy(function(comp){
                return comp["jcr:primaryType"] == "cq:Panel";
            }, dialog);

            if(!panel || !panel.length){

            panel = panel[0];

            //get title field
            var titleField = panel.findBy(function(comp){
                return comp["fieldLabel"] == "Title";
            }, panel);

            if(!titleField || !titleField.length){

            titleField = titleField[0];

            titleField.on('change', function(t, nvalue){
                //when user enters title, search CRX if a title with same wording exists
                    type: "GET",
                    url: "/bin/querybuilder.json",
                    data: {
                        "path": parentPath,
                        "0_property": "jcr:title",
                        "0_property.value": nvalue
                    if(data && data.hits && data.hits.length > 0){
                        CQ.Ext.Msg.alert("Error", "Page " + nvalue + " exists in path " + parentPath);

                    //not a duplicate, enable create button
            console.log("Error executing CQ.wcm.Page.getCreatePageDialog override");

        return dialog;

No comments:

Post a Comment