Skip to content

Instantly share code, notes, and snippets.

@leekelleher
Forked from TimGeyssens/PropertyDataDataSource.cs
Last active November 15, 2022 11:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save leekelleher/d786f4b72e7c16f37fb22d4d23c1b516 to your computer and use it in GitHub Desktop.
Save leekelleher/d786f4b72e7c16f37fb22d4d23c1b516 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 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>();
}
}
}
@LottePitcher
Copy link

Thanks @leekelleher as of course I just needed the umbraco/ in the view path. Good job @greystate isn't paying me for this, otherwise that might have been embarrassing 😆

I still get an error but I now see that's from the Config dictionary field property (as lifted from v8), so I can make progress with investigating that now. I think I was thinking this might be a problem caused by RCLs or something... But clearly not! 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment