Skip to content

Commit a033f14

Browse files
committed
Update value converter and mark MultiUrls as obsolete
1 parent f729e22 commit a033f14

File tree

2 files changed

+53
-18
lines changed

2 files changed

+53
-18
lines changed

src/RJP.MultiUrlPicker/Models/MultiUrls.cs

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,51 @@
55
using Newtonsoft.Json.Linq;
66

77
using Umbraco.Core.Logging;
8+
using System;
89

910
namespace RJP.MultiUrlPicker.Models
1011
{
12+
[Obsolete("Use IEnumerable<Link> instead")]
1113
public class MultiUrls : IEnumerable<Link>
1214
{
1315
private readonly string _propertyData;
1416
private readonly List<Link> _multiUrls = new List<Link>();
1517

18+
internal MultiUrls()
19+
{
20+
}
21+
22+
internal MultiUrls(JArray propertyData)
23+
{
24+
_propertyData = propertyData.ToString();
25+
26+
Initialize(propertyData);
27+
}
28+
1629
public MultiUrls(string propertyData)
1730
{
1831
_propertyData = propertyData;
1932

2033
if (!string.IsNullOrEmpty(propertyData))
2134
{
2235
var relatedLinks = JsonConvert.DeserializeObject<JArray>(propertyData);
23-
foreach (var item in relatedLinks)
36+
Initialize(relatedLinks);
37+
}
38+
}
39+
40+
private void Initialize(JArray data)
41+
{
42+
foreach (var item in data)
43+
{
44+
var newLink = new Link(item);
45+
if (!newLink.Deleted)
46+
{
47+
_multiUrls.Add(new Link(item));
48+
}
49+
else
2450
{
25-
var newLink = new Link(item);
26-
if (!newLink.Deleted)
27-
{
28-
_multiUrls.Add(new Link(item));
29-
}
30-
else
31-
{
32-
LogHelper.Warn<MultiUrls>(
33-
string.Format("MultiUrlPicker value converter skipped a link as the node has been upublished/deleted (Id: {0}), ", newLink.Id));
34-
}
51+
LogHelper.Warn<MultiUrls>(
52+
string.Format("MultiUrlPicker value converter skipped a link as the node has been upublished/deleted (Id: {0}), ", newLink.Id));
3553
}
3654
}
3755
}
Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,42 @@
1-
using Umbraco.Core.Models.PublishedContent;
1+
using System.Collections.Generic;
2+
3+
using Newtonsoft.Json.Linq;
4+
5+
using Umbraco.Core.Models.PublishedContent;
26
using Umbraco.Core.PropertyEditors;
3-
using Umbraco.Web;
47

58
using RJP.MultiUrlPicker.Models;
69

710
namespace RJP.MultiUrlPicker
811
{
9-
[PropertyValueType(typeof(MultiUrls))]
10-
[PropertyValueCache(PropertyCacheValue.All, PropertyCacheLevel.Content)]
12+
[PropertyValueType(typeof(IEnumerable<Link>))]
13+
[PropertyValueCache(PropertyCacheValue.Source, PropertyCacheLevel.Content)]
14+
[PropertyValueCache(PropertyCacheValue.Object, PropertyCacheLevel.ContentCache)]
15+
[PropertyValueCache(PropertyCacheValue.XPath, PropertyCacheLevel.ContentCache)]
1116
public class MultiUrlPickerValueConverter : PropertyValueConverterBase
1217
{
1318
public override bool IsConverter(PublishedPropertyType propertyType)
1419
{
1520
return propertyType.PropertyEditorAlias.Equals("RJP.MultiUrlPicker");
1621
}
22+
1723
public override object ConvertDataToSource(PublishedPropertyType propertyType, object source, bool preview)
1824
{
19-
if (source == null) return null;
20-
var sourceString = source.ToString();
25+
if (source == null)
26+
{
27+
return null;
28+
}
29+
return JArray.Parse(source.ToString());
30+
}
31+
32+
public override object ConvertSourceToObject(PublishedPropertyType propertyType, object source, bool preview)
33+
{
34+
if (source == null)
35+
{
36+
return new MultiUrls();
37+
}
2138

22-
return UmbracoContext.Current != null ? new MultiUrls(sourceString) : null;
39+
return new MultiUrls((JArray)source);
2340
}
2441
}
2542
}

0 commit comments

Comments
 (0)