-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
YesNoValueConverter.cs
63 lines (50 loc) · 2.12 KB
/
YesNoValueConverter.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using Umbraco.Cms.Core.Models.PublishedContent;
namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters;
[DefaultPropertyValueConverter]
public class YesNoValueConverter : PropertyValueConverterBase
{
public override bool IsConverter(IPublishedPropertyType propertyType)
=> propertyType.EditorAlias == Constants.PropertyEditors.Aliases.Boolean;
public override Type GetPropertyValueType(IPublishedPropertyType propertyType)
=> typeof(bool);
public override PropertyCacheLevel GetPropertyCacheLevel(IPublishedPropertyType propertyType)
=> PropertyCacheLevel.Element;
public override object ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType, object? source, bool preview)
{
// in xml a boolean is: string
// in the database a boolean is: string "1" or "0" or empty
// typically the converter does not need to handle anything else ("true"...)
// however there are cases where the value passed to the converter could be a non-string object, e.g. int, bool
if (source is string s)
{
if (s.Length == 0 || s == "0")
{
return false;
}
if (s == "1")
{
return true;
}
return bool.TryParse(s, out var result) && result;
}
if (source is int)
{
return (int)source == 1;
}
// this is required for correct true/false handling in nested content elements
if (source is long)
{
return (long)source == 1;
}
if (source is bool)
{
return (bool)source;
}
// default value is: false
return false;
}
// default ConvertSourceToObject just returns source ie a boolean value
public override object ConvertIntermediateToXPath(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object? inter, bool preview) =>
// source should come from ConvertSource and be a boolean already
(bool?)inter ?? false ? "1" : "0";
}