Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Dave 15 posts 88 karma points
    Apr 22, 2018 @ 17:01
    Dave
    0

    Umbraco Forms - Hook into On Save event

    Hey Guys,

    Anyone know if it's possible to hook into the On Save event in Umbraco Forms?

    I had some problems with my custom fields permanently breaking forms so it would be good if I could keep and audit log type trail of the forms.

    Cheers, Dave

  • Rune Grønkjær 1304 posts 2897 karma points
    Nov 27, 2020 @ 08:01
    Rune Grønkjær
    0

    Hi Dave,

    Did you find a way. I have searched all over the place with no result.

    /Rune

  • Lee Kelleher 3954 posts 15283 karma points MVP 11x admin c-trib
    Nov 27, 2020 @ 10:05
  • Rune Grønkjær 1304 posts 2897 karma points
    Nov 27, 2020 @ 10:17
    Rune Grønkjær
    0

    I wanted to clear a cache whenever a form is saved. I cannot find the usual service for that event.

  • Lee Kelleher 3954 posts 15283 karma points MVP 11x admin c-trib
    Nov 27, 2020 @ 10:22
    Lee Kelleher
    0

    Oops sorry, I thought you meant a Save event for a form submitted by a (frontend) user, doh!

    OK, for a saving of a Form (in the backoffice), could try this...

    using Umbraco.Core.Composing;
    using Umbraco.Forms.Core;
    using Umbraco.Forms.Core.Services;
    
    namespace Umbraco.Forms.Community
    {
        internal sealed class UmbracoFormsSavedComposer : ComponentComposer<UmbracoFormsSavedComponent>
        { }
    
        internal sealed class UmbracoFormsSavedComponent : IComponent
        {
            private readonly IFormService _formService;
    
            public UmbracoFormsSavedComponent(IFormService formService)
            {
                _formService = formService;
            }
    
            public void Initialize()
            {
                _formService.Saved += Form_Saved;
            }
    
            public void Terminate()
            {
                _formService.Saved -= Form_Saved;
            }
    
            private void Form_Saved(object sender, FormEventArgs e)
            {
                if (e.Form != null)
                {
                    // Do your thang!
                }
            }
        }
    }
    

    I haven't tested that, hopefully it'll give a good starting point. Let me know how it goes.

    Cheers,
    - Lee

  • Rune Grønkjær 1304 posts 2897 karma points
    Nov 27, 2020 @ 10:55
    Rune Grønkjær
    0

    Thankyou for the great example. That should work in theory but weirdly enough none of the events I try is actually called.

    I have tried these: formService.Saved += FormServiceSaved; formService.Saving += FormServiceSaving; formService.Updated += FormServiceUpdated; formService.Deleted += FormServiceDeleted;

    This code is run in my CacheComponent and the formService is not null. So it should work. But the events is not being fired.

    That is weird. Not sure if it's a UmbracoForms bug or if the events is not hooked up to changing the forms.

    Just for good measure I tried posting a form as well, which of course did nothing.

    /Rune

  • Lee Kelleher 3954 posts 15283 karma points MVP 11x admin c-trib
    Nov 27, 2020 @ 12:12
    Lee Kelleher
    101

    Hi Rune,

    I've had a quick play around with it, (whilst watching umbraCoffee!), and it looks like FormStorage is still being used, instead of FormService.

    Even though the IFormStorage.Saved events are marked as deprecated, along with a message to use IFormService.Saved instead.

    Here's my code snippet, which did fire when I saved an existing form.

    internal sealed class UmbracoFormsSavedComponent : IComponent
    {
        private readonly IFormStorage _formStorage;
    
        public UmbracoFormsSavedComponent(IFormStorage formStorage)
        {
            _formStorage = formStorage;
        }
    
        public void Initialize()
        {
            _formStorage.Saved += FormStorage_Saved;
        }
    
        public void Terminate()
        {
            _formStorage.Saved -= FormStorage_Saved;
        }
    
        private void FormStorage_Saved(object sender, Umbraco.Forms.Core.FormEventArgs e)
        {
            if (e.Form != null)
            {
                // Do your thang!
            }
        }
    }
    

    Hope this helps.

    Cheers,
    - Lee

  • Rune Grønkjær 1304 posts 2897 karma points
    Nov 30, 2020 @ 10:51
    Rune Grønkjær
    0

    Ah yes. Brilliant. We have to use the deprecated ones instead.

    They work as supposed to though.

    I have chosen to use the to side by side. Could be that the service works in some other scenario.

    Thanks for the help!

    /Rune

Please Sign in or register to post replies

Write your reply to:

Draft