Loading

Adding confirmation to page actions of administration interface

This post shows a few more things how Kentico global event handling can be extended to add more functionality to Kentico admin interface without need to modify Kentico built-in controls.

Let's imagine the situation that you need to show some javascript confirmation box to Kentico editor when he saves the data he has entered on the Form or Page tab of Kentico administration. You may ask what the reason is to show confirmation dialog when save the form? There might be a lot of tricky tasks and requirements that will require to do this. For example, you have a lot of documents on the site that are linked to different places under content tree. Editor of one site decided to modify content in some linked article. So you decided just to remind the Editor that he saves the linked document and if he don't want the changes influence all the linked documents the system will simply create a copy of this document. Another thing that might happen - you want to disable some header action (for example for some complicated workflow logic).

To implement such requirements you can find the file of the header action control in the Kentico project and add your logic to the code of that control. But as we may know modifying Kentico build-in controls is not really good practice as after upgrade to newer Kentico version your code might be rewritten and you will need to add this logic again. Do you agree? :)

So Kentico event handling is become handy here. More information about Kentico global event hanling might be found here: Handling global events. Code sample below demonstrate how to add javascript confirmation dialog on 'Save' action. You can access all page header action buttons and their properties and modify them accordingly. And it is all about subscription to OnAfterUserControlPreRender event and handling this event appropriately. You may add any required logic to that method according to the requirements you have. 

using System;
using System.Collections.Generic;
using CMS.Base;
using CMS.Controls;
using CMS.DocumentEngine;
using CMS.ExtendedControls;
using CMS.ExtendedControls.ActionsConfig;
using CMS.PortalEngine;
using CMS.SiteProvider;

[CustomizationLoader]
public partial class CMSModuleLoader
{
    /// <summary>
    /// Custom attribute class for the CMSModuleLoader.
    /// </summary>
    private class CustomizationLoaderAttribute : CMSLoaderAttribute
    {
        /// <summary>
        /// Called automatically when the application starts.
        /// </summary>
        public override void Init()
        {
            //Assigns handler to display popup message
            EditMenu.OnAfterUserControlPreRender= EditMenu_OnAfterUserControlPreRender;
        }

        private void EditMenu_OnAfterUserControlPreRender(object sender, EventArgs e)
        {
            HeaderActions headerActions = sender as HeaderActions;

            if (headerActions != null && headerActions.ActionsList.Count > 0)
            {
                if (PortalContext.ViewMode == ViewModeEnum.EditForm || PortalContext.ViewMode == ViewModeEnum.EditLive || PortalContext.ViewMode == ViewModeEnum.Edit)
                {
                    List<HeaderAction> saveActionsList = headerActions.ActionsList.FindAll(p => p.EventName == "Save");
                    foreach (HeaderAction action in saveActionsList)
                    {
                        action.OnClientClick += "return confirm(\"Do you want to save the document?\");";
                    }
                }
            }
        }
    }
}
 

If you find this post usefull please leave your comments below. Thanks in advance, Mariia.