Skip to content

Instantly share code, notes, and snippets.

@netojoa
Created January 17, 2020 08:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save netojoa/209f79e48eb9ba702d73248c2afee49d to your computer and use it in GitHub Desktop.
Save netojoa/209f79e48eb9ba702d73248c2afee49d to your computer and use it in GitHub Desktop.
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