Tokenizing Taxonomy form field text and value.

This commit is contained in:
Sipke Schoorstra
2014-10-16 20:12:49 -07:00
parent 3cb9761823
commit 9ebb3966d3
2 changed files with 39 additions and 20 deletions

View File

@@ -8,8 +8,8 @@ using Orchard.Environment.Extensions;
using Orchard.Forms.Services; using Orchard.Forms.Services;
using Orchard.Layouts.Framework.Display; using Orchard.Layouts.Framework.Display;
using Orchard.Layouts.Framework.Drivers; using Orchard.Layouts.Framework.Drivers;
using Orchard.Taxonomies.Models;
using Orchard.Taxonomies.Services; using Orchard.Taxonomies.Services;
using Orchard.Tokens;
using Orchard.Utility.Extensions; using Orchard.Utility.Extensions;
using DescribeContext = Orchard.Forms.Services.DescribeContext; using DescribeContext = Orchard.Forms.Services.DescribeContext;
@@ -17,10 +17,12 @@ namespace Orchard.DynamicForms.Drivers {
[OrchardFeature("Orchard.DynamicForms.Taxonomies")] [OrchardFeature("Orchard.DynamicForms.Taxonomies")]
public class TaxonomyDriver : FormsElementDriver<Taxonomy> { public class TaxonomyDriver : FormsElementDriver<Taxonomy> {
private readonly ITaxonomyService _taxonomyService; private readonly ITaxonomyService _taxonomyService;
private readonly ITokenizer _tokenizer;
public TaxonomyDriver(IFormManager formManager, ITaxonomyService taxonomyService) public TaxonomyDriver(IFormManager formManager, ITaxonomyService taxonomyService, ITokenizer tokenizer)
: base(formManager) { : base(formManager) {
_taxonomyService = taxonomyService; _taxonomyService = taxonomyService;
_tokenizer = tokenizer;
} }
protected override IEnumerable<string> FormNames { protected override IEnumerable<string> FormNames {
@@ -50,20 +52,20 @@ namespace Orchard.DynamicForms.Drivers {
Name: "SortOrder", Name: "SortOrder",
Title: "Sort Order", Title: "Sort Order",
Description: T("The sort order to use when presenting the term values.")), Description: T("The sort order to use when presenting the term values.")),
_ValueType_TermText: shape.Radio( _TextExpression: shape.Textbox(
Id: "ValueType_TermText", Id: "TextExpression",
Name: "ValueType", Name: "TextExpression",
Title: "Use Term Name", Title: "Text Expression",
Checked: true, Value: "{Content.DisplayText}",
Value: "TermText", Description: T("Specify the expression to get the display text of each option."),
Description: T("Select this option to use the term name as the option value.")), Classes: new[] { "text", "large", "tokenized" }),
_ValueType_TermId: shape.Radio( _ValueExpression: shape.Textbox(
Id: "ValueType_TermId", Id: "ValueExpression",
Name: "ValueType", Name: "ValueExpression",
Title: "Use Term ID", Title: "Value Expression",
Checked: false, Value: "{Content.Id}",
Value: "TermId", Description: T("Specify the expression to get the value of each option."),
Description: T("Select this option to use the term ID as the option value.")), Classes: new[] { "text", "large", "tokenized" }),
_InputType: shape.SelectList( _InputType: shape.SelectList(
Id: "InputType", Id: "InputType",
Name: "InputType", Name: "InputType",
@@ -113,8 +115,19 @@ namespace Orchard.DynamicForms.Drivers {
yield break; yield break;
var terms = _taxonomyService.GetTerms(taxonomyId.Value); var terms = _taxonomyService.GetTerms(taxonomyId.Value);
var valueAccessor = element.UseTermId ? (Func<TermPart, string>)(x => x.Id.ToString()) : (x => x.Name); var valueExpression = !String.IsNullOrWhiteSpace(element.ValueExpression) ? element.ValueExpression : "{Content.Id}";
var projection = terms.Select(x => new SelectListItem {Text = x.Name, Value = valueAccessor(x)}); var textExpression = !String.IsNullOrWhiteSpace(element.TextExpression) ? element.TextExpression : "{Content.DisplayText}";
var projection = terms.Select(x => {
var data = new {Content = x};
var value = _tokenizer.Replace(valueExpression, data);
var text = _tokenizer.Replace(textExpression, data);
return new SelectListItem {
Text = text,
Value = value
};
});
switch (element.SortOrder) { switch (element.SortOrder) {
case "Asc": case "Asc":

View File

@@ -23,8 +23,14 @@ namespace Orchard.DynamicForms.Elements {
set { State["OptionLabel"] = value; } set { State["OptionLabel"] = value; }
} }
public bool UseTermId { public string TextExpression {
get { return State.Get("ValueType") == "TermId"; } get { return State.Get("TextExpression"); }
set { State["TextExpression"] = value; }
}
public string ValueExpression {
get { return State.Get("ValueExpression"); }
set { State["ValueExpression"] = value; }
} }
} }
} }