Skip to content

Instantly share code, notes, and snippets.

@greystate
Forked from leekelleher/PropertyDataDataSource.cs
Last active September 8, 2022 09:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greystate/116da516f39a8675d27edfc063db5db0 to your computer and use it in GitHub Desktop.
Save greystate/116da516f39a8675d27edfc063db5db0 to your computer and use it in GitHub Desktop.
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