-
Notifications
You must be signed in to change notification settings - Fork 13
/
MediaPickerPropertyConverter.cs
146 lines (134 loc) · 4.83 KB
/
MediaPickerPropertyConverter.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MediaPickerPropertyConverter.cs" company="OurUmbraco">
// Our.Umbraco
// </copyright>
// <summary>
// The legacy media picker value converter
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace Our.Umbraco.PropertyConverters
{
using System;
using Our.Umbraco.PropertyConverters.Utilities;
using global::Umbraco.Core;
using global::Umbraco.Core.Models;
using global::Umbraco.Core.Models.PublishedContent;
using global::Umbraco.Core.PropertyEditors;
using global::Umbraco.Web;
/// <summary>
/// The media picker property value converter.
/// </summary>
public class MediaPickerPropertyConverter : PropertyValueConverterBase, IPropertyValueConverterMeta
{
/// <summary>
/// Checks if this converter can convert the property editor and registers if it can.
/// </summary>
/// <param name="propertyType">
/// The published property type.
/// </param>
/// <returns>
/// The <see cref="bool"/>.
/// </returns>
public override bool IsConverter(PublishedPropertyType propertyType)
{
return propertyType.PropertyEditorAlias.Equals(Constants.PropertyEditors.MediaPickerAlias);
}
/// <summary>
/// Convert the raw string into a nodeId integer
/// </summary>
/// <param name="propertyType">
/// The published property type.
/// </param>
/// <param name="source">
/// The value of the property
/// </param>
/// <param name="preview">
/// The preview.
/// </param>
/// <returns>
/// The <see cref="object"/>.
/// </returns>
public override object ConvertDataToSource(PublishedPropertyType propertyType, object source, bool preview)
{
var attemptConvertInt = source.TryConvertTo<int>();
if (attemptConvertInt.Success)
{
return attemptConvertInt.Result;
}
return null;
}
/// <summary>
/// Convert the source nodeId into a IPublishedContent (or DynamicPublishedContent)
/// </summary>
/// <param name="propertyType">
/// The published property type.
/// </param>
/// <param name="source">
/// The value of the property
/// </param>
/// <param name="preview">
/// The preview.
/// </param>
/// <returns>
/// The <see cref="object"/>.
/// </returns>
public override object ConvertSourceToObject(PublishedPropertyType propertyType, object source, bool preview)
{
if (source == null)
{
return null;
}
if (UmbracoContext.Current != null)
{
var umbHelper = new UmbracoHelper(UmbracoContext.Current);
return ConverterHelper.DynamicInvocation() ? umbHelper.Media(source) : umbHelper.TypedMedia(source);
}
return null;
}
/// <summary>
/// The get property cache level.
/// </summary>
/// <param name="propertyType">
/// The property type.
/// </param>
/// <param name="cacheValue">
/// The cache value.
/// </param>
/// <returns>
/// The <see cref="PropertyCacheLevel"/>.
/// </returns>
public PropertyCacheLevel GetPropertyCacheLevel(PublishedPropertyType propertyType, PropertyCacheValue cacheValue)
{
PropertyCacheLevel returnLevel;
switch (cacheValue)
{
case PropertyCacheValue.Object:
returnLevel = ConverterHelper.ModeFixed() ? PropertyCacheLevel.ContentCache : PropertyCacheLevel.Request;
break;
case PropertyCacheValue.Source:
returnLevel = PropertyCacheLevel.Content;
break;
case PropertyCacheValue.XPath:
returnLevel = PropertyCacheLevel.Content;
break;
default:
returnLevel = PropertyCacheLevel.None;
break;
}
return returnLevel;
}
/// <summary>
/// The CLR type that the value converter returns.
/// </summary>
/// <param name="propertyType">
/// The property type.
/// </param>
/// <returns>
/// The <see cref="Type"/>.
/// </returns>
public Type GetPropertyValueType(PublishedPropertyType propertyType)
{
return typeof(IPublishedContent);
}
}
}