Skip to content

Instantly share code, notes, and snippets.

@leekelleher
Created January 16, 2014 11:13
Show Gist options
  • Select an option

  • Save leekelleher/8453227 to your computer and use it in GitHub Desktop.

Select an option

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
Copy Markdown
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