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>(); | |
} | |
} | |
} |
@greystate I haven't tried this one out since Tim posted his article about it, which at the time was only for Umbraco 8.
With v9/v10, it probably needs the IIOHelper
to be injected so it can map the path correctly.
(I appreciate this sounds like one of those "oh, just inject this thing) π€¦
Here's an example of this for the Examine data-source: https://github.com/leekelleher/umbraco-contentment/blob/4.2.0/src/Umbraco.Community.Contentment/DataEditors/DataList/DataSources/EnumDataListSource.cs#L82
I may get around to adding this data-source to Contentment some day soon... I love the idea of it, but haven't felt right about the configuration options, kinda feels that it should be a wizard interface... e.g. Step 1: pick a node, Step 2: select a property - I dunno, maybe I'm over-thinking it? π€
Thanks Lee!
In the mean time, I've put one of my best field agents on the case, and I'm sure she'll track you down π
You're right about the wizardry-thing; the way I'm finding the node is not very "discoverable"... (buried deep down in this code file).
In the meantime, you could use what Tim originally did and use "textstring"
?
https://gist.github.com/TimGeyssens/5e9e156d66c3d85d0bfb24a1ae9a7504#file-propertydatadatasource-cs-L30
Pretty sure I tried that too (just so see if I was totally crazy) - but I'll give it a go :)
Hey @leekelleher (field agent Pitcher checking in here π)
Thanks for the suggestion, hope we've got this working now using the textstring
view. So yes you have to type the alias of the property to use, but as you (very helpfully) show the editor preview in the backoffice it's pretty obvious if you've got the alias correct!
For the record... if we were wanting to use a Contentment view then you were correct in that
View = "~/App_Plugins/Contentment/editors/dictionary-picker.html",
was fixed by changing it to
View = _ioHelper.ResolveRelativeOrVirtualUrl("~/App_Plugins/Contentment/editors/dictionary-picker.html"),
However couldn't get it to work for a 'core' picker - specifically views/propertyeditors/entitypicker/entitypicker.html
as Chriztian was hoping. If I actually copied the core file into wwwroot then it found the file but raised a different error. I mean copying core files into wwwroot isn't a good idea, I was just playing around...
Anyway as I say we have got a working solution so all good! But if you have any other thoughts then me and my laptop will be at the hackathon on Thursday if you are, just saying... π
Agent Pitcher, we meet again! π΅οΈββοΈ // @LottePitcher @greystate
Looking at how I've done this elsewhere in Contentment, here's an example:
https://github.com/leekelleher/umbraco-contentment/blob/4.2.0/src/Umbraco.Community.Contentment/DataEditors/SocialLinks/SocialLinksConfigurationEditor.cs#L103
Would this not work for you? π€
View = _ioHelper.ResolveRelativeOrVirtualUrl("~/umbraco/views/propertyeditors/entitypicker/entitypicker.html"),
Previously, (e.g. v8) you could omit the ~/umbraco/
bit, as that was already in the context the "umbraco" directory, but in this .NET Core world, the paths are somewhat different. I'm not yet sure what the context of the "root" directory is, e.g. Content directory or the Web (wwwroot) directory. π€· That's why I use the ResolveRelativeOrVirtualUrl
extension method to sort it out for me. π¬
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! π
@leekelleher Any clues about how to get this working in Umbraco 10?
I can't seem to get the
ConfigurationField
'sView
to load, regardless of what I've tried (even pointing to one of Contentment's views brings up a 404, even though they're definitely there physically).