Skip to content
This repository was archived by the owner on May 9, 2023. It is now read-only.

Commit 366e60d

Browse files
committed
Added CustomValueConverterDataValueEditor
So to support the wrapped editor's data-type configuration.
1 parent 845c60c commit 366e60d

File tree

2 files changed

+84
-6
lines changed

2 files changed

+84
-6
lines changed

src/DataEditor/CustomValueConverterDataEditor.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,15 @@ public CustomValueConverterDataEditor(
8484

8585
public IDataValueEditor GetValueEditor()
8686
{
87-
#if NET472
88-
return new DataValueEditor
89-
#else
90-
return new DataValueEditor(
87+
return new CustomValueConverterDataValueEditor(
88+
_dataTypeService,
89+
_propertyEditors,
9190
_localizedTextService,
9291
_shortStringHelper,
9392
_jsonSerializer)
94-
#endif
9593
{
9694
ValueType = ValueTypes.Json,
97-
View = DataEditorViewPath,
95+
View = DataEditorViewPath
9896
};
9997
}
10098

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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

Comments
 (0)