Luuk Peters (Proud Nerds)
01/21/2025, 8:20 AMcsharp
//propertyEditorCollection is an injected PropertyEditorCollection
_ = propertyEditorCollection.TryGet("Umbraco.Cms.Core.Constants.PropertyEditors.Aliases.Slider", out var propertyEditor);
Next we need to create the configuration for our new DataType:
csharp
var dataTypeConfiguration = new SliderConfiguration
{
EnableRange = false,
MinimumValue = 1,
MaximumValue = 10,
};
//configurationEditorJsonSerializer is an injected IConfigurationEditorJsonSerializer
var configurationDictionary = propertyEditor?.GetConfigurationEditor().FromConfigurationObject(_configuration, configurationEditorJsonSerializer);
And finally create the data type:
csharp
//configurationEditorJsonSerializer is an injected IConfigurationEditorJsonSerializer
var newType = new DataType(propertyEditor, configurationEditorJsonSerializer)
{
DatabaseType = ValueStorageType.Ntext,
ConfigurationData = configurationDictionary,
CreateDate = DateTime.Now,
CreatorId = Umbraco.Cms.Core.Constants.Security.SuperUserId, //Deprecation warning on SuperUserId!
Name = "Example slider"
};
var result = await dataTypeService.CreateAsync(newType, Umbraco.Cms.Core.Constants.Security.SuperUserKey);
Nice and makes sense! Except that this doesn't work and there are multiple issues with this. (See next post)
https://cdn.discordapp.com/attachments/1331176660518699062/1331176661106032701/image.png?ex=6790aa60&is=678f58e0&hm=71840613f6c02ef1e3d9eef31ae58b581ed658cec6905db712409574bd3ee59e&Luuk Peters (Proud Nerds)
01/21/2025, 8:23 AMcsharp
var newType = new DataType(propertyEditor, configurationEditorJsonSerializer)
{
DatabaseType = databaseType,
ConfigurationData = dictionary,
CreateDate = DateTime.Now,
CreatorId = -1,
Name = dataTypeName,
EditorUiAlias = "Umb.PropertyEditorUi.Slider"
};
This property is nullable and I would assume that because we give the propertyEditor in the constructor that we wouldn't need to set it, but we do. There doesn't seem to be a list of constants in Umbraco either for this, too bad. And a sidenote: CreatorId is still an integer, so if we use Umbraco.Cms.Core.Constants.Security.SuperUserId, we get all kinds of deprecation warnings...
So now we have a data type with a correctly set Property editor, but we defined the configuration of our slider on a scale of 1 to 10. This doesn't work either, all values (minumum value, maximum value, step etc) remain 0. It turns out that the SliderConfiguration model simply is not up to date. Looking at the properties that are set when saving a slider data type, it's clear that not all properties are present in that model as it has only three properties. Also the configurator (propertyEditor?.GetConfigurationEditor()) has three properties.
So when I create the property dictionary myself, I can finally get it to work:
csharp
var dictionary = new Dictionary<string, object>()
{
{"enableRange", false},
{"minimumValue", 1},
{"maximumValue", 10},
{"minVal", 1},
{"maxVal", 10},
{"initVal1", 5},
{"step", 1}
};
var newType = new DataType(propertyEditor, configurationEditorJsonSerializer)
{
DatabaseType = databaseType,
ConfigurationData = dictionary,
CreateDate = DateTime.Now,
CreatorId = -1,
Name = dataTypeName,
Editor = propertyEditor,
EditorUiAlias = "Umb.PropertyEditorUi.Slider"
};
Luuk Peters (Proud Nerds)
01/21/2025, 8:28 AMPatrick de Mooij
01/21/2025, 8:49 AM