Skip to content

Commit d4e185e

Browse files
committed
Add extension method for handling lists and arrays.
1 parent 8699c01 commit d4e185e

File tree

2 files changed

+76
-5
lines changed

2 files changed

+76
-5
lines changed

src/Umbraco.Cms.Integrations.Automation.Zapier/Extensions/ContentExtensions.cs

Lines changed: 75 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
34
using System.Linq;
5+
using System.Reflection;
6+
using System.Web;
7+
8+
49

510
#if NETCOREAPP
611
using Umbraco.Cms.Core.Models;
@@ -35,10 +40,15 @@ public static Dictionary<string, string> ToContentTypeDictionary(this IContentTy
3540

3641
var contentProperty = content.Properties.First(p => p.Alias == propertyType.Alias);
3742

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+
{
3948
contentDict.Add(propertyType.Alias, url);
49+
}
4050
else
41-
contentDict.Add(propertyType.Alias, contentProperty.GetValue().ToString());
51+
contentDict.Add(propertyType.Alias, contentProperty.GetValue()?.ToString());
4252
}
4353

4454
return contentDict;
@@ -67,16 +77,77 @@ private static bool IsMedia(IPublishedProperty contentProperty, out string url)
6777
{
6878
case Core.Constants.PropertyEditors.Aliases.MediaPicker:
6979
var mediaPickerValue = contentProperty.GetValue() as IPublishedContent;
70-
url = mediaPickerValue.Url();
80+
url = mediaPickerValue != null ? mediaPickerValue.Url() : string.Empty;
7181
return true;
7282
case Core.Constants.PropertyEditors.Aliases.MediaPicker3:
7383
var mediaPicker3Value = contentProperty.GetValue() as MediaWithCrops;
74-
url = mediaPicker3Value.LocalCrops.Src;
84+
url = mediaPicker3Value != null ? mediaPicker3Value.LocalCrops.Src : string.Empty;
7585
return true;
7686
default:
7787
url = string.Empty;
7888
return false;
7989
}
8090
}
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+
}
81152
}
82153
}

src/Umbraco.Cms.Integrations.Automation.Zapier/Umbraco.Cms.Integrations.Automation.Zapier.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<RepositoryUrl>https://github.com/umbraco/Umbraco.Cms.Integrations</RepositoryUrl>
1010
<PackageProjectUrl>https://github.com/umbraco/Umbraco.Cms.Integrations/blob/main/src/Umbraco.Cms.Integrations.Automation.Zapier</PackageProjectUrl>
1111
<Product>Umbraco.Cms.Integrations.Automation.Zapier</Product>
12-
<Version>1.2.2</Version>
12+
<Version>1.2.3</Version>
1313
<PackageTags>Umbraco;Umbraco-Marketplace</PackageTags>
1414
<PackageIcon>zapier.png</PackageIcon>
1515
<PackageReadmeFile>readme.md</PackageReadmeFile>

0 commit comments

Comments
 (0)