using System.Collections.Generic; using Umbraco.Core; using Umbraco.Core.Logging; using Umbraco.Core.Models; using Umbraco.Core.PropertyEditors; using Umbraco.Core.Services; namespace Umbraco.Web.PropertyEditors { /// /// A property editor to allow multiple selection of pre-defined items /// /// /// Due to maintaining backwards compatibility this data type stores the value as a string which is a comma separated value of the /// ids of the individual items so we have logic in here to deal with that. /// [PropertyEditor(Constants.PropertyEditors.DropdownlistMultiplePublishKeysAlias, "Dropdown list multiple, publish keys", "dropdown", Group = "lists", Icon = "icon-bulleted-list")] public class DropDownMultipleWithKeysPropertyEditor : DropDownPropertyEditor { private readonly ILocalizedTextService _textService; /// /// The constructor will setup the property editor based on the attribute if one is found /// public DropDownMultipleWithKeysPropertyEditor(ILogger logger, ILocalizedTextService textService) : base(logger, textService) { _textService = textService; } protected override PropertyValueEditor CreateValueEditor() { return new PublishValuesMultipleValueEditor(true, base.CreateValueEditor()); } protected override PreValueEditor CreatePreValueEditor() { return new DropDownMultiplePreValueEditor(_textService); } /// /// A pre-value editor for the 'drop down list multiple' property editor that ensures that 'multiple' is saved for the config in the db but is not /// rendered as a pre-value field. /// /// /// This is mostly to maintain backwards compatibility with old property editors. Devs can now simply use the Drop down property editor and check the multiple pre-value checkbox /// internal class DropDownMultiplePreValueEditor : ValueListPreValueEditor { public DropDownMultiplePreValueEditor(ILocalizedTextService textService) : base(textService) { //add the multiple field, we'll make it hidden so it is not seen in the pre-value editor Fields.Add(new PreValueField { Key = "multiple", Name = "multiple", View = "hidden", HideLabel = true }); } /// /// Always /// /// /// /// public override IDictionary ConvertDbToEditor(IDictionary defaultPreVals, PreValueCollection persistedPreVals) { var returnVal = base.ConvertDbToEditor(defaultPreVals, persistedPreVals); //always add the multiple param to true returnVal["multiple"] = "1"; return returnVal; } } } }