-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
UmbracoDictionaryDataListSource.cs
105 lines (90 loc) · 3.78 KB
/
UmbracoDictionaryDataListSource.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
/* Copyright © 2020 Lee Kelleher.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Newtonsoft.Json.Linq;
#if NET472
using Umbraco.Core;
using Umbraco.Core.IO;
using Umbraco.Core.Models;
using Umbraco.Core.PropertyEditors;
using Umbraco.Core.Services;
#else
using Umbraco.Cms.Core.IO;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.PropertyEditors;
using Umbraco.Cms.Core.Services;
using Umbraco.Extensions;
#endif
namespace Umbraco.Community.Contentment.DataEditors
{
public sealed class UmbracoDictionaryDataListSource : IDataListSource
{
private readonly ILocalizationService _localizationService;
private readonly IIOHelper _ioHelper;
public UmbracoDictionaryDataListSource(
ILocalizationService localizationService,
IIOHelper ioHelper)
{
_localizationService = localizationService;
_ioHelper = ioHelper;
}
public string Name => "Umbraco Dictionary Items";
public string Description => "Select an Umbraco dictionary item to populate the data source with its child items.";
public string Icon => "icon-book-alt";
public string Group => Constants.Conventions.DataSourceGroups.Umbraco;
public IEnumerable<ConfigurationField> Fields => new[]
{
new ConfigurationField
{
Key = "item",
Name = "Dictionary item",
Description = "Select a parent dictionary item to display the child items.",
View = _ioHelper.ResolveRelativeOrVirtualUrl(DictionaryPickerDataEditor.DataEditorViewPath),
Config = new Dictionary<string, object>
{
{ MaxItemsConfigurationField.MaxItems, 1 }
}
}
};
public Dictionary<string, object> DefaultValues => default;
public OverlaySize OverlaySize => OverlaySize.Small;
public IEnumerable<DataListItem> GetItems(Dictionary<string, object> config)
{
if (config.TryGetValueAs("item", out JArray array) == true &&
array.Count > 0 &&
array[0] is JObject dictItem)
{
var parent = default(IDictionaryItem);
if (dictItem.Value<string>("key") is string guid && Guid.TryParse(guid, out var key) == true && key.Equals(Guid.Empty) == false)
{
parent = _localizationService.GetDictionaryItemById(key);
}
else if (dictItem.Value<int>("id") is int id && id > 0)
{
// NOTE: Fallback on the `int` ID (for backwards-compatibility)
parent = _localizationService.GetDictionaryItemById(id);
}
if (parent != null)
{
var cultureName = CultureInfo.CurrentCulture.Name;
return _localizationService
.GetDictionaryItemChildren(parent.Key)
.OrderBy(x => x.ItemKey)
.Select(x => new DataListItem
{
Name = x.Translations.FirstOrDefault(t => t.Language.IsoCode.InvariantEquals(cultureName) == true || t.Language.IsDefault == true)?.Value ?? x.ItemKey,
Value = x.ItemKey,
Icon = this.Icon,
Description = x.ItemKey
});
}
}
return Enumerable.Empty<DataListItem>();
}
}
}