Created
January 17, 2020 08:43
-
-
Save netojoa/209f79e48eb9ba702d73248c2afee49d to your computer and use it in GitHub Desktop.
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 SetFormId : MvcPipelineProcessor<RenderFormEventArgs> | |
{ | |
public override void Process(RenderFormEventArgs args) | |
{ | |
var customId = string.Empty; | |
// Try get customId from form submission | |
if (args.IsPost) | |
{ | |
var fullKey = | |
HttpContext.Current.Request.Form.AllKeys.FirstOrDefault(key => | |
key.ToLower().EndsWith("customId".ToLower())); | |
if (!string.IsNullOrEmpty(fullKey)) | |
customId = HttpContext.Current.Request.Form[fullKey]; | |
} | |
if (string.IsNullOrEmpty(customId)) | |
{ | |
// Get RenderingContext | |
var renderingContext = Sitecore.Mvc.Presentation.RenderingContext.CurrentOrNull; | |
if (renderingContext == null && !args.Attributes.ContainsKey("customId")) | |
return; | |
if (renderingContext!=null && !renderingContext.Rendering.Parameters.Contains("id")) | |
return; | |
// Get custom Form ID from RenderingContext or Attributes | |
customId = renderingContext != null | |
? renderingContext.Rendering.Parameters["id"] | |
: (string)args.Attributes["customId"]; | |
} | |
if (string.IsNullOrEmpty(customId)) | |
return; | |
// Add CustomId to Attributes if not already there | |
if (!args.Attributes.ContainsKey("customId")) | |
args.Attributes.Add("customId",customId); | |
// Setup custom form ID | |
args.FormHtmlId = customId; | |
if (args.Attributes.ContainsKey("id")) | |
args.Attributes["id"] = customId; | |
else | |
args.Attributes.Add("id", customId); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment