Before & After Save
If you want to make changes to an entity (i.e. Content, Media etc...) before it's saved or after it's saved, or when something new is added or deleted. You can use the IBeforeEntitySave plugin.
Just implement the Interface
Interfaces
/// <summary>
/// Defines a contract for handling operations that need to occur before saving an entity to the database.
/// </summary>
public interface IBeforeEntitySave<TEntity>
{
/// <summary>
/// Executes operations needed before saving an entity to the database.
/// </summary>
/// <param name="entity">The entity instance that is about to be saved.</param>
/// <param name="entityState">The state of the entity within the context (e.g., Added, Modified, Deleted).</param>
/// <returns>Returns false if the save operation should be canceled; otherwise, true.</returns>
bool BeforeSave(TEntity entity, EntityState entityState);
/// <summary>
/// Gets or sets the order in which operations or processes should be executed or entities should be managed.
/// </summary>
/// <remarks>
/// This property is typically used to indicate the priority or position of an entity or operation
/// in contexts such as sorting, processing, or UI display.
/// </remarks>
int SortOrder { get; }
}Usage
And then you can check the entity state or just just update the entity before it's saved. Very simple example below showing the save being abandoned
Or an example showing some content being replaced
Advanced Example
In this example, I am creating a autogenerated blog snippet of 30 words, taken from a specific element type in a block list. I then update the snippet on the content and use it for the meta description if that is blank (The meta description is part of the SEO property editor).
Last updated