Forked from leekelleher/PropertyDataDataSource.cs
Last active
September 8, 2022 09:20
-
-
Save greystate/116da516f39a8675d27edfc063db5db0 to your computer and use it in GitHub Desktop.
This file contains 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
using System.Collections.Generic; | |
using System.Linq; | |
using Umbraco.Community.Contentment.DataEditors; | |
using Umbraco.Core.PropertyEditors; | |
using Umbraco.Web; | |
namespace MyWebsite.DataSources | |
{ | |
public class PropertyDataDataSource : IDataListSource | |
{ | |
private readonly IUmbracoContextAccessor _umbracoContextAccessor; | |
public PropertyDataDataSource(IUmbracoContextAccessor umbracoContextAccessor) | |
{ | |
_umbracoContextAccessor = umbracoContextAccessor; | |
} | |
public string Name => "Umbraco Property Data"; | |
public string Description => "Data source for umbraco property data coming from the website root node."; | |
public string Icon => "icon-globe"; | |
public string Group => nameof(Umbraco); | |
public OverlaySize OverlaySize => OverlaySize.Small; | |
public Dictionary<string, object> DefaultValues => new Dictionary<string, object>(); | |
public IEnumerable<ConfigurationField> Fields => new ConfigurationField[] | |
{ | |
new ConfigurationField | |
{ | |
Key = "propAlias", | |
Name = "Property", | |
Description = "Select the property to populate the data source with.", | |
View = "~/umbraco/views/propertyeditors/entitypicker/entitypicker.html", | |
Config = new Dictionary<string, object> | |
{ | |
{ "multiple", "0" }, | |
{ "entityType", "PropertyType" }, | |
{ "publishBy", "alias" }, | |
} | |
} | |
}; | |
public IEnumerable<DataListItem> GetItems(Dictionary<string, object> config) | |
{ | |
if (config.TryGetValue("propAlias", out var tmp) && tmp is string alias && string.IsNullOrWhiteSpace(alias) == false) | |
{ | |
var content = _umbracoContextAccessor.UmbracoContext.Content.GetAtRoot().First(); | |
if (content != null) | |
{ | |
var propertyValue = content.Value<string[]>(alias); | |
if (propertyValue != null) | |
{ | |
return propertyValue.Select(x => new DataListItem { Name = x, Value = x }); | |
} | |
} | |
} | |
return Enumerable.Empty<DataListItem>(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment