Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

Extending Quick Publish Functionality for Selected User Groups with Email Notification

Avatar

Level 3
  1. I have a requirement to extend the functionality of the Quick Publish button. Currently, when a user clicks it, the page and its references are published, and a version of the page is created.

    I want to customize this functionality so that after the version is created, an email is sent to specific people in a particular user group.

  2. Additionally, this new functionality should apply only to a few selected user groups.

    I am considering two options and would appreciate any guidance:

    1. Is there a way to extend the functionality of the existing Quick Publish button to achieve this?
    2. Alternatively, can I create a new button and implement the complete functionality from scratch, applying it only to the selected user groups?

    @arunpatidar  
1 Accepted Solution

Avatar

Correct answer by
Community Advisor
3 Replies

Avatar

Correct answer by
Community Advisor

Avatar

Community Advisor

Hi @SRC_AEM_DEV 
You can create a new button and hide the OOTB for the users who should receive the email. You should reverse the visibility for remaining users.

Please write a logic to include references as well.

 

OOTB quick publish logic example :

var REPLICATE_URL = Granite.HTTP.externalize("/bin/replicate");
    var QUICKPUBLISH_TITLE = Granite.I18n.get("Quick Publish");
    var PUBLISH_TEXT = Granite.I18n.get("Publish");
    var CANCEL_TEXT = Granite.I18n.get("Cancel");
    var ERROR_TEXT = Granite.I18n.get("Error");
    var DEFAULT_REPLICATION_AGENT_ID = "publish";
    $(window).adaptTo("foundation-registry").register("foundation.collection.action.action", {
        name: "cq.wcm.quickpublish",
        handler: function(name, el, config, collection, selections) {
            var message = createEl("div");
            var intro = createEl("p").appendTo(message);
            if (selections.length === 1)
                intro.text(Granite.I18n.get("The page and their references will be published."));
            else
                intro.text(Granite.I18n.get("The {0} pages and their references will be published.", selections.length));
            var ui = $(window).adaptTo("foundation-ui");
            ui.prompt(QUICKPUBLISH_TITLE, message.html(), "notice", [{
                text: CANCEL_TEXT
            }, {
                text: PUBLISH_TEXT,
                primary: true,
                handler: function() {
                    activatePagesAndItsReferences(config, collection, selections)
                }
            }])
        }
    })
}
)(window, document, Granite.$, Granite);
(function(window, document, $, URITemplate) {
    var replicateURL = Granite.HTTP.externalize("/bin/replicate");
    var successMessage = Granite.I18n.get("The item has been published");
    var DEFAULT_REPLICATION_AGENT_ID = "publish";
    $(window).adaptTo("foundation-registry").register("foundation.collection.action.action", {
        name: "cq.wcm.publish",
        handler: function(name, el, config, collection, selections) {
            var ui = $(window).adaptTo("foundation-ui");
            ui.wait();
            var paths = selections.map(function(v) {
                var item = $(v);
                var refPath = item.data("checkReferencesPath");
                if (!refPath)
                    refPath = item.children().data("checkReferencesPath");
                if (refPath)
                    return refPath.split(",");
                return item.data("foundationCollectionItemId")
            });
            if (!paths.length)
                return;
            paths = [].concat.apply([], paths);
            config.data.referencesrc=config.data.referenceSrc.startsWith("/") ? config.data.referenceSrc : "/" + config.data.referenceSrc;
            var referencePromise = $.ajax({
                url: URITemplate.expand(config.data.referenceSrc, {
                    path: paths
                }),
                "type": "POST",
                cache: false,
                dataType: "json"
            });
            referencePromise.done(function(json) {
                if (json.assets.length === 0)
                    $.ajax({
                        url: replicateURL,
                        type: "POST",
                        data: {
                            _charset_: "utf-8",
                            cmd: "Activate",
                            path: paths,
                            agentId: DEFAULT_REPLICATION_AGENT_ID
                        }
                    }).always(function() {
                        ui.clearWait()
                    }).done(function() {
                        var api = $(collection).adaptTo("foundation-collection");
                        if (api && "reload"in api) {
                            api.reload();
                            ui.notify(null, successMessage);
                            return
                        }
                        var contentApi = $(".foundation-content").adaptTo("foundation-content");
                        if (contentApi)
                            contentApi.refresh();
                        ui.notify(null, successMessage)
                    }).fail(function(xhr) {
                        var title = Granite.I18n.get("Error");
                        var message = Granite.I18n.getVar($(xhr.responseText).find("#Message").html());
                        ui.alert(title, message, "error")
                    });
                else {
                    sessionStorage.setItem("document.referrer", window.location.href);
                    window.location.href = URITemplate.expand(config.data.wizardSrc, {
                        item: paths
                    })
                }
            });
            referencePromise.fail(function(xhr) {
                ui.clearWait();
                var title = Granite.I18n.get("Error");
                var message = Granite.I18n.get("Failed to retrieve references for selected items.");
                ui.alert(title, message, "error")
            })
        }
    });
    $(document).on("foundation-contentloaded", function(e) {
        var message = sessionStorage.getItem("cq-page-published-message");
        if (message !== null) {
            sessionStorage.removeItem("cq-page-published-message");
            var ui = $(window).adaptTo("foundation-ui");
            ui.notify(null, Granite.I18n.getVar(message))
        }
    })
}

Note: Above code is copied from AEMaaCS SDK.



Arun Patidar

Avatar

Administrator

@SRC_AEM_DEV Did you find the suggestions helpful? Please let us know if you require more information. Otherwise, please mark the answer as correct for posterity. If you've discovered a solution yourself, we would appreciate it if you could share it with the community. Thank you!



Kautuk Sahni