Created
September 21, 2019 07:03
-
-
Save sreekrishnan1993/07349616ee0ac3e2d159897cece5569f to your computer and use it in GitHub Desktop.
Sitecore Forms Hidden Field Binding
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
public class GatedAssetBindingHandler : IBindingHandler | |
{ | |
public IBindingHandlerResult GetBindingValue() | |
{ | |
var mediaID = HttpContext.Current.Request.QueryString["mediaid"]; | |
if (!string.IsNullOrEmpty(mediaID)) | |
{ | |
var mediaItem = Sitecore.Context.Database.GetItem(mediaID); | |
if (mediaItem != null && mediaItem.Paths.IsMediaItem) | |
{ | |
var theURL = Sitecore.Resources.Media.MediaManager.GetMediaUrl(mediaItem); | |
var mediaUrl = Sitecore.Resources.Media.HashingUtils.ProtectAssetUrl(theURL); | |
if (string.IsNullOrEmpty(mediaUrl)) | |
{ | |
return new NoBindingValueFoundResult(); | |
} | |
return new BindingValueFoundResult(mediaUrl); | |
} | |
else | |
{ | |
return new NoBindingValueFoundResult(); | |
} | |
} | |
else | |
{ | |
return new NoBindingValueFoundResult(); | |
} | |
} | |
public void StoreBindingValue(object newValue) | |
{ | |
if (newValue is string mediaUrl) | |
{ | |
newValue = mediaUrl; | |
} | |
} | |
} |
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
public class GatedAssetBindingHandlerLoader : MvcPipelineProcessor<LoadFieldBindingHandlersArgs> | |
{ | |
public override void Process(LoadFieldBindingHandlersArgs args) | |
{ | |
var tokenKey = new FieldBindingTokenKey("Gated Asset Binding Handler", "GatedAssetDownloadForm", | |
"Gated Asset Binding Handler"); | |
args.FieldBindingHandlers.Add(tokenKey, new GatedAssetBindingHandler()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment