Instantly share code, notes, and snippets.
Last active
October 8, 2017 19:21
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save jammykam/2b24736a8586452bb7bc1e7ee70dc6ce to your computer and use it in GitHub Desktop.
Sitecore WFFM Hidden Token Field
This file contains 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.ComponentModel; | |
using System.Web.UI; | |
using Sitecore.Form.Core.Attributes; | |
using Sitecore.Form.Core.Visual; | |
using Sitecore.Form.Web.UI.Controls; | |
namespace MyProject.CMS.Custom.WFFM.Fields.HiddenTokenField | |
{ | |
public class HiddenToken : InputControl | |
{ | |
protected HiddenToken() | |
{ | |
} | |
protected HiddenToken(HtmlTextWriterTag tag) | |
: base(tag) | |
{ | |
} | |
// Override to hide fields in Form Editor | |
public new string CssClass { get; set; } | |
public new string Information { get; set; } | |
public new string Text { get; set; } | |
[VisualFieldType(typeof(ListChoiceField), new object[] { "{7FF10263-1505-48B3-9027-D26FF529D7F1}" })] | |
[VisualCategory("Appearance")] | |
[VisualProperty("Token Value:", 100)] | |
public virtual string TokenValue { get; set; } | |
[DefaultValue("")] | |
[VisualCategory("Appearance")] | |
[VisualProperty("Custom Value:", 200)] | |
public virtual string CustomTokenValue { get; set; } | |
// If we were using ASP.Net WebForms then we'd need to expand this | |
private string _value; | |
public virtual string Value | |
{ | |
get | |
{ | |
if (TokenValue == "{custom}" || TokenValue == "{fieldname}") | |
{ | |
return CustomTokenValue; | |
} | |
return TokenValue; | |
} | |
set | |
{ | |
this._value = value; | |
} | |
} | |
} | |
} |
This file contains 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.ComponentModel; | |
using Sitecore.Forms.Mvc.ViewModels; | |
using Sitecore.Forms.Mvc.Interfaces; | |
using Sitecore.Links; | |
using Sitecore.WFFM.Abstractions.Actions; | |
namespace MyProject.CMS.Custom.WFFM.Fields.HiddenTokenField | |
{ | |
public class HiddenTokenField : FieldViewModel, IFieldResult, IContainerMetadata | |
{ | |
public virtual string TokenValue { get; set; } | |
public virtual string CustomTokenValue { get; set; } | |
public string ResultParameters { get; set; } | |
[DefaultValue("")] | |
public virtual string Value { get; set; } | |
private string GetTokenValue() | |
{ | |
switch (TokenValue) | |
{ | |
case "{custom}": | |
return CustomTokenValue; | |
case "{fieldname}": | |
return Sitecore.Context.Item[CustomTokenValue].Replace("&", "and").Replace('?', '.'); | |
case "{pageurl}": | |
var options = UrlOptions.DefaultOptions; | |
options.AlwaysIncludeServerUrl = true; | |
return LinkManager.GetItemUrl(Sitecore.Context.Item, options); | |
case "{itempath}": | |
return Sitecore.Context.Item.Paths.FullPath; | |
case "{itemid}": | |
return Sitecore.Context.Item.ID.ToString(); | |
case "{shortid}": | |
return Sitecore.Context.Item.ID.ToShortID().ToString(); | |
default: | |
return TokenValue; | |
} | |
} | |
public virtual ControlResult GetResult() | |
{ | |
return new ControlResult(this.FieldItemId, this.Name, (object)this.Value, this.ResultParameters, false); | |
} | |
public override void Initialize() | |
{ | |
base.Initialize(); | |
Value = GetTokenValue(); | |
} | |
} | |
} |
This file contains 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
@model MyProject.CMS.Custom.WFFM.Fields.HiddenTokenField | |
@if (!Model.Visible) | |
{ | |
return; | |
} | |
@Html.HiddenFor(x => Model.FieldItemId) | |
@Html.HiddenFor(x => Model.Value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment