[SOLVED] How to set configuration on DataType in c...
# help-with-umbraco
l
The answer is probably simple, but I can't find a way to correctly update some code from Umbraco 13 into the 15 equivalent. I have the following code in Umbraco 13 (redacted for easier reading):
Copy code
csharp
var configuration = new SliderConfiguration
{
    EnableRange = false,
    MinimumValue = new decimal(0),
    MaximumValue = new decimal(1),
}

var editorAlias = Umbraco.Cms.Core.Constants.PropertyEditors.Aliases.Slider;

//How to get the editor from the alias is not important
//serializer is an injected IConfigurationEditorJsonSerializer
var newType = new DataType(editor, serializer)
{
    DatabaseType = _databaseType ?? ValueStorageType.Ntext,
    CreateDate = DateTime.Now,
    CreatorId = -1,
    Name = _dataTypeName,
    Configuration = configuration
};
In this case, Configuration is an object?. And Umbraco has models (like the SliderConfiguration) for the configuration of the editor associated with the datatype. However, things change a little (but enough) in Umbraco 15: Configuration is now ConfigurationData and no longer a object?, but a Dictionary. Problem 1: I assume that the dictionary is a collection where the string is a property name and the object it's value. However, I'm not sure because I can't find any documentation on it. Is that's the case, what do I need to use as the keys? I mean, the SliderConfiguration class has some annotations, do I need to use these as key?
Copy code
csharp
public class SliderConfiguration
{
    [ConfigurationField("enableRange")]
    public bool EnableRange { get; set; }

    [ConfigurationField("minVal")]
    public decimal MinimumValue { get; set; }

    [ConfigurationField("maxVal")]
    public decimal MaximumValue { get; set; }
}
Problem 2 is that I have a perfectly good strong typed model. I don't want to create a dictionary myself and trying to figure out what keys to use. I think there should be some converter/parser that could create the dictionary of my model. Any thoughts?
s
I think the keyword you should be googling for is Property Editor (admittedly, I didn't read the post 😅).
l
Not sure how to that would help (yet)
s
Just "how to create custom property editor Umbraco 15" (a property editor is a datatype, with configured settings)
okay that doesn't make sense, that's just our built-in slider, don't mind me
l
I want to create a new DataType in code, not a property editor. I 'm using existing property editors 🙂
s
not at a computer right now! 😅
property editors are data types with a configuration
when you say "in code" - you are thinking C#, but in Umbraco 14+ it's a web component
good starting point with
dotnet new umbraco-extension
to start with the boilerplate stuff is here https://kjac.dev/posts/rebuilding-a-package-for-umbraco-15/ instead of making whatever Ken is making, you can move to https://docs.umbraco.com/umbraco-cms/tutorials/creating-a-property-editor
l
I appreciate you helping even if you are not at a computer 😉 But I'm getting confused and I think you mix some things up (or concept really have changed in the mean time). A DataType is just an implementation of a Property editor with configuration, you said it was the other way around. I created plenty of property editors, I don't need to create one now. I want to take an existing property editor (in my example the slider, which is shipped with Umbraco) and create a datatype (an instance so to speak) with a value between 0 and 1. So as far as I know, I don't need to create a property editor at all. Besides that, in Umbraco 15, the IDataTypeService is very much still present and all my existing code seems to be supported in Umbraco 15, except the way to set the configuration on the data type. So it would be weird if the IDataTypeService wouldn't do anything anymore...
s
I should not have replied 😂 Got it all wrong! 🙈 I don't really know how to answer actually, an existing editor.. so you want to create a new Datatype, based on an existing Property Editor from code, is that the actual question?
l
Almost all code to do this seems to be the same between 13 and 15 except setting the properties (like the minimum value and maximum value etc)
s
Got it!! I actually have no idea, but I'd love to find out. If nobody beats me to it then I'll have a look tomorrow.
l
Thanks! I'd rather have that you have a good weekend and take a look on monday after the weekend 😉
s
You nerdswiped me... can't give a developer a puzzle, because.. we NEED to solve it 😂
l
@Sebastiaan I figured it out. You need to call the FromConfigurationObject on the ConfigurationEditor of the editor itself (CoPilot was actually usefull for once :P):
Copy code
csharp
IConfigurationEditorJsonSerializer serializer; //Injected
PropertyEditorCollection propertyEditorCollection; //Injected

var editorAlias = Umbraco.Cms.Core.Constants.PropertyEditors.Aliases.Slider;

if (!propertyEditorCollection.TryGet(editorAlias , out dataEditor))
  return;

var configuration = new SliderConfiguration
{

    EnableRange = false,
    MinimumValue = new decimal(0),
    MaximumValue = new decimal(1),
}

var configurationDictionary = editor?.GetConfigurationEditor().FromConfigurationObject(_configuration, serializer);

var newType = new DataType(dataEditor, serializer)
{
    DatabaseType = _databaseType ?? ValueStorageType.Ntext,
    CreateDate = DateTime.Now,
    CreatorId = -1,
    Name = _dataTypeName,
};

//Because configurationDictionary is nullable, I handle it seperately, because the configurationData parameter on the constructor does not allow nullables.
if (configurationDictionary != null)
  newType.ConfigurationData = configurationDictionary;
s
Awesome! I have been thinking about it but my weekend got busy, so I'm glad you found a good way! 🙌
44 Views