Pages

Monday, October 4, 2021

Hide Optimizely Form Info when Redirecting

With Optimizely Forms you have the option to redirect to a page after a complete submission. By default, Optimizely appends some form information to the URL it redirects to. This allows you to process the form submission data in any way you need to on the page it directs to, or perform an action based on what form was submitted. However, if you don't want that information in the URL you can change the behaviour.

Start by creating a method to determine when to hide the info

If you decide you want to avoid the extra form info for all Forms skip this section, and modify the logic in the next one. I wanted a way to control when to hide the info, so I created a new Form Container and added a checkbox property to control it. 
[pre class="brush:csharp;class-name:collapse-box;"]
using EPiServer.DataAbstraction;
using EPiServer.DataAnnotations;
using EPiServer.Forms.Core;
using EPiServer.Forms.Implementation.Elements;
using EPiServer.ServiceLocation;
using System.ComponentModel.DataAnnotations;

namespace EpiSandbox.Models
{
    [ContentType(DisplayName = "Custom Form Container", GUID = "d6b5bbce-c897-4e4f-bb54-d14cd6b8a3a7", Description = "Custom form container to use in Episerver.",
        GroupName = EPiServer.Forms.Constants.FormElementGroup_Container)]
    [ServiceConfiguration(typeof(IFormContainerBlock))]

    [ImageUrl("~/resources/icons/custom-form-block.jpg")]
    public class StandardFormContainerBlock : FormContainerBlock
    {
        [Display(Name = "Hide Form Info on Redirect",
            Description = "Check this if you don't want to show form information in the URL when redirecting after submission.",
            GroupName = SystemTabNames.Content, Order = -6499)]
        public virtual bool HideFormInfoOnRedirect{ get; set; }
    }
}
[/pre]

Because it's a custom Form Container, I needed to add a controller for it to render. Since it's just adding a hidden property to the default FormContainerBlock, the controller is really simple.
[pre class="brush:csharp;class-name:collapse-box;"]
using EpiSandbox.Models;
using EPiServer.Forms.Controllers;
using EPiServer.Framework.DataAnnotations;
using EPiServer.Framework.Web;

namespace EpiSandbox.Controllers
{
    [TemplateDescriptor(AvailableWithoutTag = true,
        Default = true,
        ModelType = typeof(StandardFormContainerBlock),
        TemplateTypeCategory = TemplateTypeCategories.MvcPartialController
    )]
    public class StandardFormContainerBlockController: FormContainerBlockController
    {  
        // the base Index action takes over
    }
}
[/pre]

Next create a new AppendFormInfoToRedirect module

There are two ways to approach this:

  1. Inherit from the EPiServer.Forms.Core.IAppendExtraInfoToRedirection interface, and write your own GetExtraInfo method for adding any info you need or don't.

  2. Inherit from the existing EPiServer.Forms.Implementation.DefaultAppendExtraInfoToRedirection method, and override the base GetExtraInfo.

For this purpose I am using #2 because it makes it easier to default to the OOB behaviour when we want it. In the GetExtraInfo method I have a cast to my custom StandardFormContainerBlock and a conditional for the HideFormInfoOnRedirect check property. If you want this to work for all of your forms, remove these two pieces.
[pre class="brush:csharp;class-name:collapse-box;"]
using EpiSandbox.Models;
using EPiServer;
using EPiServer.Forms.Core;
using EPiServer.Forms.Core.Models;
using EPiServer.Forms.Implementation;
using EPiServer.Forms.Implementation.Elements;
using EPiServer.ServiceLocation;
using System.Collections.Generic;

namespace EpiSandbox.Business.Initialization
{
    [ServiceConfiguration(typeof(IAppendExtraInfoToRedirection))]
    public class AppendFormInfoToRedirect : DefaultAppendExtraInfoToRedirection
    {
        private static IContentRepository _contentRepository;
        public AppendFormInfoToRedirect(IContentRepository contentRepo)
        {
            _contentRepository = contentRepo;
        }

        public override IDictionary<string, object> GetExtraInfo(FormIdentity formIden, Submission submission)
        {
            if (formIden == null) { return new Dictionary<string, object>(); }

            // remove the cast and the HideFormInfoOnRedirect check to affect all forms.
            var formBlock = _contentRepository.Get<FormContainerBlock>(formIden.Guid) as StandardFormContainerBlock;
            if (formBlock != null && formBlock.HideFormInfoOnRedirect)
            {
                return new Dictionary<string, object>();  // this prevents info from the URL
            }

            return base.GetExtraInfo(formIden, submission);  // this is the default behaviour
        }
    }
}
[/pre]

Finally, register the new AppendFormInfoToRedirect module to your IOC Container

[pre class="brush:csharp;class-name:collapse-box;"]
using EPiServer.Forms.Core;
using EPiServer.Framework;
using EPiServer.Framework.Initialization;
using EPiServer.ServiceLocation;

namespace EpiSandbox.Business.Initialization
{
    [ModuleDependency(typeof(EPiServer.Forms.EditView.InitializationModule))]
    public class FormsInitializationModule : IConfigurableModule
    {
        public void ConfigureContainer(ServiceConfigurationContext context)
        {
            context.StructureMap().Configure(c =>
            {
                c.For<IAppendExtraInfoToRedirection>().Use<AppendFormInfoToRedirect>();
            });
        }

        public void Initialize(InitializationEngine context)
        {
            // nothing to see here
        }

        public void Uninitialize(InitializationEngine context)
        {
            // or here
        }
    }
}
[/pre]

The end result is a new Form Container Block with an option to disable appending the extra info to the url on redirect. 


And a clean URL when it's put into action.



No comments:

Post a Comment

Share your thoughts or ask questions...