Event API in Plugins - Changing content before it's saved

I'm working with the In-Process Event API, hooking via ContentBeforeCreateEventHandler / ContentBeforeUpdateEventHandler.

This is triggering fine & I'm hitting my breakpoint in the C# code. I can see the contents of the fields via GetReviewableText, so that's working nicely.

The bit I'm stuck on is how to makes changes to the content before it's actually saved?

Here's a basic class showing what I have so far..

namespace Blah.Blah
{
    public class ContentRewrite : IPlugin
    {
        public string Name => "Test";

        public string Description => "Test";

        public void Initialize()
        {
            Apis.Get<IContents>().Events.BeforeCreate += new ContentBeforeCreateEventHandler(this.Events_BeforeCreate);
            Apis.Get<IContents>().Events.BeforeUpdate += new ContentBeforeUpdateEventHandler(this.Events_BeforeUpdate);
        }

        private void Events_BeforeCreate(ContentBeforeCreateEventArgs args)
        {
        }

        private void Events_BeforeUpdate(ContentBeforeUpdateEventArgs args)
        {
            args.GetReviewableText().Add("Body", "This doesn't do anything");
        }
    }
}
 

The GetReviewableText() method doesn't have much that I can see re. making changes to the submitted data, maybe apart from using .Add, but I tried that and it doesn't affect anything.

Am I missing something?



clarifying the title
[edited by: Matt at 2:02 PM (GMT 0) on Tue, Jan 12 2021]
Parents Reply Children
  • Ohh, ok. The documentation here says "BeforeCreate event provides editable properties in event arguments to support adjusting the data associated with the entity before it is created", but doesn't say what events it applies to, so you end up assuming it's all BeforeCreate events.

    I've got that working with the Html events now Slight smile

    We could really do with some in-depth examples to help with this kind of thing, both from a plugin or Automation Rule point-of-view. The ones in the current documentation aren't of much help.

  • Here is a simple automation that will add ' Edited' after each HTML property on any content created or edited:

    ProcessTextInHtmlExample-Automation.xml
    <automation name="Process Text in HTML Example" version="11.0.0.0" description="" id="f4d47054148f484aab0b27d64ae1ed75" executeAsServiceUser="true" isSingleton="false" trigger="Event" lastModified="2021-01-13 15:10:22Z">
    	<events>
    		<event key="html.beforecreate" />
    		<event key="html.beforeupdate" />
    	</events>
    	<executionScript language="JavaScript"><![CDATA[var properties = context_v2_automationTrigger.Arguments.Properties;
    properties.PropertyNames.forEach(function(propertyName) {
       if (properties.IsRendered(propertyName)) {
           // this one can support embed codes if that's important
       } 
       
       // append ' Edited' after each HTML supporting field:
       properties[propertyName] = properties[propertyName] + ' Edited';
    });]]></executionScript>
    </automation>

  • BeforeCreate event provides editable properties in event arguments to support adjusting the data associated with the entity before it is created

    This is almost always true. The content events are special cases.

  • Smiley Thanks for the example (very closely matches the C# I coded this morning and works great.

    And yeah, special cases could do with highlighting in the docs.

    Thanks for your help on this.. it's got me back on the road.