From 477761a81adf317c6f1b94d9d59d0c4ec296e698 Mon Sep 17 00:00:00 2001 From: Ronald Barendse Date: Thu, 22 Mar 2018 10:41:14 +0100 Subject: [PATCH] Added property editor alias to PreValueDisplayResolver warning and updated key lookup --- .../Models/Mapping/DataTypeModelMapper.cs | 2 +- .../Models/Mapping/PreValueDisplayResolver.cs | 36 ++++++++++--------- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/Umbraco.Web/Models/Mapping/DataTypeModelMapper.cs b/src/Umbraco.Web/Models/Mapping/DataTypeModelMapper.cs index 60d9eb7d3b..c2f8d8e25d 100644 --- a/src/Umbraco.Web/Models/Mapping/DataTypeModelMapper.cs +++ b/src/Umbraco.Web/Models/Mapping/DataTypeModelMapper.cs @@ -116,7 +116,7 @@ namespace Umbraco.Web.Models.Mapping var fields = editor.PreValueEditor.Fields.Select(Mapper.Map).ToArray(); if (defaultVals != null) { - PreValueDisplayResolver.MapPreValueValuesToPreValueFields(fields, defaultVals); + PreValueDisplayResolver.MapPreValueValuesToPreValueFields(fields, defaultVals, editor.Alias); } return fields; }); diff --git a/src/Umbraco.Web/Models/Mapping/PreValueDisplayResolver.cs b/src/Umbraco.Web/Models/Mapping/PreValueDisplayResolver.cs index f4e7a78504..9d1331e691 100644 --- a/src/Umbraco.Web/Models/Mapping/PreValueDisplayResolver.cs +++ b/src/Umbraco.Web/Models/Mapping/PreValueDisplayResolver.cs @@ -18,27 +18,31 @@ namespace Umbraco.Web.Models.Mapping public PreValueDisplayResolver(IDataTypeService dataTypeService) { _dataTypeService = dataTypeService; - } - + } + /// - /// Maps pre-values in the dictionary to the values for the fields + /// Maps pre-values in the dictionary to the values for the fields. /// - /// - /// - internal static void MapPreValueValuesToPreValueFields(PreValueFieldDisplay[] fields, IDictionary preValues) + /// The fields. + /// The pre-values. + /// The editor alias. + internal static void MapPreValueValuesToPreValueFields(PreValueFieldDisplay[] fields, IDictionary preValues, string editorAlias) { - if (fields == null) throw new ArgumentNullException("fields"); - if (preValues == null) throw new ArgumentNullException("preValues"); - //now we need to wire up the pre-values values with the actual fields defined + if (fields == null) throw new ArgumentNullException(nameof(fields)); + if (preValues == null) throw new ArgumentNullException(nameof(preValues)); + + // Now we need to wire up the pre-values values with the actual fields defined foreach (var field in fields) { - var found = preValues.Any(x => x.Key.InvariantEquals(field.Key)); - if (found == false) + // If the dictionary would be constructed with StringComparer.InvariantCultureIgnoreCase, we could just use TryGetValue + var preValue = preValues.SingleOrDefault(x => x.Key.InvariantEquals(field.Key)); + if (preValue.Key == null) { - LogHelper.Warn("Could not find persisted pre-value for field " + field.Key); + LogHelper.Warn("Could not find persisted pre-value for field {0} on property editor {1}", () => field.Key, () => editorAlias); continue; } - field.Value = preValues.Single(x => x.Key.InvariantEquals(field.Key)).Value; + + field.Value = preValue.Value; } } @@ -54,20 +58,20 @@ namespace Umbraco.Web.Models.Mapping } } - //set up the defaults + // Set up the defaults var dataTypeService = _dataTypeService; var preVals = dataTypeService.GetPreValuesCollectionByDataTypeId(source.Id); IDictionary dictionaryVals = preVals.FormatAsDictionary().ToDictionary(x => x.Key, x => (object)x.Value); var result = Enumerable.Empty().ToArray(); - //if we have a prop editor, then format the pre-values based on it and create it's fields. + // If we have a prop editor, then format the pre-values based on it and create it's fields if (propEd != null) { result = propEd.PreValueEditor.Fields.Select(Mapper.Map).ToArray(); dictionaryVals = propEd.PreValueEditor.ConvertDbToEditor(propEd.DefaultPreValues, preVals); } - MapPreValueValuesToPreValueFields(result, dictionaryVals); + MapPreValueValuesToPreValueFields(result, dictionaryVals, source.PropertyEditorAlias); return result; }