What globel event is triggered on saving the 'Form' tab of a custom page type?

Alex Jones asked on October 16, 2024 02:44

The ask: we have a custom page type with a checkbox we'd like to synchronize across the different language versions of a page.

e.g. Admin user goes to page type in admin tree view, checks the box on the English page, we'd like to synchronize that selection across the other languages as well.

I thought our best answer would be to capture the checkbox being updated via global event handlers and to process the checkbox for the other language variants at the same time. I feel like this should still be feasible but I'm having trouble capturing any events on update of the checkbox on the 'Form' tab.

Steps I've taken: I followed (https://docs.kentico.com/k11/custom-development/handling-global-events/handling-object-events) to make a module that I can confirm the OnInit is hit (via breakpoint running locally) and I have confirmed that other events fire on page load (ObjectEvents.Update.Before) but when checking the 'Debug' tool with 'Event Handlers' turned on I don't see any interaction when saving the given page with the 'Form' tab visible in the 'Page' tree view. I expected the DocumentEvents.Update.Before event to trigger on save but it doesn't seem to.

Any advice on process or guidance on how to properly trigger an event would be appreciated. Perhaps additionally if there's a simple code behind way to grab the other language versions of a page for future reference that would also be appreciated.

Class created (the 'data' lines are just to give something for a breakpoint to hit as a proof of concept):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CMS;
using CMS.DataEngine;
using CMS.DocumentEngine;
using CMS.Helpers;
using CMSApp.Helpers;

// Registers the custom module into the system
[assembly: RegisterModule(typeof(LanguageToggleSyncModule))]

namespace CMSApp.Helpers
{
    public class LanguageToggleSyncModule : Module
    {

        // Module class constructor, the system registers the module under the name "LanguageToggleSyncInit"
        public LanguageToggleSyncModule() : base("LanguageToggleSyncInit")
        {
        }

        // Contains initialization code that is executed when the application starts
        protected override void OnInit()
        {
            base.OnInit();

            // Assigns custom handlers to events
            DocumentEvents.Update.Before += LanguageToggleSyncBeforeUpdateHandler;

            DocumentEvents.Update.After += Update_After;
            DocumentEvents.GetData.After += GetData_After;
            ObjectEvents.Update.Before += Update_Before;
        }

        private void Update_Before(object sender, ObjectEventArgs e)
        {
            var data = 1;
        }

        private void GetData_After(object sender, DocumentDataEventArgs e)
        {
            var data = 1;
        }

        private void Update_After(object sender, DocumentEventArgs e)
        {
            var data = 1;
        }

        // ensure the "Show Language Property" is synced across both english and spanish version for "Agency Office" page types before saving them
        private void LanguageToggleSyncBeforeUpdateHandler(object sender, DocumentEventArgs e)
        {
            if(e.Node!= null)
            {
                var docClassName = e.Node.NodeClassName;
                //confirm working with an agency office page type being saved
                if (docClassName == "custom.AgencyOffice")
                {
                    var agencyOffice = e;
                }
            }

        }
    }
}

Recent Answers


Juraj Ondrus answered on October 16, 2024 09:28

That should be the right event to use - reference. I just tested it using K11 and it works well. If you are using workflows, then you should be using the workflow events too. Alsom, make sure you are using latest hotfix.

0 votesVote for this answer Mark as a Correct answer

OHI Accounting answered on October 23, 2024 10:04

To achieve synchronization of the checkbox across different language versions of a custom page type in Kentico, you are on the right track by using global event handlers. However, it appears you may not be capturing the right event when the checkbox is updated in the 'Form' tab.

Key Considerations: DocumentEvents vs. ObjectEvents: The DocumentEvents.Update.Before event should generally capture updates to documents (including changes made through the Form tab). However, ensure that the checkbox is tied to the document's data and is being recognized as an update to the document.

Debugging Event Triggers: Since you confirmed that ObjectEvents.Update.Before is firing but DocumentEvents.Update.Before is not, ensure that the event is correctly associated with the document being edited. Sometimes, the user interface may trigger a different update than expected.

Form Tab Interactions: If your checkbox is part of a custom form, make sure that it is properly bound to the document properties. Sometimes, issues arise if the property isn't configured correctly, leading to the event not firing.

Steps to Troubleshoot: Check the Checkbox Binding: Ensure that the checkbox is mapped to a property of the document in the form configuration. This is essential for triggering the DocumentEvents.Update events.

Utilize Debugging: Confirm that the LanguageToggleSyncBeforeUpdateHandler is indeed receiving the expected document object. You can place breakpoints or log outputs to verify the document type and its properties during the event.

Review Module Registration: Ensure that your module is correctly registered and that the event handlers are indeed being invoked at the right point in the document lifecycle.

Synchronizing Language Versions: To synchronize the checkbox across language versions, you can fetch the related document versions based on the current document's culture. Here’s a snippet to get you started:

csharp Copy code private void LanguageToggleSyncBeforeUpdateHandler(object sender, DocumentEventArgs e) { if (e.Node != null && e.Node.NodeClassName == "custom.AgencyOffice") { // Get the current checkbox value bool checkboxValue = e.Node.GetBooleanValue("YourCheckboxFieldName");

    // Get all language versions of the document
    var relatedDocuments = DocumentHelper.GetDocuments()
        .WhereEquals("NodeClassName", "custom.AgencyOffice")
        .WhereEquals("NodeParentID", e.Node.NodeParentID)
        .ToList();

    foreach (var relatedDocument in relatedDocuments)
    {
        if (relatedDocument.NodeID != e.Node.NodeID)
        {
            // Synchronize the checkbox value
            relatedDocument.SetValue("YourCheckboxFieldName", checkboxValue);
            relatedDocument.Update();
        }
    }
}

} Summary: Ensure your checkbox is properly bound to the document properties. Confirm that the right events are firing during the update process. Implement the synchronization logic as outlined above to update related language versions. By refining your approach based on these points, you should be able to achieve the desired synchronization across your custom page type's language versions.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.