This project is read-only.

Need help handling submission event of a Custom Form

Topics: Customizing Orchard
Dec 18, 2012 at 2:25 AM


I'm trying to write a handler that will capture the form submitted event that is exposed in the Custom Forms module.  I have successfully implemented the IRulesManager interface and implemented the TriggerEvent method in my class, but the Func<Dictionary<string, object>> tokensContext parameter that gets passed into that method only gives me access to the Content item that was created on form submission.  Is there any way to get the actual Custom Form that was submitted as well?  

I have a custom part that I've attached to the Custom Form Type that I would like to access during a form submission.  I can't find a way to reference the Custom Form item that was used to create the content item during form submission.

Here is the code for my event handler that captures the submission event:

using System;
using System.Collections.Generic;
using Orchard.ContentManagement;
using Orchard.CustomForms.Rules;

namespace ATS.HubSpot.Handlers
    //Target implementation. Take in the current item and do something interesting with it. 
    public class HubSpotFormEventHandler : IRulesManager {
        private readonly IContentManager _manager;
        public HubSpotFormEventHandler(IContentManager manager) {
            _manager = manager;
        //this method listens for a custom form submission and submits data to hubspot if the option has been selected on the HubSpotFormPart associated with the Custom Form.
        public void TriggerEvent(string category, string type, Func<Dictionary<string, object>> tokensContext)
            if (type.ToLower().Equals("submitted"))

                //a custom form has been submitted.  Check for a content item in the dictionary
                if (tokensContext.Invoke().ContainsKey("Content"))

                    var contentItem = tokensContext.Invoke()["Content"] as ContentItem;
                    if (contentItem != null)
                        //do stuff with content item.  Content item should equal the Form type being submitted.
                        //var CustomFormPart = not sure if there is a way to get the Custom Form here... 




The only other way I can think of to do this is to create a custom Event and override the Item Controller for Custom Forms....

Any help would be appreciated.

Dec 19, 2012 at 3:06 AM

I think I have figured this out.  I believe the content item that gets passed in when the TriggerEvent event occurs is a child of the Custom Form that submitted it.  The way to get the corresponding Custom Form Part from the child item is to cast the contentitem as a CommonPart and pull its Container attribute.  The Container attribute contains the "Parent" of the item it is attached to.  In this case, the parent is the custom form.  

I just wanted to document the answer to my question in case anyone else has trouble with the TriggerEvent method in the future.