Skip to content

Commit 71d21a8

Browse files
committed
Return a single Link if max number of items is 1
This only applies to new data types, existing ones will continue to return IEnumerable<Link>
1 parent f35bb2e commit 71d21a8

File tree

6 files changed

+135
-8
lines changed

6 files changed

+135
-8
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
77

88
* Updated styling to match Umbraco v7.6
99
* Added Udi support
10+
* New data types with `max number of items` set to 1 will return a single `Link` or `null`.
11+
Existing data types will continue to return `IEnumerable<Link>`
1012

1113
### Breaking
1214

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,15 @@ If you're using the models builder, you can access the property on your model e.
3030
}
3131
```
3232

33+
If `Max number of items` is configured to 1
34+
35+
```csharp
36+
@if(Model.Link != null)
37+
{
38+
<a href="@Model.Link.Url" target="@Model.Link.Target">@Model.Link.Name</a>
39+
}
40+
41+
```
42+
3343
## Changelog
3444
See the [changelog here](CHANGELOG.md)

src/RJP.MultiUrlPicker/Information.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace RJP.MultiUrlPicker
2+
{
3+
using System;
4+
using System.Reflection;
5+
6+
internal static class Information
7+
{
8+
private static readonly Lazy<Version> _version;
9+
10+
static Information()
11+
{
12+
_version = new Lazy<Version>(() => Assembly.GetExecutingAssembly().GetName().Version);
13+
}
14+
15+
public static Version Version
16+
{
17+
get
18+
{
19+
return _version.Value;
20+
}
21+
}
22+
}
23+
}

src/RJP.MultiUrlPicker/MultiUrlPickerPropertyEditor.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,22 @@
2828
Group ="pickers", Icon = "icon-link", IsParameterEditor = true)]
2929
public class MultiUrlPickerPropertyEditor : PropertyEditor
3030
{
31+
private IDictionary<string, object> _defaultPreValues;
32+
33+
public MultiUrlPickerPropertyEditor()
34+
{
35+
_defaultPreValues = new Dictionary<string, object>
36+
{
37+
{"version", Information.Version.ToString(3)},
38+
};
39+
}
40+
41+
public override IDictionary<string, object> DefaultPreValues
42+
{
43+
get { return _defaultPreValues; }
44+
set { _defaultPreValues = value; }
45+
}
46+
3147
protected override PreValueEditor CreatePreValueEditor()
3248
{
3349
return new MultiUrlPickerPreValueEditor();
@@ -45,6 +61,20 @@ private class MultiUrlPickerPreValueEditor : PreValueEditor
4561

4662
[PreValueField("maxNumberOfItems", "Max number of items", "number")]
4763
public int? MaxNumberOfItems { get; set; }
64+
65+
[PreValueField("version", "Multi Url Picker version", "hidden", HideLabel = true)]
66+
public string Version { get; set; }
67+
68+
public override IDictionary<string, object> ConvertDbToEditor(IDictionary<string, object> defaultPreVals, PreValueCollection persistedPreVals)
69+
{
70+
// if there isn't a version stored set it to 0 for backwards compatibility
71+
if(!persistedPreVals.PreValuesAsDictionary.ContainsKey("version"))
72+
{
73+
persistedPreVals.PreValuesAsDictionary["version"] = new PreValue("0");
74+
}
75+
76+
return base.ConvertDbToEditor(defaultPreVals, persistedPreVals);
77+
}
4878
}
4979

5080
private class MultiUrlPickerPropertyValueEditor : PropertyValueEditorWrapper
Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
namespace RJP.MultiUrlPicker
22
{
3+
using System;
4+
using System.Linq;
35
using System.Collections.Generic;
46

57
using Newtonsoft.Json.Linq;
68

9+
using Umbraco.Core;
10+
using Umbraco.Core.Models;
711
using Umbraco.Core.Models.PublishedContent;
812
using Umbraco.Core.PropertyEditors;
13+
using Umbraco.Core.Services;
914

1015
using Models;
1116

12-
[PropertyValueType(typeof(IEnumerable<Link>))]
13-
[PropertyValueCache(PropertyCacheValue.Source, PropertyCacheLevel.Content)]
14-
[PropertyValueCache(PropertyCacheValue.Object, PropertyCacheLevel.ContentCache)]
15-
[PropertyValueCache(PropertyCacheValue.XPath, PropertyCacheLevel.ContentCache)]
16-
public class MultiUrlPickerValueConverter : PropertyValueConverterBase
17+
public class MultiUrlPickerValueConverter : PropertyValueConverterBase, IPropertyValueConverterMeta
1718
{
19+
private static int _maxNumberOfItems;
20+
1821
public override bool IsConverter(PublishedPropertyType propertyType)
1922
{
2023
return propertyType.PropertyEditorAlias.Equals("RJP.MultiUrlPicker");
2124
}
2225

2326
public override object ConvertDataToSource(PublishedPropertyType propertyType, object source, bool preview)
2427
{
25-
if (source == null)
28+
if (string.IsNullOrEmpty(source?.ToString()))
2629
{
2730
return null;
2831
}
@@ -31,12 +34,70 @@ public override object ConvertDataToSource(PublishedPropertyType propertyType, o
3134

3235
public override object ConvertSourceToObject(PublishedPropertyType propertyType, object source, bool preview)
3336
{
37+
bool isMultiple = IsMultipleDataType(propertyType.DataTypeId);
3438
if (source == null)
3539
{
36-
return new MultiUrls();
40+
return isMultiple ? new MultiUrls() : null;
41+
}
42+
43+
var urls = new MultiUrls((JArray)source);
44+
if(isMultiple)
45+
{
46+
if(_maxNumberOfItems > 0)
47+
{
48+
return urls.Take(_maxNumberOfItems);
49+
}
50+
return urls;
3751
}
52+
return urls.FirstOrDefault();
53+
}
54+
55+
public Type GetPropertyValueType(PublishedPropertyType propertyType)
56+
{
57+
if (IsMultipleDataType(propertyType.DataTypeId))
58+
{
59+
return typeof(IEnumerable<Link>);
60+
}
61+
return typeof(Link);
62+
}
3863

39-
return new MultiUrls((JArray)source);
64+
public PropertyCacheLevel GetPropertyCacheLevel(PublishedPropertyType propertyType, PropertyCacheValue cacheValue)
65+
{
66+
switch (cacheValue)
67+
{
68+
case PropertyCacheValue.Source:
69+
return PropertyCacheLevel.Content;
70+
case PropertyCacheValue.Object:
71+
case PropertyCacheValue.XPath:
72+
return PropertyCacheLevel.ContentCache;
73+
}
74+
75+
return PropertyCacheLevel.None;
76+
}
77+
78+
private static bool IsMultipleDataType(int dataTypeId)
79+
{
80+
IDataTypeService dataTypeService = ApplicationContext.Current.Services.DataTypeService;
81+
IDictionary<string, PreValue> preValues = dataTypeService
82+
.GetPreValuesCollectionByDataTypeId(dataTypeId)
83+
.PreValuesAsDictionary;
84+
85+
PreValue maxNumberOfItems;
86+
if (preValues.TryGetValue("maxNumberOfItems", out maxNumberOfItems) &&
87+
int.TryParse(maxNumberOfItems.Value, out _maxNumberOfItems))
88+
{
89+
PreValue versionPreValue;
90+
Version version;
91+
// for backwards compatibility, always return true if version
92+
// is less than 2.0.0
93+
if (preValues.TryGetValue("version", out versionPreValue) &&
94+
Version.TryParse(versionPreValue.Value, out version)
95+
&& version >= new Version(2, 0, 0))
96+
{
97+
return _maxNumberOfItems != 1;
98+
}
99+
}
100+
return true;
40101
}
41102
}
42103
}

src/RJP.MultiUrlPicker/RJP.MultiUrlPicker.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@
274274
</Reference>
275275
</ItemGroup>
276276
<ItemGroup>
277+
<Compile Include="Information.cs" />
277278
<Compile Include="Models\Link.cs" />
278279
<Compile Include="Models\LinkDisplay.cs" />
279280
<Compile Include="Models\LinkDto.cs" />

0 commit comments

Comments
 (0)