This repository has been archived by the owner on Dec 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
UmbracoDictionaryAttribute.cs
77 lines (69 loc) · 2.5 KB
/
UmbracoDictionaryAttribute.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
using System;
using System.Diagnostics.CodeAnalysis;
using Umbraco.Core.Models;
using Umbraco.Web;
namespace Our.Umbraco.Ditto
{
/// <summary>
/// The Umbraco dictionary value processor attribute.
/// Used for providing Umbraco with additional information about a dictionary item to aid property value processing.
/// </summary>
[DittoProcessorMetaData(ValueType = typeof(IPublishedContent))]
public class UmbracoDictionaryAttribute : DittoProcessorAttribute
{
/// <summary>
/// Initializes a new instance of the <see cref="UmbracoDictionaryAttribute" /> class.
/// </summary>
public UmbracoDictionaryAttribute()
{ }
/// <summary>
/// Initializes a new instance of the <see cref="UmbracoDictionaryAttribute" /> class.
/// </summary>
/// <param name="dictionaryKey">The dictionary key.</param>
public UmbracoDictionaryAttribute(string dictionaryKey)
{
this.DictionaryKey = dictionaryKey;
}
/// <summary>
/// Gets or sets the dictionary key.
/// </summary>
public string DictionaryKey { get; set; }
/// <summary>
/// Processes the value.
/// </summary>
/// <returns>
/// The <see cref="object" /> representing the processed value.
/// </returns>
public override object ProcessValue()
{
var dictionaryKey = this.DictionaryKey
?? (this.Context.PropertyDescriptor != null ? this.Context.PropertyDescriptor.Name : string.Empty);
if (string.IsNullOrWhiteSpace(dictionaryKey))
{
return null;
}
var content = this.Value as IPublishedContent;
if (content == null)
{
return null;
}
return UmbracoDictionaryHelper.GetValue(dictionaryKey);
}
}
/// <summary>
/// An Umbraco dictionary.
/// </summary>
[SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1401:FieldsMustBePrivate", Justification = "Reviewed.")]
internal static class UmbracoDictionaryHelper
{
/// <summary>
/// Gets the value from the Umbraco dictionary using a <c>key</c>.
/// </summary>
internal static Func<string, string> GetValue = (key) =>
{
return UmbracoContext.Current != null
? new UmbracoHelper(UmbracoContext.Current).GetDictionaryValue(key)
: null;
};
}
}