JustNik

How to add default values to Umbraco properties in the backoffice in v8

Umbracologoblue05

Back in September 2018 Paul Seal, of CodeShare.co.uk, wrote a brilliant guide for hooking into events in Umbraco to pre-load default values into properties. Since this post was written Umbraco launched version 8 and with it a few things have changed. With Paul's permission I'm writing this post to help show you how to do the same thing in Umbraco v8.

In Umbraco v8 the code base changed and the ApplicationEventHandler class was replaced with the concept of Components and Compositions. As a result the way in which we need to bind to the SendingContentModel has changed. On top of this, v8 introduced the concept of variants for different languages which means we need to consider this for setting the default value.

The setup

First up the code for the Component element, this is where the event is wired up and the default value is set.

public class EditorModelComponent : IComponent
{
    private read only ILogger logService;

    public EditorModelComponent(ILogger logService)
    {
        this.logService = logService;
    }

    public void Initialize()
    {
        EditorModelEventManager.SendingContentModel += 
            EditorModelEventManager_SendingContentModel;
    }

    public void Terminate() { return; }

    private void EditorModelEventManage_SendingContentModel( 
            HttpActionExecutedContext sender, 
            EditorModelEventArgs<ContentItemDisplay> e)
    {
        SetDefaultValue(e, new string[] { BlogPost.ModelTypeAlias }, 
                "postDate", DateTime.Now.AddDays(1).Date);
    }

    private void SetDefaultValue(EditorModelEventArgs<ContentItemDisplay> e, 
                string[] docTypeAliases, string propertyAlias, object newValue)
    {
        try
        {
            if(docTypeAliases.Contains(e.Model.ContentTypeAlias))
            {
                //Here we need to consider variants
                foreach(var variant in e.Model.Variants)
                {
                    var props = variant.Tabs.SelectMany( t =>
                            t.Properties.Where(p =>
                                p.Alias.Equals(propertyAlias) &&
                                (p.Value == null || string.IsNullOrWhiteSpace(p.Value.ToString()))));
                    foreach(var prop in props)
                        prop.Value = newValue;
                }
            }
        }
        catch (Exception e)
        {
            logService.Error(GetType(), ex, "Error occurred setting default value");
        }
    }
}

Next we need to tell Umbraco about the component, and in order to do this we use a Composer.

public class EditorModelComposer : IUserComposer
{
    public void Compose(Composition composition)
    {
        composition.Components().Append(EditorModelComponent);
    }
}

And there you have it, with these two blocks of code it would be possible to set default values on your content types.

Thanks goes to Paul from CodeShare.co.uk for his original post on doing this in Umbraco v7.

If you want to know more about the Component and Composer elements of this blog post the official Umbraco documentation is your best starting point.

Additional information

In order to help with the use of this code the following are the using statements that are required.

using System;
using System.Linq;
using Umbraco.Core;
using Umbraco.Core.Composing;
using Umbraco.Web.Editors;
using Umbraco.Web.Models.ContentEditing;

//If using Models Builder generated models from the default namespace
using Umbraco.Web.PublishedModels;
JustNik
Connect with me
Twitter GitHub LinkedIn
© JustNik 2024