Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Daniel Rogers 134 posts 712 karma points
    Jan 24, 2023 @ 14:43
    Daniel Rogers
    0

    error getting datType in code

    Im generating datatypes pragmatically (Quicker and easier to maintain accross multiple websites)

    using umbraco 11.1.0

    but I have an issue where I have created some custom property editors. They show up fine in the back end of the website if you create a datatype manually.

    However in generation code for my datTypes I have a line

    var editor = _dataTypeService.GetByEditorAlias(docType).FirstOrDefault();
    

    is meant to get the propertyEditor the given alias. however it returns a null there is no dataType with this alias already created.

    but If I create a dataType with a the given property editor as named anything. This code finds the property editor via the alias.

    Not I have checked and checked and checked the Aliase names match etc. not code works if there is a least 1 dataType that uses this propertyEditor alias.

    It is like you have 2 lists

    List "A" contains all the possable propertyEdits available and this is the list that we see in the backoffice when creating dataTypes manually.

    Then there is List "B" that contains al thepropertyEditors that have been used at least once.

    so list B is smaller than List A but the code above searchs list B but it should be searching list A

    full code

    public void CreateDataType(int containerId, string name, string docType, Dictionary<string, object> preValues)
            {
                var exists = _dataTypeService.GetDataType(name);
                if (exists == null)
                {
                    var editor = _dataTypeService.GetByEditorAlias(docType).FirstOrDefault();
                    if (editor != null)
                    {
    
                        DataType newDataType = new DataType(editor.Editor, _serializer, containerId);
    
                        newDataType.Name = name;
    
                        _dataTypeService.Save(ConfigureDataType(newDataType,docType,preValues));
                        int newDataTypeId = newDataType.Id;
                    }
                }
                else
                {
                    DataType created = (DataType)_dataTypeService.GetDataType(name);
    
                    _dataTypeService.Save(ConfigureDataType(created, docType, preValues));
                }
            }
    

    ConfigureDataType handles the different prevalue types.

  • Huw Reddick 1737 posts 6098 karma points MVP c-trib
    Jan 27, 2023 @ 10:43
    Huw Reddick
    0

    Hi Daniel,

    If you do _dataTypeService.GetAll() does it appear in the results?

  • Daniel Rogers 134 posts 712 karma points
    Jan 27, 2023 @ 11:21
    Daniel Rogers
    0

    Tried that. But no it doesnt come up in the list.

    temporarily got round it by passing

    _install.CreateDataType(formPickerId, "IBD Advanced Form Template Block Picker", "IBD.BlockMultinodePicker", preValues, new BlockMultinodePickerEditor(_ioHelper, _dataValueEditorFactory, _editorConfigurationParser, EditorType.PropertyValue));
    

    Editor decleared

    [DataEditor(
            alias: "IBD.BlockMultinodePicker",
            type: EditorType.PropertyValue,
            name: "Block Multinode Picker",
            view: "/umbraco/backoffice/Plugins/IBDStarterKitBackoffice/BlockMultinodePicker",
            HideLabel = false,
            ValueType = ValueTypes.Json,
            Group = "Common",
            Icon = "icon-fa-b-linode color-deep-orange")]
        public class BlockMultinodePickerEditor : DataEditor
        {
            private readonly IIOHelper _ioHelper;
            private readonly IEditorConfigurationParser _editorConfigurationParser;
    
            public BlockMultinodePickerEditor(
                IIOHelper ioHelper,
                IDataValueEditorFactory dataValueEditorFactory,
                IEditorConfigurationParser editorConfigurationParser,
                EditorType type = EditorType.PropertyValue)
                : base(dataValueEditorFactory, type)
            {
                _ioHelper = ioHelper;
                _editorConfigurationParser = editorConfigurationParser;
            }
            protected override IConfigurationEditor CreateConfigurationEditor() => new BlockMultinodePickerConfigurationEditor(_ioHelper, _editorConfigurationParser);
        }
    

    then inside routine did this

        public void CreateDataType(int containerId, string name, string docType, Dictionary<string, object> preValues, IDataEditor newEditor)
        {
            var exists = _dataTypeService.GetDataType(name);
            if (exists == null)
            {
                DataType newDataType = newEditor == null ? new DataType(_dataTypeService.GetByEditorAlias(docType).FirstOrDefault().Editor, _serializer, containerId) : new DataType(editor: newEditor, _serializer, containerId);
    
                newDataType.Name = name;
    
                _dataTypeService.Save(ConfigureDataType(newDataType,docType,preValues));
                int newDataTypeId = newDataType.Id;
            }
            else
            {
                DataType created = (DataType)_dataTypeService.GetDataType(name);
    
                _dataTypeService.Save(ConfigureDataType(created, docType, preValues));
            }
        }
    

    line in question is from above

    DataType newDataType = newEditor == null ? new DataType(_dataTypeService.GetByEditorAlias(docType).FirstOrDefault().Editor, _serializer, containerId) : new DataType(editor: newEditor, _serializer, containerId);
    

    this replaced the code in the question above.

    works from the perspective that the dataTypes that are not working are my new ones that have never been used therefore it i pass the into newEditor the IDataEditor it uses that and it works and if its a standard umbraco editor or another editor that I havent found the model for and pass it as a null it works as long as there is a property editor thathas been already created.

    in looking into it deep I established that

    var editor = _dataTypeService.GetByEditorAlias(docType).FirstOrDefault();
    

    finds the first Data Type that exists and using that to create the new one. Its not finding the Property Editor by that alias.

    with the work around both these work

            _install.CreateDataType(formPickerId, "IBD Advanced Form Block Settings", "Umbraco.BlockList", preValues, null);
    
            _install.CreateDataType(formPickerId, "IBD Advanced Form Template Block Picker", "IBD.BlockMultinodePicker", preValues, new BlockMultinodePickerEditor(_ioHelper, _dataValueEditorFactory, _editorConfigurationParser, EditorType.PropertyValue));
    

    not ideal solution as if the Property has not been used yet and I dont noe the model then it will fail.

    its like I should be looking for something like

    _dataTypeService.GetDataEditorByAlias("IBD.BlockMultinodePicker").FirstOrDefault();
    

    but it doesn't exist.

    Hope this makes sense

Please Sign in or register to post replies

Write your reply to:

Draft