From 5c3d045aa1189e8760b5090ebb939ce571dccab0 Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Thu, 23 Jan 2025 11:59:24 +0100 Subject: [PATCH] #8800: Reverting breaking changes to Enumeration Fields caused by #8789 (#8824) * Revert "#8640: Fixing consistency between different Enumeration Field flavors' data storage (#8789)" This reverts commit fdbb06ba8dc641be9f46dabd7f94086d507e9537. * Re-adding change to fix that changing the ListMode of an EnumerationField from a multi-select to a single-select flavor shouldn't break the editor * Code styling in Fields/Enumeration.Edit.cshtml --- .../Drivers/EnumerationFieldDriver.cs | 35 +++++++++++-------- .../Orchard.Fields/Fields/EnumerationField.cs | 26 ++++++++++---- .../Orchard.Fields/Orchard.Fields.csproj | 6 +--- .../Fields/Enumeration.Edit.cshtml | 32 +++++++++++++---- 4 files changed, 66 insertions(+), 33 deletions(-) diff --git a/src/Orchard.Web/Modules/Orchard.Fields/Drivers/EnumerationFieldDriver.cs b/src/Orchard.Web/Modules/Orchard.Fields/Drivers/EnumerationFieldDriver.cs index d1b93fe6e..fb9e64649 100644 --- a/src/Orchard.Web/Modules/Orchard.Fields/Drivers/EnumerationFieldDriver.cs +++ b/src/Orchard.Web/Modules/Orchard.Fields/Drivers/EnumerationFieldDriver.cs @@ -4,41 +4,46 @@ using Orchard.ContentManagement.Handlers; using Orchard.Fields.Fields; using Orchard.Fields.Settings; using Orchard.Localization; +using System; +using System.Collections.Generic; +using System.Linq; namespace Orchard.Fields.Drivers { public class EnumerationFieldDriver : ContentFieldDriver { public IOrchardServices Services { get; set; } - private const string TemplateName = "Fields/Enumeration.Edit"; public EnumerationFieldDriver(IOrchardServices services) { Services = services; - T = NullLocalizer.Instance; } public Localizer T { get; set; } - private static string GetPrefix(ContentField field, ContentPart part) => - part.PartDefinition.Name + "." + field.Name; + private static string GetPrefix(ContentField field, ContentPart part) { + return part.PartDefinition.Name + "." + field.Name; + } - private static string GetDifferentiator(EnumerationField field) => field.Name; + private static string GetDifferentiator(EnumerationField field, ContentPart part) { + return field.Name; + } protected override DriverResult Display(ContentPart part, EnumerationField field, string displayType, dynamic shapeHelper) { - return ContentShape("Fields_Enumeration", GetDifferentiator(field), () => shapeHelper.Fields_Enumeration()); + return ContentShape("Fields_Enumeration", GetDifferentiator(field, part), + () => shapeHelper.Fields_Enumeration()); } protected override DriverResult Editor(ContentPart part, EnumerationField field, dynamic shapeHelper) { - return ContentShape("Fields_Enumeration_Edit", GetDifferentiator(field), () => { - if (part.IsNew() && string.IsNullOrEmpty(field.Value)) { - var settings = field.PartFieldDefinition.Settings.GetModel(); - if (!string.IsNullOrWhiteSpace(settings.DefaultValue)) { - field.SelectedValues = new string[] { settings.DefaultValue }; + return ContentShape("Fields_Enumeration_Edit", GetDifferentiator(field, part), + () => { + if (part.IsNew() && String.IsNullOrEmpty(field.Value)) { + var settings = field.PartFieldDefinition.Settings.GetModel(); + if (!String.IsNullOrWhiteSpace(settings.DefaultValue)) { + field.Value = settings.DefaultValue; + } } - } - - return shapeHelper.EditorTemplate(TemplateName: TemplateName, Model: field, Prefix: GetPrefix(field, part)); - }); + return shapeHelper.EditorTemplate(TemplateName: TemplateName, Model: field, Prefix: GetPrefix(field, part)); + }); } protected override DriverResult Editor(ContentPart part, EnumerationField field, IUpdateModel updater, dynamic shapeHelper) { diff --git a/src/Orchard.Web/Modules/Orchard.Fields/Fields/EnumerationField.cs b/src/Orchard.Web/Modules/Orchard.Fields/Fields/EnumerationField.cs index ee572b66b..a81d6829e 100644 --- a/src/Orchard.Web/Modules/Orchard.Fields/Fields/EnumerationField.cs +++ b/src/Orchard.Web/Modules/Orchard.Fields/Fields/EnumerationField.cs @@ -7,16 +7,28 @@ namespace Orchard.Fields.Fields { private const char Separator = ';'; public string Value { - get => Storage.Get()?.Trim(Separator) ?? ""; - set => Storage.Set(string.IsNullOrWhiteSpace(value) - ? string.Empty - // It is now the responsibility of this field to (re-)add the separators. - : Separator + value.Trim(Separator) + Separator); + get { return Storage.Get(); } + set { Storage.Set(value ?? String.Empty); } } public string[] SelectedValues { - get => Value?.Split(new[] { Separator }, StringSplitOptions.RemoveEmptyEntries) ?? new string[0]; - set => Value = value?.Length > 0 ? string.Join(Separator.ToString(), value) : ""; + get { + var value = Value; + if(string.IsNullOrWhiteSpace(value)) { + return new string[0]; + } + + return value.Split(new [] { Separator }, StringSplitOptions.RemoveEmptyEntries); + } + + set { + if (value == null || value.Length == 0) { + Value = String.Empty; + } + else { + Value = Separator + string.Join(Separator.ToString(), value) + Separator; + } + } } } } diff --git a/src/Orchard.Web/Modules/Orchard.Fields/Orchard.Fields.csproj b/src/Orchard.Web/Modules/Orchard.Fields/Orchard.Fields.csproj index e77d8c707..239ad497d 100644 --- a/src/Orchard.Web/Modules/Orchard.Fields/Orchard.Fields.csproj +++ b/src/Orchard.Web/Modules/Orchard.Fields/Orchard.Fields.csproj @@ -169,10 +169,6 @@ - - - - @@ -223,4 +219,4 @@ - + \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Fields/Views/EditorTemplates/Fields/Enumeration.Edit.cshtml b/src/Orchard.Web/Modules/Orchard.Fields/Views/EditorTemplates/Fields/Enumeration.Edit.cshtml index 0f3d10036..bf63c5e00 100644 --- a/src/Orchard.Web/Modules/Orchard.Fields/Views/EditorTemplates/Fields/Enumeration.Edit.cshtml +++ b/src/Orchard.Web/Modules/Orchard.Fields/Views/EditorTemplates/Fields/Enumeration.Edit.cshtml @@ -4,30 +4,48 @@ @{ var settings = Model.PartFieldDefinition.Settings.GetModel(); - string[] options = (!String.IsNullOrWhiteSpace(settings.Options)) ? settings.Options.Split(new string[] { System.Environment.NewLine }, StringSplitOptions.None) : new string[] { T("Select an option").ToString() }; + string[] options = (!String.IsNullOrWhiteSpace(settings.Options)) ? + settings.Options.Split(new string[] { System.Environment.NewLine }, StringSplitOptions.None) + : new string[] { T("Select an option").ToString() }; }
@switch (settings.ListMode) { case ListMode.Dropdown: - @Html.DropDownListFor(m => m.Value, new SelectList(options, Model.SelectedValues.FirstOrDefault()), settings.Required ? new { required = "required" } : null) + @Html.DropDownListFor( + m => m.Value, + new SelectList(options, Model.SelectedValues.FirstOrDefault()), + settings.Required ? new { required = "required" } : null) break; case ListMode.Radiobutton: foreach (var option in options) { if (string.IsNullOrWhiteSpace(option)) { - + } else { - + } } break; case ListMode.Listbox: - @Html.ListBoxFor(m => m.SelectedValues, new MultiSelectList(options, Model.SelectedValues), settings.Required ? new { required = "required" } : null) + @Html.ListBoxFor( + m => m.SelectedValues, + new MultiSelectList(options, Model.SelectedValues), + settings.Required ? new { required = "required" } : null) break; case ListMode.Checkbox: @@ -37,7 +55,9 @@ index++; if (!string.IsNullOrWhiteSpace(option)) {
- +
}