@@ -20,13 +20,13 @@ public class MntpValueConverter : MultiNodeTreePickerValueConverter {
20
20
21
21
#region Constructors
22
22
23
+ private readonly IMemberService _memberService ;
23
24
private readonly IPublishedSnapshotAccessor _publishedSnapshotAccessor ;
24
- private readonly IUmbracoContextAccessor umbracoContextAccessor ;
25
25
private readonly MntpConverterCollection _converterCollection ;
26
26
27
27
public MntpValueConverter ( IPublishedSnapshotAccessor publishedSnapshotAccessor , IUmbracoContextAccessor umbracoContextAccessor , IMemberService memberService , MntpConverterCollection converterCollection ) : base ( publishedSnapshotAccessor , umbracoContextAccessor , memberService ) {
28
28
_publishedSnapshotAccessor = publishedSnapshotAccessor ?? throw new ArgumentNullException ( nameof ( publishedSnapshotAccessor ) ) ;
29
- this . umbracoContextAccessor = umbracoContextAccessor ;
29
+ _memberService = memberService ;
30
30
_converterCollection = converterCollection ;
31
31
}
32
32
@@ -43,43 +43,48 @@ public override PropertyCacheLevel GetPropertyCacheLevel(IPublishedPropertyType
43
43
}
44
44
45
45
public override object ConvertSourceToIntermediate ( IPublishedElement owner , IPublishedPropertyType propertyType , object source , bool preview ) {
46
- return source ? . ToString ( )
46
+ return source ? . ToString ( ) ?
47
47
. Split ( new [ ] { "," } , StringSplitOptions . RemoveEmptyEntries )
48
48
. Select ( UdiParser . Parse )
49
49
. ToArray ( ) ;
50
50
}
51
51
52
52
public override object ConvertIntermediateToObject ( IPublishedElement owner , IPublishedPropertyType propertyType , PropertyCacheLevel cacheLevel , object source , bool preview ) {
53
53
54
- object value = GetPickerValue ( owner , propertyType , cacheLevel , source , preview ) ;
55
-
56
- bool single = IsSingleNodePicker ( propertyType ) ;
57
-
58
- if ( propertyType . DataType . Configuration is MntpConfiguration config && config . ItemConverter != null ) {
59
-
60
- string key = config . ItemConverter . GetString ( "key" ) ;
61
-
62
- if ( _converterCollection . TryGet ( key , out IMntpItemConverter converter ) ) {
63
-
64
- if ( value is IPublishedElement element ) return converter . Convert ( propertyType , element ) ;
65
-
66
- if ( single == false ) {
67
-
68
- Type type = converter . GetType ( propertyType ) ;
69
-
70
- return ( value as IEnumerable < IPublishedElement > ?? new IPublishedElement [ 0 ] )
71
- . Select ( x => converter . Convert ( propertyType , x ) )
72
- . Cast ( type )
73
- . ToList ( type ) ;
74
-
75
- }
54
+ // gets the picked items as IPublishedContent
55
+ object value = GetPickerValue ( propertyType , source , preview ) ;
56
+
57
+ // Return "value" if the data type isn't configured with an item converter
58
+ if ( propertyType . DataType . Configuration is not MntpConfiguration config ) return value ;
59
+ if ( config . ItemConverter == null ) return value ;
60
+
61
+ // Get the key of the converter
62
+ string key = config . ItemConverter . GetString ( "key" ) ;
63
+
64
+ // Return "value" if item converter wasn't found
65
+ if ( ! _converterCollection . TryGet ( key , out IMntpItemConverter converter ) ) return value ;
66
+
67
+ switch ( value ) {
68
+
69
+ // If "value" is a single element, we can call the converter directly
70
+ case IPublishedElement element :
71
+ return converter . Convert ( propertyType , element ) ;
72
+
73
+ // If "value" is a list of elements, we call the converter for each item, and make sure to return a
74
+ // list of the type specified by the converter
75
+ case List < IPublishedElement > elements :
76
+ Type type = converter . GetType ( propertyType ) ;
77
+ return elements
78
+ . Select ( x => converter . Convert ( propertyType , x ) )
79
+ . Cast ( type )
80
+ . ToList ( type ) ;
76
81
77
- }
82
+ // If neither of the above cases are matched, we return "value" as a fallback
83
+ default :
84
+ return value ;
78
85
79
86
}
80
87
81
- return value ;
82
-
83
88
}
84
89
85
90
public override Type GetPropertyValueType ( IPublishedPropertyType propertyType ) {
@@ -104,75 +109,70 @@ public override Type GetPropertyValueType(IPublishedPropertyType propertyType) {
104
109
105
110
}
106
111
107
- private IPublishedContent GetPublishedContent < T > ( T nodeId , ref UmbracoObjectTypes actualType , UmbracoObjectTypes expectedType , Func < T , IPublishedContent > contentFetcher ) {
108
-
109
- // is the actual type supported by the content fetcher?
110
- if ( actualType != UmbracoObjectTypes . Unknown && actualType != expectedType ) {
111
- // no, return null
112
- return null ;
113
- }
114
-
115
- // attempt to get the content
116
- var content = contentFetcher ( nodeId ) ;
117
- if ( content != null ) {
118
- // if we found the content, assign the expected type to the actual type so we don't have to keep looking for other types of content
119
- actualType = expectedType ;
120
- }
121
- return content ;
122
- }
123
-
124
- public object GetPickerValue ( IPublishedElement owner , IPublishedPropertyType propertyType , PropertyCacheLevel cacheLevel , object source , bool preview ) {
125
- umbracoContextAccessor . TryGetUmbracoContext ( out var umbracoContext ) ;
112
+ private object GetPickerValue ( IPublishedPropertyType propertyType , object source , bool preview ) {
113
+
126
114
if ( source == null ) return null ;
127
115
128
- if ( umbracoContext == null ) return source ;
129
-
130
- Udi [ ] udis = source as Udi [ ] ?? new Udi [ 0 ] ;
131
-
116
+ Udi [ ] udis = source as Udi [ ] ?? Array . Empty < Udi > ( ) ;
132
117
if ( propertyType . Alias . Equals ( Constants . Conventions . Content . InternalRedirectId ) ) return udis . FirstOrDefault ( ) ;
133
118
if ( propertyType . Alias . Equals ( Constants . Conventions . Content . Redirect ) ) return udis . FirstOrDefault ( ) ;
119
+
120
+ // Get a reference to the current published snapshot
121
+ _publishedSnapshotAccessor . TryGetPublishedSnapshot ( out IPublishedSnapshot publishedSnapshot ) ;
122
+ if ( publishedSnapshot == null ) return source ;
134
123
124
+ // Is the data type configured as a single picker?
135
125
bool single = IsSingleNodePicker ( propertyType ) ;
136
126
137
- List < IPublishedContent > multiNodeTreePicker = new List < IPublishedContent > ( ) ;
138
-
139
- UmbracoObjectTypes objectType = UmbracoObjectTypes . Unknown ;
127
+ // Initialize a new list for the items
128
+ List < IPublishedContent > items = new List < IPublishedContent > ( ) ;
140
129
141
- foreach ( var udi in udis ) {
130
+ foreach ( Udi udi in udis ) {
142
131
132
+ // Make sure we have a GUID UDI
143
133
GuidUdi guidUdi = udi as GuidUdi ;
144
134
if ( guidUdi == null ) continue ;
145
135
146
- IPublishedContent multiNodeTreePickerItem = null ;
147
- var couldGetPublishedSnapshotAccessor = _publishedSnapshotAccessor . TryGetPublishedSnapshot ( out var publishedSnapshot ) ;
136
+ IPublishedContent item = null ;
137
+
148
138
switch ( udi . EntityType ) {
149
139
case Constants . UdiEntityType . Document :
150
- multiNodeTreePickerItem = GetPublishedContent ( udi , ref objectType , UmbracoObjectTypes . Document , id => publishedSnapshot . Content . GetById ( guidUdi . Guid ) ) ;
140
+ item = publishedSnapshot . Content . GetById ( preview , guidUdi . Guid ) ;
151
141
break ;
152
142
case Constants . UdiEntityType . Media :
153
- multiNodeTreePickerItem = GetPublishedContent ( udi , ref objectType , UmbracoObjectTypes . Media , id => publishedSnapshot . Media . GetById ( guidUdi . Guid ) ) ;
143
+ item = publishedSnapshot . Media . GetById ( preview , guidUdi . Guid ) ;
154
144
break ;
155
145
case Constants . UdiEntityType . Member :
156
- //multiNodeTreePickerItem = GetPublishedContent(udi, ref objectType, UmbracoObjectTypes.Member, id => publishedSnapshot.Members.GetByProviderKey(guidUdi.Guid) );
146
+ item = GetMemberByGuidUdi ( guidUdi , publishedSnapshot ) ;
157
147
break ;
158
148
}
159
149
160
- if ( multiNodeTreePickerItem != null && multiNodeTreePickerItem . ContentType . ItemType != PublishedItemType . Element ) {
161
- multiNodeTreePicker . Add ( multiNodeTreePickerItem ) ;
162
- if ( single ) break ;
163
- }
150
+ // Continue to the next UDI if "item" is either null or an element type
151
+ if ( item == null ) continue ;
152
+ if ( item . ItemType == PublishedItemType . Element ) continue ;
153
+
154
+ // Append the item to the list
155
+ items . Add ( item ) ;
156
+
157
+ // If the data type is configured as a single picker, we break the loop as we don't really need to
158
+ // look up any additional items that may be picked
159
+ if ( single ) break ;
164
160
165
161
}
166
162
167
- if ( single ) return multiNodeTreePicker . FirstOrDefault ( ) ;
168
- return multiNodeTreePicker ;
163
+ return single ? items . FirstOrDefault ( ) : items ;
169
164
170
165
}
171
166
172
167
private static bool IsSingleNodePicker ( IPublishedPropertyType propertyType ) {
173
168
return propertyType . DataType . ConfigurationAs < MultiNodePickerConfiguration > ( ) . MaxNumber == 1 ;
174
169
}
175
170
171
+ private IPublishedContent GetMemberByGuidUdi ( GuidUdi udi , IPublishedSnapshot snapshot ) {
172
+ IMember member = _memberService . GetByKey ( udi . Guid ) ;
173
+ return member == null ? null : snapshot . Members . Get ( member ) ;
174
+ }
175
+
176
176
#endregion
177
177
178
178
}
0 commit comments