4

I have created custom data type based on built-in dropdown list, but cannot figure out how to specify default value for the list. The default value is always blank:

enter image description here

Ivan Studenikin
  • 1,362
  • 4
  • 17
  • 30

2 Answers2

6

The default dropdown does not support default value

There is two way of achieving what you want

  1. create your own dropdown datatype (or use a plugin someone else has made - I am not sure which one support it, but maybe have a look at nuPickers )

  2. use a web api handler to intercept the call of getting the content value - and set a default value to your property if it is empty (null)

below is some un-tested code:

first create the web api handler

public class SetDropdownDefaultHandler : DelegatingHandler
{
    protected override async Task<HttpResponseMessage> SendAsync
            (HttpRequestMessage request, CancellationToken cancellationToken)
    {
        var response = await base.SendAsync(request, cancellationToken);

        var url = request?.RequestUri?.AbsolutePath.ToLower;

        // only process when a create (getempty) or editing a specific content (getbyid) 
        if (url == "/umbraco/backoffice/umbracoapi/content/getempty"
            || url == "/umbraco/backoffice/umbracoapi/content/getbyid")
        {
            var content = (ObjectContent)response.Content;
            var data = content?.Value as PagedResult<ContentItemBasic<ContentPropertyBasic, IContent>>;

            if (data?.Items != null)
            {
                var tempResult = data?.Items?.ToList();

                foreach (var item in tempResult)
                {
                    foreach (var prop in item?.Properties?.Where(p => p?.Editor == "Umbraco.DropDown"))
                    {
                        var propStr = prop.Value?.ToString();
                        if (!propStr.IsNullOrWhiteSpace())
                        {
                            // set your default value if it is empty
                            prop.Value = "your default option prevalue id";
                        }
                    }
                }

                data.Items = tempResult;
            }
        }

        return response;
    }
}

then register it at started event

public class UmbracoEvent : ApplicationEventHandler
{
  protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
  {
    GlobalConfiguration.Configuration.MessageHandlers.Add(new SetDropdownDefaultHandler());
  }
}

your problem maybe you don't know your prevalueid - you can look it up in db or you could use datatype service to get the datatype prevalues then decide which to put as default

Alan Tsai
  • 2,465
  • 1
  • 13
  • 16
1

Look at: FieldType.DropDownList in the fieldTypes folder.

Replace:<option value=""></option>

With:

var settings = Model.AdditionalSettings; <option value="">@settings["DefaultValue"]</option>

Then ensure you set the default value property in your dropdown list in the Umbraco Forms backoffice for the given form

enter image description here enter image description here

Damo
  • 44
  • 4