orchardcmsorchardcms-1.8

Set a default value for an Orchard Content Field


I am adding a BooleanField to a WidgetPart from a migration as follows:

ContentDefinitionManager.AlterPartDefinition("WidgetPart",
    builder => builder.WithField("DisplayInSubnav", fieldBuilder => fieldBuilder
        .OfType("BooleanField")
        .WithDisplayName("Display in Subnav")));

When it renders on the admin UI's Edit Widget page, the field's default value is null. How can we set the BooleanField's default to true?

I have tried to set the default from the WidgetPartHandler to no avail.

public class WidgetPartHandler : ContentHandler
{
    public WidgetPartHandler(IRepository<WidgetPartRecord> widgetsRepository) {

        OnInitializing<WidgetPart>((context, part) => 
            (part as dynamic).ShowInSubnav.Value = true);

        OnLoading<WidgetPart>((context, part) => 
            (part as dynamic).ShowInSubnav.Value = true);
    }
}

Solution

  • The default value is a setting of the field. Add .WithSetting("BooleanFieldSettings.DefaultValue", "true") to the field builder in the migration.