From b160dc731d3d825d5abb1aff38d0d4cd5a102aec Mon Sep 17 00:00:00 2001 From: jtkech Date: Thu, 27 Oct 2016 22:15:47 +0200 Subject: [PATCH] Allow the initial value of some fields to be tokenized (#6613) Fixes #6613 Fixes #6237 --- .../Drivers/EmailFieldElementDriver.cs | 10 +++++++--- .../Drivers/EnumerationElementDriver.cs | 15 ++++++++++++++- .../Drivers/HiddenFieldElementDriver.cs | 10 +++++++--- .../Drivers/QueryElementDriver.cs | 13 +++++++++++++ .../Drivers/TaxonomyElementDriver.cs | 13 +++++++++++++ .../Drivers/TextAreaElementDriver.cs | 10 +++++++--- .../Drivers/TextFieldElementDriver.cs | 10 +++++++--- .../Drivers/UrlFieldElementDriver.cs | 10 +++++++--- .../Orchard.DynamicForms/Elements/Enumeration.cs | 4 ++++ .../Orchard.DynamicForms/Elements/Query.cs | 4 ++++ .../Orchard.DynamicForms/Elements/Taxonomy.cs | 4 ++++ 11 files changed, 87 insertions(+), 16 deletions(-) diff --git a/src/Orchard.Web/Modules/Orchard.DynamicForms/Drivers/EmailFieldElementDriver.cs b/src/Orchard.Web/Modules/Orchard.DynamicForms/Drivers/EmailFieldElementDriver.cs index d86b80a35..91c9da709 100644 --- a/src/Orchard.Web/Modules/Orchard.DynamicForms/Drivers/EmailFieldElementDriver.cs +++ b/src/Orchard.Web/Modules/Orchard.DynamicForms/Drivers/EmailFieldElementDriver.cs @@ -77,9 +77,13 @@ namespace Orchard.DynamicForms.Drivers { } protected override void OnDisplaying(EmailField element, ElementDisplayingContext context) { - context.ElementShape.ProcessedName = _tokenizer.Replace(element.Name, context.GetTokenData()); - context.ElementShape.ProcessedLabel = _tokenizer.Replace(element.Label, context.GetTokenData(), new ReplaceOptions { Encoding = ReplaceOptions.NoEncode }); - context.ElementShape.ProcessedValue = element.RuntimeValue; + var tokenData = context.GetTokenData(); + context.ElementShape.ProcessedName = _tokenizer.Replace(element.Name, tokenData); + context.ElementShape.ProcessedLabel = _tokenizer.Replace(element.Label, tokenData, new ReplaceOptions { Encoding = ReplaceOptions.NoEncode }); + + // Allow the initial value to be tokenized. + // If a value was posted, use that value instead (without tokenizing it). + context.ElementShape.ProcessedValue = element.PostedValue != null ? element.PostedValue : _tokenizer.Replace(element.RuntimeValue, tokenData, new ReplaceOptions { Encoding = ReplaceOptions.NoEncode }); } } } \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.DynamicForms/Drivers/EnumerationElementDriver.cs b/src/Orchard.Web/Modules/Orchard.DynamicForms/Drivers/EnumerationElementDriver.cs index 3e8fb619b..3e67340ea 100644 --- a/src/Orchard.Web/Modules/Orchard.DynamicForms/Drivers/EnumerationElementDriver.cs +++ b/src/Orchard.Web/Modules/Orchard.DynamicForms/Drivers/EnumerationElementDriver.cs @@ -41,7 +41,13 @@ namespace Orchard.DynamicForms.Drivers { Id: "InputType", Name: "InputType", Title: "Input Type", - Description: T("The control to render when presenting the list of options."))); + Description: T("The control to render when presenting the list of options.")), + _DefaultValue: shape.Textbox( + Id: "DefaultValue", + Name: "DefaultValue", + Title: "Default Value", + Classes: new[] { "text", "large", "tokenized" }, + Description: T("The default value of this enumeration field."))); form._InputType.Items.Add(new SelectListItem { Text = T("Select List").Text, Value = "SelectList" }); form._InputType.Items.Add(new SelectListItem { Text = T("Multi Select List").Text, Value = "MultiSelectList" }); @@ -83,6 +89,13 @@ namespace Orchard.DynamicForms.Drivers { var displayType = context.DisplayType; var tokenData = context.GetTokenData(); + // Allow the initially selected value to be tokenized. + // If a value was posted, use that value instead (without tokenizing it). + if (element.PostedValue == null) { + var defaultValue = _tokenizer.Replace(element.DefaultValue, tokenData, new ReplaceOptions { Encoding = ReplaceOptions.NoEncode }); + element.RuntimeValue = defaultValue; + } + context.ElementShape.ProcessedName = _tokenizer.Replace(element.Name, tokenData); context.ElementShape.ProcessedLabel = _tokenizer.Replace(element.Label, tokenData, new ReplaceOptions { Encoding = ReplaceOptions.NoEncode }); context.ElementShape.ProcessedOptions = _tokenizer.Replace(element.Options, tokenData, new ReplaceOptions { Encoding = ReplaceOptions.NoEncode }).ToArray(); diff --git a/src/Orchard.Web/Modules/Orchard.DynamicForms/Drivers/HiddenFieldElementDriver.cs b/src/Orchard.Web/Modules/Orchard.DynamicForms/Drivers/HiddenFieldElementDriver.cs index 62448c847..20f44324b 100644 --- a/src/Orchard.Web/Modules/Orchard.DynamicForms/Drivers/HiddenFieldElementDriver.cs +++ b/src/Orchard.Web/Modules/Orchard.DynamicForms/Drivers/HiddenFieldElementDriver.cs @@ -27,7 +27,7 @@ namespace Orchard.DynamicForms.Drivers { Id: "Value", Name: "Value", Title: "Value", - Classes: new[] { "text", "medium" }, + Classes: new[] { "text", "medium", "tokenized" }, Description: T("The value of this hidden field."))); return form; @@ -35,8 +35,12 @@ namespace Orchard.DynamicForms.Drivers { } protected override void OnDisplaying(HiddenField element, ElementDisplayingContext context) { - context.ElementShape.ProcessedName = _tokenizer.Replace(element.Name, context.GetTokenData()); - context.ElementShape.ProcessedValue = element.RuntimeValue ?? string.Empty; + var tokenData = context.GetTokenData(); + context.ElementShape.ProcessedName = _tokenizer.Replace(element.Name, tokenData); + + // Allow the initial value to be tokenized. + // If a value was posted, use that value instead (without tokenizing it). + context.ElementShape.ProcessedValue = element.PostedValue != null ? element.PostedValue : _tokenizer.Replace(element.RuntimeValue, tokenData, new ReplaceOptions { Encoding = ReplaceOptions.NoEncode }); } } } diff --git a/src/Orchard.Web/Modules/Orchard.DynamicForms/Drivers/QueryElementDriver.cs b/src/Orchard.Web/Modules/Orchard.DynamicForms/Drivers/QueryElementDriver.cs index 3960c87cc..83399063e 100644 --- a/src/Orchard.Web/Modules/Orchard.DynamicForms/Drivers/QueryElementDriver.cs +++ b/src/Orchard.Web/Modules/Orchard.DynamicForms/Drivers/QueryElementDriver.cs @@ -68,6 +68,12 @@ namespace Orchard.DynamicForms.Drivers { Value: "{Content.Id}", Description: T("Specify the expression to get the value of each option."), Classes: new[]{"text", "large", "tokenized"}), + _DefaultValue: shape.Textbox( + Id: "DefaultValue", + Name: "DefaultValue", + Title: "Default Value", + Classes: new[] { "text", "large", "tokenized" }, + Description: T("The default value of this query field.")), _InputType: shape.SelectList( Id: "InputType", Name: "InputType", @@ -122,6 +128,13 @@ namespace Orchard.DynamicForms.Drivers { var displayType = context.DisplayType; var tokenData = context.GetTokenData(); + // Allow the initially selected value to be tokenized. + // If a value was posted, use that value instead (without tokenizing it). + if (element.PostedValue == null) { + var defaultValue = _tokenizer.Replace(element.DefaultValue, tokenData, new ReplaceOptions { Encoding = ReplaceOptions.NoEncode }); + element.RuntimeValue = defaultValue; + } + context.ElementShape.ProcessedName = _tokenizer.Replace(element.Name, tokenData); context.ElementShape.ProcessedLabel = _tokenizer.Replace(element.Label, tokenData); context.ElementShape.Options = GetOptions(element, context.DisplayType, queryId, tokenData).ToArray(); diff --git a/src/Orchard.Web/Modules/Orchard.DynamicForms/Drivers/TaxonomyElementDriver.cs b/src/Orchard.Web/Modules/Orchard.DynamicForms/Drivers/TaxonomyElementDriver.cs index 59c4b7b9f..7e2b10701 100644 --- a/src/Orchard.Web/Modules/Orchard.DynamicForms/Drivers/TaxonomyElementDriver.cs +++ b/src/Orchard.Web/Modules/Orchard.DynamicForms/Drivers/TaxonomyElementDriver.cs @@ -67,6 +67,12 @@ namespace Orchard.DynamicForms.Drivers { Value: "{Content.Id}", Description: T("Specify the expression to get the value of each option."), Classes: new[] { "text", "large", "tokenized" }), + _DefaultValue: shape.Textbox( + Id: "DefaultValue", + Name: "DefaultValue", + Title: "Default Value", + Classes: new[] { "text", "large", "tokenized" }, + Description: T("The default value of this query field.")), _InputType: shape.SelectList( Id: "InputType", Name: "InputType", @@ -126,6 +132,13 @@ namespace Orchard.DynamicForms.Drivers { var displayType = context.DisplayType; var tokenData = context.GetTokenData(); + // Allow the initially selected value to be tokenized. + // If a value was posted, use that value instead (without tokenizing it). + if (element.PostedValue == null) { + var defaultValue = _tokenizer.Replace(element.DefaultValue, tokenData, new ReplaceOptions { Encoding = ReplaceOptions.NoEncode }); + element.RuntimeValue = defaultValue; + } + context.ElementShape.ProcessedName = _tokenizer.Replace(element.Name, tokenData); context.ElementShape.ProcessedLabel = _tokenizer.Replace(element.Label, tokenData, new ReplaceOptions { Encoding = ReplaceOptions.NoEncode }); context.ElementShape.TermOptions = GetTermOptions(element, context.DisplayType, taxonomyId, tokenData).ToArray(); diff --git a/src/Orchard.Web/Modules/Orchard.DynamicForms/Drivers/TextAreaElementDriver.cs b/src/Orchard.Web/Modules/Orchard.DynamicForms/Drivers/TextAreaElementDriver.cs index dedebedae..3dc880b8a 100644 --- a/src/Orchard.Web/Modules/Orchard.DynamicForms/Drivers/TextAreaElementDriver.cs +++ b/src/Orchard.Web/Modules/Orchard.DynamicForms/Drivers/TextAreaElementDriver.cs @@ -88,9 +88,13 @@ namespace Orchard.DynamicForms.Drivers { } protected override void OnDisplaying(TextArea element, ElementDisplayingContext context) { - context.ElementShape.ProcessedName = _tokenizer.Replace(element.Name, context.GetTokenData()); - context.ElementShape.ProcessedLabel = _tokenizer.Replace(element.Label, context.GetTokenData(), new ReplaceOptions { Encoding = ReplaceOptions.NoEncode }); - context.ElementShape.ProcessedValue = element.RuntimeValue; + var tokenData = context.GetTokenData(); + context.ElementShape.ProcessedName = _tokenizer.Replace(element.Name, tokenData); + context.ElementShape.ProcessedLabel = _tokenizer.Replace(element.Label, tokenData, new ReplaceOptions { Encoding = ReplaceOptions.NoEncode }); + + // Allow the initial value to be tokenized. + // If a value was posted, use that value instead (without tokenizing it). + context.ElementShape.ProcessedValue = element.PostedValue != null ? element.PostedValue : _tokenizer.Replace(element.RuntimeValue, tokenData, new ReplaceOptions { Encoding = ReplaceOptions.NoEncode }); } } } \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.DynamicForms/Drivers/TextFieldElementDriver.cs b/src/Orchard.Web/Modules/Orchard.DynamicForms/Drivers/TextFieldElementDriver.cs index 253ede4be..7176ad9b8 100644 --- a/src/Orchard.Web/Modules/Orchard.DynamicForms/Drivers/TextFieldElementDriver.cs +++ b/src/Orchard.Web/Modules/Orchard.DynamicForms/Drivers/TextFieldElementDriver.cs @@ -83,9 +83,13 @@ namespace Orchard.DynamicForms.Drivers { } protected override void OnDisplaying(TextField element, ElementDisplayingContext context) { - context.ElementShape.ProcessedName = _tokenizer.Replace(element.Name, context.GetTokenData()); - context.ElementShape.ProcessedLabel = _tokenizer.Replace(element.Label, context.GetTokenData(), new ReplaceOptions {Encoding = ReplaceOptions.NoEncode}); - context.ElementShape.ProcessedValue = element.RuntimeValue; + var tokenData = context.GetTokenData(); + context.ElementShape.ProcessedName = _tokenizer.Replace(element.Name, tokenData); + context.ElementShape.ProcessedLabel = _tokenizer.Replace(element.Label, tokenData, new ReplaceOptions { Encoding = ReplaceOptions.NoEncode }); + + // Allow the initial value to be tokenized. + // If a value was posted, use that value instead (without tokenizing it). + context.ElementShape.ProcessedValue = element.PostedValue != null ? element.PostedValue : _tokenizer.Replace(element.RuntimeValue, tokenData, new ReplaceOptions { Encoding = ReplaceOptions.NoEncode }); } } } \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.DynamicForms/Drivers/UrlFieldElementDriver.cs b/src/Orchard.Web/Modules/Orchard.DynamicForms/Drivers/UrlFieldElementDriver.cs index c6ddb4950..05ecc9c04 100644 --- a/src/Orchard.Web/Modules/Orchard.DynamicForms/Drivers/UrlFieldElementDriver.cs +++ b/src/Orchard.Web/Modules/Orchard.DynamicForms/Drivers/UrlFieldElementDriver.cs @@ -71,9 +71,13 @@ namespace Orchard.DynamicForms.Drivers { } protected override void OnDisplaying(UrlField element, ElementDisplayingContext context) { - context.ElementShape.ProcessedName = _tokenizer.Replace(element.Name, context.GetTokenData()); - context.ElementShape.ProcessedLabel = _tokenizer.Replace(element.Label, context.GetTokenData(), new ReplaceOptions { Encoding = ReplaceOptions.NoEncode }); - context.ElementShape.ProcessedValue = element.RuntimeValue; + var tokenData = context.GetTokenData(); + context.ElementShape.ProcessedName = _tokenizer.Replace(element.Name, tokenData); + context.ElementShape.ProcessedLabel = _tokenizer.Replace(element.Label, tokenData, new ReplaceOptions { Encoding = ReplaceOptions.NoEncode }); + + // Allow the initial value to be tokenized. + // If a value was posted, use that value instead (without tokenizing it). + context.ElementShape.ProcessedValue = element.PostedValue != null ? element.PostedValue : _tokenizer.Replace(element.RuntimeValue, tokenData, new ReplaceOptions { Encoding = ReplaceOptions.NoEncode }); } } } \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.DynamicForms/Elements/Enumeration.cs b/src/Orchard.Web/Modules/Orchard.DynamicForms/Elements/Enumeration.cs index 93c400b08..7118464ba 100644 --- a/src/Orchard.Web/Modules/Orchard.DynamicForms/Elements/Enumeration.cs +++ b/src/Orchard.Web/Modules/Orchard.DynamicForms/Elements/Enumeration.cs @@ -20,6 +20,10 @@ namespace Orchard.DynamicForms.Elements { get { return _options.Value; } } + public string DefaultValue { + get { return this.Retrieve(x => x.DefaultValue); } + } + public IEnumerable RuntimeValues { get { return _runtimeValues.Value; } } diff --git a/src/Orchard.Web/Modules/Orchard.DynamicForms/Elements/Query.cs b/src/Orchard.Web/Modules/Orchard.DynamicForms/Elements/Query.cs index 293c9e06a..200cf4556 100644 --- a/src/Orchard.Web/Modules/Orchard.DynamicForms/Elements/Query.cs +++ b/src/Orchard.Web/Modules/Orchard.DynamicForms/Elements/Query.cs @@ -27,6 +27,10 @@ namespace Orchard.DynamicForms.Elements { get { return this.Retrieve(x => x.ValueExpression, () => "{Content.Id}"); } } + public string DefaultValue { + get { return this.Retrieve(x => x.DefaultValue); } + } + public EnumerationValidationSettings ValidationSettings { get { return Data.GetModel(""); } } diff --git a/src/Orchard.Web/Modules/Orchard.DynamicForms/Elements/Taxonomy.cs b/src/Orchard.Web/Modules/Orchard.DynamicForms/Elements/Taxonomy.cs index 3da3359d5..b964df847 100644 --- a/src/Orchard.Web/Modules/Orchard.DynamicForms/Elements/Taxonomy.cs +++ b/src/Orchard.Web/Modules/Orchard.DynamicForms/Elements/Taxonomy.cs @@ -34,6 +34,10 @@ namespace Orchard.DynamicForms.Elements { set { this.Store(x => x.ValueExpression, value); } } + public string DefaultValue { + get { return this.Retrieve(x => x.DefaultValue); } + } + public EnumerationValidationSettings ValidationSettings { get { return Data.GetModel(""); } }