Skip to content

Commit a3aa0a4

Browse files
committed
Code cleanup
+ Private fields should have an underscore as prefix + Simplified the implementation of the value converter + etc.
1 parent c22a1a2 commit a3aa0a4

File tree

2 files changed

+69
-69
lines changed

2 files changed

+69
-69
lines changed

src/Skybrud.Umbraco.MultiNodeTreePicker/PropertyEditors/MntpEditor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ public class MntpEditor : MultiNodeTreePickerPropertyEditor {
1010

1111
internal const string EditorView = "contentpicker";
1212

13-
private readonly IIOHelper iOHelper;
13+
private readonly IIOHelper _iOHelper;
1414

1515
public MntpEditor(IDataValueEditorFactory dataValueEditorFactory, IIOHelper iOHelper) : base(dataValueEditorFactory, iOHelper) {
16-
this.iOHelper = iOHelper;
16+
this._iOHelper = iOHelper;
1717
}
1818

1919
protected override IConfigurationEditor CreateConfigurationEditor() {
20-
return new MntpConfigurationEditor(iOHelper);
20+
return new MntpConfigurationEditor(_iOHelper);
2121
}
2222

2323
}

src/Skybrud.Umbraco.MultiNodeTreePicker/PropertyEditors/ValueConverters/MntpValueConverter.cs

Lines changed: 66 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ public class MntpValueConverter : MultiNodeTreePickerValueConverter {
2020

2121
#region Constructors
2222

23+
private readonly IMemberService _memberService;
2324
private readonly IPublishedSnapshotAccessor _publishedSnapshotAccessor;
24-
private readonly IUmbracoContextAccessor umbracoContextAccessor;
2525
private readonly MntpConverterCollection _converterCollection;
2626

2727
public MntpValueConverter(IPublishedSnapshotAccessor publishedSnapshotAccessor, IUmbracoContextAccessor umbracoContextAccessor, IMemberService memberService, MntpConverterCollection converterCollection) : base(publishedSnapshotAccessor, umbracoContextAccessor, memberService) {
2828
_publishedSnapshotAccessor = publishedSnapshotAccessor ?? throw new ArgumentNullException(nameof(publishedSnapshotAccessor));
29-
this.umbracoContextAccessor = umbracoContextAccessor;
29+
_memberService = memberService;
3030
_converterCollection = converterCollection;
3131
}
3232

@@ -43,43 +43,48 @@ public override PropertyCacheLevel GetPropertyCacheLevel(IPublishedPropertyType
4343
}
4444

4545
public override object ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType, object source, bool preview) {
46-
return source?.ToString()
46+
return source?.ToString()?
4747
.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries)
4848
.Select(UdiParser.Parse)
4949
.ToArray();
5050
}
5151

5252
public override object ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel cacheLevel, object source, bool preview) {
5353

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);
7681

77-
}
82+
// If neither of the above cases are matched, we return "value" as a fallback
83+
default:
84+
return value;
7885

7986
}
8087

81-
return value;
82-
8388
}
8489

8590
public override Type GetPropertyValueType(IPublishedPropertyType propertyType) {
@@ -104,75 +109,70 @@ public override Type GetPropertyValueType(IPublishedPropertyType propertyType) {
104109

105110
}
106111

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+
126114
if (source == null) return null;
127115

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>();
132117
if (propertyType.Alias.Equals(Constants.Conventions.Content.InternalRedirectId)) return udis.FirstOrDefault();
133118
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;
134123

124+
// Is the data type configured as a single picker?
135125
bool single = IsSingleNodePicker(propertyType);
136126

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>();
140129

141-
foreach (var udi in udis) {
130+
foreach (Udi udi in udis) {
142131

132+
// Make sure we have a GUID UDI
143133
GuidUdi guidUdi = udi as GuidUdi;
144134
if (guidUdi == null) continue;
145135

146-
IPublishedContent multiNodeTreePickerItem = null;
147-
var couldGetPublishedSnapshotAccessor = _publishedSnapshotAccessor.TryGetPublishedSnapshot(out var publishedSnapshot);
136+
IPublishedContent item = null;
137+
148138
switch (udi.EntityType) {
149139
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);
151141
break;
152142
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);
154144
break;
155145
case Constants.UdiEntityType.Member:
156-
//multiNodeTreePickerItem = GetPublishedContent(udi, ref objectType, UmbracoObjectTypes.Member, id => publishedSnapshot.Members.GetByProviderKey(guidUdi.Guid));
146+
item = GetMemberByGuidUdi(guidUdi, publishedSnapshot);
157147
break;
158148
}
159149

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;
164160

165161
}
166162

167-
if (single) return multiNodeTreePicker.FirstOrDefault();
168-
return multiNodeTreePicker;
163+
return single ? items.FirstOrDefault() : items;
169164

170165
}
171166

172167
private static bool IsSingleNodePicker(IPublishedPropertyType propertyType) {
173168
return propertyType.DataType.ConfigurationAs<MultiNodePickerConfiguration>().MaxNumber == 1;
174169
}
175170

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+
176176
#endregion
177177

178178
}

0 commit comments

Comments
 (0)