mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-02-09 09:16:41 +08:00
Tokenizing Taxonomy form field text and value.
This commit is contained in:
@@ -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":
|
||||||
|
|||||||
@@ -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; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user