-
Notifications
You must be signed in to change notification settings - Fork 30
/
GMapsPropertyValueConverter.cs
48 lines (41 loc) · 1.49 KB
/
GMapsPropertyValueConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Our.Umbraco.GMaps.Models;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Core.PropertyEditors;
namespace Our.Umbraco.GMaps.PropertyValueConverter
{
public class GMapsPropertyValueConverter : PropertyValueConverterBase
{
public override bool IsConverter(IPublishedPropertyType propertyType) => propertyType.EditorAlias.Equals("Our.Umbraco.GMaps");
public override Type GetPropertyValueType(IPublishedPropertyType propertyType) => typeof(GMapsModel);
public override PropertyCacheLevel GetPropertyCacheLevel(IPublishedPropertyType propertyType) => PropertyCacheLevel.Element;
public override object ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object inter, bool preview)
{
if (inter != null)
{
var model = JsonConvert.DeserializeObject<GMapsModel>(inter.ToString());
if (model.Address == null)
{
// Ensure address is not null
model.Address = new GMapsAddress();
}
if (model.MapConfig == null)
{
// Ensure map config is not null
model.MapConfig = new GMapsMapConfig();
}
// Get API key from configuration
var config = propertyType.DataType.ConfigurationAs<IDictionary<string, object>>();
if (config != null &&
config.TryGetValue("apikey", out var apiKey))
{
model.MapConfig.ApiKey = apiKey?.ToString();
}
return model;
}
return null;
}
}
}