Create a custom datatype based on enum values in Ucommerce

296 views Asked by At

Can anyone help me on how I can create a custom datatype, that works exactly like that of an enum, but the editor must be called, in my case, "Radio Button". So the values will be populated via the "Create Data Type Enum" option.

I've read the documentation at https://docs.ucommerce.net/ucommerce/v8.1/extending-ucommerce/custom-data-type.html, but it's not clear how I can get the data like this, it just explains how I get the info from a data source (_priceGroupRepository in the example).

Like this mock up.

Example of how I want to set up radio button

Example of how I want to add the data

2

There are 2 answers

1
Anders from Ucommerce On

From the documentation you sent, it looks like you will just have to return a RadioButtonList rather than a DropDownList, try using System.Web.UI.WebControls.RadioButtonList

To replace the enum datatype, you will want to return the editor as "Enum" instead of "PriceGroupPicker" and when registering the component, you will want to use this ID id="DropDownListControlFactory" as you want to overwrite the Enum Factory.

0
Anders from Ucommerce On

I tested out how to get the desired behavior of Radio Button locally, here's my findings:

You will only be able to do this by replacing the current behavior of the Enum, as this is the only one with the functionality of adding Data Type Enum Fields in Ucommerce.

The ControlFactory should look like this:

You can see I'm extending IEnumerable<DataTypeDefinition> to get the functionality of the Enum. And also replacing the foreach of the priceGroups with the foreach of DefinitionFields.DataTypeEnums.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Ucommerce.EntitiesV2;
using Ucommerce.EntitiesV2.Definitions;
using Ucommerce.Presentation.Web.Controls;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ucommerce.Infrastructure.Components.Windsor;
using Ucommerce.Security;
namespace UcommerceSamples.Extensions.Code
{
    public class RadioButtonControlFactory: IControlFactory, IEnumerable<DataTypeDefinition>
    {
        private readonly IDataTypeDefinitionInspector _dataTypeDefinitionInspector;
        private DataTypeDefinition Definition = new DataTypeDefinition("Enum");
        [Mandatory]
        public ITextSanitizer TextSanitizer { get; set; }
        public RadioButtonControlFactory(IDataTypeDefinitionInspector dataTypeDefinitionInspector )
        {
            _dataTypeDefinitionInspector = dataTypeDefinitionInspector;
        }
        public bool Supports(DataType dataType)
        {
            var dataTypeDefinitionName = _dataTypeDefinitionInspector.GetDataTypeDefintion(dataType);
            return dataTypeDefinitionName.Equals(Definition.Name, StringComparison.InvariantCultureIgnoreCase);
        }
        public Control GetControl(IProperty property)
        {
            string stringValue = property.GetValue() != null ? TextSanitizer.SanitizeOutput(property.GetValue().ToString())
                : "";
            var radioButtonListControl = new RadioButtonList()
            {
                ID = TextSanitizer.SanitizeOutput(property.GetDefinitionField().Name)
            };
            foreach (var enumValue in property.GetDefinitionField().DataType.DataTypeEnums.OrderBy(x => x.SortOrder))
            {
                var listItem = new ListItem(TextSanitizer.SanitizeOutput(enumValue.Value),
                    TextSanitizer.SanitizeOutput(enumValue.Value));
                radioButtonListControl.Items.Add(listItem);
            }
            if (radioButtonListControl.Items.FindByValue(stringValue) != null)
                radioButtonListControl.SelectedValue = stringValue;
            radioButtonListControl.CssClass = "uc-drop-down-list";
            return radioButtonListControl;
        }
        public IEnumerator<DataTypeDefinition> GetEnumerator()
        {
            yield return Definition;
        }
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
}

The config should look like this to override the DropDownListControlFactory (read EnumListControlFactory):

<configuration>
        <components>
                <component
                        id="DropDownListControlFactory"
                        service="Ucommerce.Presentation.Web.Controls.IControlFactory, Ucommerce.Presentation"
                        type="UcommerceSamples.Extensions.Code.RadioButtonControlFactory, UcommerceSamples.Extensions" />
        </components>
</configuration>

I hope this is sufficient unless you want to use both RadioButton and Enum, let me know if that's the case, and I will see if that's a possibility.