Skip to content

Instantly share code, notes, and snippets.

@leekelleher
Created January 16, 2014 11:13
Show Gist options
  • Save leekelleher/8453227 to your computer and use it in GitHub Desktop.
Save leekelleher/8453227 to your computer and use it in GitHub Desktop.
Prototype for an Umbraco "XPath DropDownList" property editor.
using System.Collections.Generic;
using System.Linq;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.PropertyEditors;
using Umbraco.Core.Xml;
namespace Umbraco.Web.PropertyEditors
{
[PropertyEditor(Constants.PropertyEditors.XPathDropDownListAlias, "XPath DropDownList", "dropdown", ValueType = "INT")]
public class XPathDropDownListPropertyEditor : DropDownWithKeysPropertyEditor
{
public override IDictionary<string, object> DefaultPreValues
{
get { return new Dictionary<string, object> { { "xpath", "//*[@isDoc]" } }; }
}
protected override PreValueEditor CreatePreValueEditor()
{
return new XPathDropDownListPreValueEditor();
}
internal class XPathDropDownListPreValueEditor : PreValueEditor
{
public XPathDropDownListPreValueEditor()
{
Fields.Add(new PreValueField
{
Key = "xpath",
Name = "XPath",
View = "textstring",
Description = "You can use the tokens `$ancestorOrSelf` and `$currentPage`",
HideLabel = false
});
}
public override IDictionary<string, object> ConvertDbToEditor(IDictionary<string, object> defaultPreVals, PreValueCollection persistedPreVals)
{
var config = base.ConvertDbToEditor(defaultPreVals, persistedPreVals);
var xpath = config.ContainsKey("xpath") ? config["xpath"].ToString() : string.Empty;
var vars = new[]
{
new XPathVariable("ancestorOrSelf", ""), // [LK] What to do here?
new XPathVariable("currentPage", "") // [LK] What to do here?
};
var helper = new UmbracoHelper(UmbracoContext.Current);
var items = helper.TypedContentAtXPath(xpath, vars);
if (items != null)
config.Add("items", items.ToDictionary(x => x.Id, x => x.Name));
return config;
}
}
}
}
@leekelleher
Copy link
Author

I used an internal class for the XPathDropDownListPreValueEditor only for development (I'd noticed that the SliderPropertyEditor does the same). Going forwards, I'd make it generic so that other XPath lists can re-use it.

@leekelleher
Copy link
Author

The specific part that I am looking for advice on... is overriding the ConvertDbToEditor method the best way to pre-populate the list of items?

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