November Happy Hour will be moved to Thursday December 5th.

Default form elements with predefined value for a Custom form container block

Vote:
 

In order to passthrough few common prefined input values based on the form utilized, is there a way to default to automatically create a form container block with form elements. 

I got Salesforce connector add on and I need to send few hidden values for each form created among visible input fields mapped. So, if there is a way to default the form elements as part of form container block then it is less time for editors to create a predefined hidden field form element when every time they want to create a new custom form container block. 

#258962
Jul 14, 2021 23:22
Vote:
 

Hi Arun

Do you have these fields added to every single form created ever? If so then you can hook into the CreatedContent event, check if the content type is a form container and then add the fields yourself.

The alterantive approach is to create a custom form container block using the code below:

using EPiServer.DataAbstraction;
using EPiServer.DataAnnotations;
using EPiServer.Forms.Core;
using EPiServer.Forms.Implementation.Elements;
using EPiServer.ServiceLocation;

namespace Example.FormExample
{
    [ContentType(
         DisplayName = "Salesforce form container", 
         GUID = "16660C0A-5129-4E6C-85C0-E16161842F2D", 
         GroupName = "FormsContainerElements")]
    [ServiceConfiguration(typeof(IFormContainerBlock))]
    public class SalesforceFormContainerBlock : FormContainerBlock
    {
        public override void SetDefaultValues(ContentType contentType)
        {
            base.SetDefaultValues(contentType);

            //Add custom elements here
        }
    }
}

This then allows editors to select the form type they want and retains the ability t create non-Salesforce specfic forms as shown below:

#258964
Jul 15, 2021 7:45
Vote:
 

Hi David, 

Thanks for the reommendation.Sorry if i didnt explain the challenge I am facing properly, I am trying to default form element blocks rather form fields in my case I need to add 2 predefined hiddenItem blocks as part of the custom form container block when created by default

#259006
Edited, Jul 16, 2021 1:30
Vote:
 

Hello Arun

Something like this will do the trick:

using EPiServer;
using EPiServer.Core;
using EPiServer.DataAccess;
using EPiServer.DataAnnotations;
using EPiServer.Forms.Controllers;
using EPiServer.Forms.Core;
using EPiServer.Forms.Implementation.Elements;
using EPiServer.Framework;
using EPiServer.Framework.DataAnnotations;
using EPiServer.Framework.Initialization;
using EPiServer.Framework.Web;
using EPiServer.Security;
using EPiServer.ServiceLocation;

namespace Example.FormExample
{
    [ContentType(
         DisplayName = "Salesforce form container",
         Description = "Form with pre-defined hidden fields",
         GUID = "16660C0A-5129-4E6C-85C0-E16161842F2D",
         GroupName = "FormsContainerElements")]
    [ServiceConfiguration(typeof(IFormContainerBlock))]
    public class SalesforceFormContainerBlock : FormContainerBlock
    {
    }

    [TemplateDescriptor(AvailableWithoutTag = true, Default = true, ModelType = typeof(SalesforceFormContainerBlock), TemplateTypeCategory = TemplateTypeCategories.MvcPartialController)]
    public class SalesforceFormContainerController : FormContainerBlockController
    {
    }

    [ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
    public class SalesforceFormInit : IInitializableModule
    {
        public void Initialize(InitializationEngine context)
        {
            var contentEvents = context.Locate.Advanced.GetInstance<IContentEvents>();
            contentEvents.CreatedContent += ContentEvents_CreatedContent;
        }

        private void ContentEvents_CreatedContent(object sender, ContentEventArgs e)
        {
            if (!(e.Content is SalesforceFormContainerBlock)) 
                return;
            
            if ((e.Content as SalesforceFormContainerBlock)?.ElementsArea?.IsEmpty == false) 
                return;

            var contentAssetHelper = ServiceLocator.Current.GetInstance<ContentAssetHelper>();
            var formAssetFolder = contentAssetHelper.GetOrCreateAssetFolder(e.Content.ContentLink);
            var contentRepo = ServiceLocator.Current.GetInstance<IContentRepository>();

            //Add predefined elements here
            var hiddenFormElement = contentRepo.GetDefault<PredefinedHiddenElementBlock>(formAssetFolder.ContentLink);
            hiddenFormElement.PredefinedValue = "Some value";
            hiddenFormElement.Content.Name = "Predefined hidden element";
            contentRepo.Publish(hiddenFormElement as IContent, AccessLevel.NoAccess);

            //Update the form itself
            var updateableForm = (e.Content as SalesforceFormContainerBlock).CreateWritableClone() as SalesforceFormContainerBlock;
            updateableForm.ElementsArea = new ContentArea();
            updateableForm.ElementsArea.Items.Add(new ContentAreaItem()
                {ContentLink = hiddenFormElement.Content.ContentLink});
            contentRepo.Save(updateableForm as IContent, SaveAction.ForceCurrentVersion, AccessLevel.NoAccess);
        }

        public void Uninitialize(InitializationEngine context)
        {
            var contentEvents = context.Locate.Advanced.GetInstance<IContentEvents>();
            contentEvents.CreatedContent -= ContentEvents_CreatedContent;
        }
    }

}

The outcome is a a form that has hidden fields on it as shown below:

#260076
Aug 04, 2021 17:20
Vote:
 

Came across this post today while specing out some similar Forms customisations. Very helpful. Thanks for posting this answer David

#267595
Nov 30, 2021 12:45
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.