Search code examples
sitecoreweb-forms-for-marketers

Add Reusable WFFM form with base fields to Insert Options


Currently, templates\wffm\forms folder has Insert Options like "Form Folder" & "Form" object. This is great for people that use the base "Form" template. In my case, I've got people who'd like to use a base payment form with hidden fields and other actions I've created.

I'd like to add my custom form "MyPaymentForm" to the Insert Options so it will be on display for all form users to select as their base form. The problem is that this is a form with fields and not a template so Insert Options will not let me add it.

  • I'd like my payment form with base fields to be a template for each group that uses it. For example, all will use the base fields but will also have custom fields in relation to their department.

Can you recommend a way I can serve up "MyPaymentForm" to users so that it is reusable for different groups?

Thanks, Chris


Solution

  • I would recommend using a Command Template to accomplish what you're asking. A Command Template is essentially a bit of custom logic that can be assigned to Insert Options.

    In your case, you could create a Command Template named 'MyPaymentForm' (or whatever you come up with) and then assign that Command Template to the Insert Options field of any template/item you wish. When a user right clicks to 'Insert->' and your Command Template is available, the user can click your 'MyPaymentForm' command template and your custom code will be executed.

    The action behind your command template could be as simple as creating a copy of your base form and inserting it in the content tree where the user executed the command template.

    Here is an example of command template code that could accomplish what you'd like. This is completely untested, but the concept is there.

    namespace MyNameSpace
    {
        public class CopyPaymentFormCommand : Sitecore.Shell.Framework.Commands.Command
        {
            public override void Execute(Sitecore.Shell.Framework.Commands.CommandContext context)
            {
                if (context.Items.Length == 0)
                    return;
    
                Sitecore.Data.Items.Item destinationItem = context.Items[0];
                if (destinationItem == null)
                    return;
    
                //retrieve the base MyPaymentForm item
                Sitecore.Data.Items.Item myPaymentForm = destinationItem.Database.GetItem("MyPaymentForm GUID");
                if (myPaymentForm == null)
                    return; // instead of just exiting here, you may want to log an error first
    
                //copy the MyPaymentForm item to the location in the content tree from which the command template was triggered
                Sitecore.Data.Items.Item copyOfMyPaymentForm = myPaymentForm.CopyTo(destinationItem, "MyPaymentForm");
    
                //perform any necessary post-processing of your newly copied item
            }
        }
    }
    

    After creating your custom code, you'll need to wire up Sitecore to recognize and use your command.

    1. Add a "command" element to the file /App_Config/Commands.config, like so:

    <command name="mycustomcommands:forms:copypaymentform" type="MyNamespace.CopyPaymentFormCommand, MyAssemblyName" />
    

    2. Next, create a Command Template item in your /sitecore/Templates section. You can do so by right-clicking the relevant folder under /sitecore/Templates and using Insert->Insert From Template, then select the "/sitecore/Templates/System/Branches/Command Template" data template.

    3. Next, in your newly created Command Template item, populate the Command field (contained in the Data section) with this text: mycustomcommands:forms:copypaymentform(id=$ParentID) note: the command name matches the command name defined in the Commands.config file

    4. You now have a Command Template that can be assigned as an Insert Option. To do so, simply edit the __Standard Values item of any template you choose and select Assign Insert Options. In the Insert Options dialog, browse to the Command Template item you created and add it to the 'Selected' list of insert options.

    Now, when a user attempts to insert a new item underneath an item with the template containing your command template insert option, they will have the option to click on your command template. Doing so will trigger your command template code, which will in turn create a copy of your payment form in the location from which the user executed the command.

    For more information on command templates, see this document on the SDN (specifically, Chapter 4): http://sdn.sitecore.net/upload/sitecore6/datadefinitioncookbook-usletter.pdf

    Hope this helps!