|
| 1 | +/* Copyright © 2022 Lee Kelleher. |
| 2 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ |
| 5 | + |
| 6 | +using System.Collections.Generic; |
| 7 | +#if NET472 |
| 8 | +using Umbraco.Core; |
| 9 | +using Umbraco.Core.Models; |
| 10 | +using Umbraco.Core.PropertyEditors; |
| 11 | +using Umbraco.Core.Serialization; |
| 12 | +using Umbraco.Core.Services; |
| 13 | +using Umbraco.Core.Strings; |
| 14 | +#else |
| 15 | +using Umbraco.Cms.Core; |
| 16 | +using Umbraco.Cms.Core.PropertyEditors; |
| 17 | +using Umbraco.Extensions; |
| 18 | +using Umbraco.Cms.Core.Models; |
| 19 | +using Umbraco.Cms.Core.Serialization; |
| 20 | +using Umbraco.Cms.Core.Services; |
| 21 | +using Umbraco.Cms.Core.Strings; |
| 22 | +#endif |
| 23 | + |
| 24 | +namespace Umbraco.Community.CustomValueConverter |
| 25 | +{ |
| 26 | + // NOTE: A custom `DataValueEditor` to specifically support `ToEditor()` calls, as it is called without DataType configuration. |
| 27 | + // ref: https://github.com/umbraco/Umbraco-CMS/blob/release-9.0.0/src/Umbraco.Core/Models/Mapping/ContentPropertyBasicMapper.cs#L81 |
| 28 | + internal sealed class CustomValueConverterDataValueEditor : DataValueEditor |
| 29 | + { |
| 30 | + private readonly IDataTypeService _dataTypeService; |
| 31 | + private readonly PropertyEditorCollection _propertyEditors; |
| 32 | + |
| 33 | + public CustomValueConverterDataValueEditor( |
| 34 | + IDataTypeService dataTypeService, |
| 35 | + PropertyEditorCollection propertyEditors, |
| 36 | + ILocalizedTextService localizedTextService, |
| 37 | + IShortStringHelper shortStringHelper, |
| 38 | + IJsonSerializer jsonSerializer) |
| 39 | +#if NET472 |
| 40 | + : base() |
| 41 | +#else |
| 42 | + : base(localizedTextService, shortStringHelper, jsonSerializer) |
| 43 | +#endif |
| 44 | + { |
| 45 | + _dataTypeService = dataTypeService; |
| 46 | + _propertyEditors = propertyEditors; |
| 47 | + } |
| 48 | + |
| 49 | +#if NET472 |
| 50 | + public override object ToEditor(Property property, IDataTypeService dataTypeService, string culture = null, string segment = null) |
| 51 | +#else |
| 52 | + public override object ToEditor(IProperty property, string culture = null, string segment = null) |
| 53 | +#endif |
| 54 | + { |
| 55 | + var dataType = _dataTypeService.GetDataType(property.PropertyType.DataTypeId); |
| 56 | + if (dataType != null && |
| 57 | + dataType.Configuration is Dictionary<string, object> config && |
| 58 | + config.TryGetValueAs(CustomValueConverterConfigurationEditor.DataType, out GuidUdi udi) == true) |
| 59 | + { |
| 60 | + var dataType2 = _dataTypeService.GetDataType(udi.Guid); |
| 61 | + if (dataType2 != null && _propertyEditors.TryGet(dataType2.EditorAlias, out var dataEditor2) == true) |
| 62 | + { |
| 63 | + var valueEditor2 = dataEditor2.GetValueEditor(dataType2.Configuration); |
| 64 | + |
| 65 | +#if NET472 |
| 66 | + return valueEditor2.ToEditor(property, dataTypeService, segment); |
| 67 | +#else |
| 68 | + return valueEditor2.ToEditor(property, culture, segment); |
| 69 | +#endif |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | +#if NET472 |
| 74 | + return base.ToEditor(property, dataTypeService, segment); |
| 75 | +#else |
| 76 | + return base.ToEditor(property, culture, segment); |
| 77 | +#endif |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments