Last active
April 28, 2021 09:35
-
-
Save leekelleher/6bf49c3bd62ff696a29af404f248ffd2 to your computer and use it in GitHub Desktop.
Contentment: Umbraco Members data-source
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Copyright © 2021 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.Linq; | |
using Newtonsoft.Json.Linq; | |
using Umbraco.Core; | |
using Umbraco.Core.IO; | |
using Umbraco.Core.Models.PublishedContent; | |
using Umbraco.Core.PropertyEditors; | |
using Umbraco.Core.Services; | |
using Umbraco.Web.PublishedCache; | |
using UmbConstants = Umbraco.Core.Constants; | |
namespace Umbraco.Community.Contentment.DataEditors | |
{ | |
public sealed class UmbracoMembersDataListSource : IDataListSource, IDataListSourceValueConverter | |
{ | |
private readonly IMemberTypeService _memberTypeService; | |
private readonly IMemberService _memberService; | |
private readonly IPublishedSnapshotAccessor _publishedSnapshotAccessor; | |
public UmbracoMembersDataListSource(IMemberTypeService memberTypeService, IMemberService memberService, IPublishedSnapshotAccessor publishedSnapshotAccessor) | |
{ | |
_memberTypeService = memberTypeService; | |
_memberService = memberService; | |
_publishedSnapshotAccessor = publishedSnapshotAccessor; | |
} | |
public string Name => "Umbraco Members"; | |
public string Description => "Populate a data source with Umbraco members."; | |
public string Icon => UmbConstants.Icons.Member; | |
public IEnumerable<ConfigurationField> Fields | |
{ | |
get | |
{ | |
var items = _memberTypeService | |
.GetAll() | |
.Select(x => new DataListItem | |
{ | |
Icon = x.Icon, | |
Description = x.Description, | |
Name = x.Name, | |
Value = Udi.Create(UmbConstants.UdiEntityType.MemberType, x.Key).ToString(), | |
}) | |
.ToList(); | |
return new ConfigurationField[] | |
{ | |
new ConfigurationField | |
{ | |
Key = "memberType", | |
Name = "Member Type", | |
Description = "Select a member type to filter the members by. If left empty, all members will be used.", | |
View = "/App_Plugins/Contentment/editors/item-picker.html", | |
Config = new Dictionary<string, object> | |
{ | |
{ "addButtonLabelKey", "defaultdialogs_selectMemberType" }, | |
{ "enableFilter", items.Count > 5 ? "1" : "0" }, | |
{ "items", items }, | |
{ "listType", "list" }, | |
{ "overlayView", IOHelper.ResolveUrl("/App_Plugins/Contentment/editors/item-picker.overlay.html") }, | |
{ "maxItems", 1 }, | |
} | |
} | |
}; | |
} | |
} | |
public Dictionary<string, object> DefaultValues => null; | |
public OverlaySize OverlaySize => OverlaySize.Small; | |
public IEnumerable<DataListItem> GetItems(Dictionary<string, object> config) | |
{ | |
if (config.TryGetValue("memberType", out var obj) == true && | |
obj is JArray array && | |
array.Count > 0 && | |
array[0].Value<string>() is string str && | |
string.IsNullOrWhiteSpace(str) == false && | |
GuidUdi.TryParse(str, out var udi) == true) | |
{ | |
var memberType = _memberTypeService.Get(udi.Guid); | |
if (memberType != null) | |
{ | |
return _memberService | |
.GetMembersByMemberType(memberType.Id) | |
.Select(x => new DataListItem | |
{ | |
Name = x.Name, | |
Value = Udi.Create(UmbConstants.UdiEntityType.Member, x.Key).ToString(), | |
Icon = memberType.Icon ?? UmbConstants.Icons.Member, | |
Description = Udi.Create(UmbConstants.UdiEntityType.Member, x.Key).ToString() | |
}); | |
} | |
} | |
else | |
{ | |
return _memberService | |
.GetAllMembers() | |
.Select(x => new DataListItem | |
{ | |
Name = x.Name, | |
Value = Udi.Create(UmbConstants.UdiEntityType.Member, x.Key).ToString(), | |
Icon = UmbConstants.Icons.Member, | |
Description = Udi.Create(UmbConstants.UdiEntityType.Member, x.Key).ToString() | |
}); | |
} | |
return Enumerable.Empty<DataListItem>(); | |
} | |
public Type GetValueType(Dictionary<string, object> config) => typeof(IPublishedContent); | |
public object ConvertValue(Type type, string value) | |
{ | |
return GuidUdi.TryParse(value, out var udi) == true && udi.Guid.Equals(Guid.Empty) == false | |
? _publishedSnapshotAccessor.PublishedSnapshot.Members.GetByProviderKey(udi.Guid) | |
: default; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment