|
1 | 1 | using System;
|
| 2 | +using System.Collections; |
2 | 3 | using System.Collections.Generic;
|
3 | 4 | using System.Linq;
|
| 5 | +using System.Reflection; |
| 6 | +using System.Web; |
| 7 | + |
| 8 | + |
4 | 9 |
|
5 | 10 | #if NETCOREAPP
|
6 | 11 | using Umbraco.Cms.Core.Models;
|
@@ -35,10 +40,15 @@ public static Dictionary<string, string> ToContentTypeDictionary(this IContentTy
|
35 | 40 |
|
36 | 41 | var contentProperty = content.Properties.First(p => p.Alias == propertyType.Alias);
|
37 | 42 |
|
38 |
| - if(IsMedia(contentProperty, out string url)) |
| 43 | + if (IsList(contentProperty, out string value)) |
| 44 | + { |
| 45 | + contentDict.Add(propertyType.Alias, value); |
| 46 | + } else if (IsMedia(contentProperty, out string url)) |
| 47 | + { |
39 | 48 | contentDict.Add(propertyType.Alias, url);
|
| 49 | + } |
40 | 50 | else
|
41 |
| - contentDict.Add(propertyType.Alias, contentProperty.GetValue().ToString()); |
| 51 | + contentDict.Add(propertyType.Alias, contentProperty.GetValue()?.ToString()); |
42 | 52 | }
|
43 | 53 |
|
44 | 54 | return contentDict;
|
@@ -67,16 +77,77 @@ private static bool IsMedia(IPublishedProperty contentProperty, out string url)
|
67 | 77 | {
|
68 | 78 | case Core.Constants.PropertyEditors.Aliases.MediaPicker:
|
69 | 79 | var mediaPickerValue = contentProperty.GetValue() as IPublishedContent;
|
70 |
| - url = mediaPickerValue.Url(); |
| 80 | + url = mediaPickerValue != null ? mediaPickerValue.Url() : string.Empty; |
71 | 81 | return true;
|
72 | 82 | case Core.Constants.PropertyEditors.Aliases.MediaPicker3:
|
73 | 83 | var mediaPicker3Value = contentProperty.GetValue() as MediaWithCrops;
|
74 |
| - url = mediaPicker3Value.LocalCrops.Src; |
| 84 | + url = mediaPicker3Value != null ? mediaPicker3Value.LocalCrops.Src : string.Empty; |
75 | 85 | return true;
|
76 | 86 | default:
|
77 | 87 | url = string.Empty;
|
78 | 88 | return false;
|
79 | 89 | }
|
80 | 90 | }
|
| 91 | + |
| 92 | + private static bool IsList(IPublishedProperty contentProperty, out string value) |
| 93 | + { |
| 94 | + List<string> items = new List<string>(); |
| 95 | + value = string.Empty; |
| 96 | + bool isList = false; |
| 97 | + |
| 98 | + if (contentProperty.GetValue() == null) { return false; } |
| 99 | + |
| 100 | + var type = contentProperty.GetValue().GetType(); |
| 101 | + if (type == null) |
| 102 | + { |
| 103 | + throw new ArgumentNullException("type"); |
| 104 | + } |
| 105 | + |
| 106 | + if (typeof(IList).IsAssignableFrom(type)) |
| 107 | + { |
| 108 | + isList = true; |
| 109 | + } |
| 110 | + |
| 111 | + foreach (var item in type.GetInterfaces()) |
| 112 | + { |
| 113 | + if (item.IsGenericType && typeof(IList<>) == item.GetGenericTypeDefinition()) |
| 114 | + { |
| 115 | + isList = true; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + if (isList) |
| 120 | + { |
| 121 | + var contentPropertyValue = contentProperty.GetValue(); |
| 122 | + var contentPropertyValueType = contentPropertyValue.GetType(); |
| 123 | + |
| 124 | + if (contentPropertyValueType.GetProperties().Count(p => p.Name == "Item") == 1) |
| 125 | + { |
| 126 | + var count = contentPropertyValueType.GetProperty("Count").GetValue(contentPropertyValue); |
| 127 | + PropertyInfo indexer = contentPropertyValueType.GetProperty("Item"); |
| 128 | + |
| 129 | + for (int i = 0; i < (int)count; i++) |
| 130 | + { |
| 131 | + object item = indexer.GetValue(contentProperty.GetValue(), new object[] { i }); |
| 132 | + var val = item.GetType().GetProperty("Name").GetValue(item).ToString(); |
| 133 | + |
| 134 | + items.Add(val); |
| 135 | + } |
| 136 | + |
| 137 | + value = string.Join(", ", items.ToArray()); |
| 138 | + } else |
| 139 | + { |
| 140 | + // is array |
| 141 | + foreach (var item in contentPropertyValue as Array) |
| 142 | + { |
| 143 | + items.Add(item.ToString()); |
| 144 | + } |
| 145 | + |
| 146 | + value = string.Join(", ", items.ToArray()); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + return isList; |
| 151 | + } |
81 | 152 | }
|
82 | 153 | }
|
0 commit comments