Sitecore – Adding the site language to Sitecore Forms postback URLS

When using Sitecore Forms, the POST url of the form does not include the current site language parameter, which can be a problem because the POST url is always /formbuilder with some query params. This may lead to issues when the site language isn’t stored in the cookie, when the cookie isn’t send to the server or when there aren’t any cookies at all, because Sitecore will then fall back to the the default language that is configured for the site.

We can easily add the site language to the URL using the sc_lang parameter, by hooking into the forms.renderForm pipeline.

The solution

Create a processor that adds the site language when it does not exist yet

using Sitecore.ExperienceForms.Mvc.Pipelines.RenderForm;
using Sitecore.Mvc.Pipelines;

namespace Example
{
    public class AddSiteLanguage : MvcPipelineProcessor<RenderFormEventArgs>
    {
        public override void Process(RenderFormEventArgs args)
        {
            if (!args.IsPost && !args.QueryString.ContainsKey("sc_lang"))
            {
                args.QueryString.Add("sc_lang", Sitecore.Context.Language.Name);
            }
        }
    }
}

Register the processor through a config patch

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <forms.renderForm>
        <processor type="Example.AddSiteLanguage, Example" resolve="true" patch:before="processor[@type='Sitecore.ExperienceForms.Mvc.Pipelines.RenderForm.BuildForm, Sitecore.ExperienceForms.Mvc']" />
      </forms.renderForm>
    </pipelines>
  </sitecore>
</configuration>

Leave a Reply

Your email address will not be published. Required fields are marked *