Created
January 16, 2014 11:13
-
-
Save leekelleher/8453227 to your computer and use it in GitHub Desktop.
Prototype for an Umbraco "XPath DropDownList" property editor.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} | |
} |
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
I used an
internal
class for theXPathDropDownListPreValueEditor
only for development (I'd noticed that theSliderPropertyEditor
does the same). Going forwards, I'd make it generic so that other XPath lists can re-use it.