Thursday, 9 June 2016

WFFM - Creating custom hidden field with Context Item ID

Last time I was creating article comment functionality using Web Forms For Marketers. I have designed it that all comments was saved as Sitecore items and put into bucket in specified location. One of the problems was to put some information about article we are commenting.

I have chose Sitecore ID to be put inside that comment item. Unfortunately I wasn't able to do it out-of the-box. I needed to write some custom code and I thought that custom WFFM field would be the best idea.

WFFM version I was using was 8.1 rev. 151217 (Update-1). It's code changes very frequently so tutorials found in internet was a bit of outdated (very probably that mine will be soon too ;)).

I decided that the best and pretty easy way, will be create control that will put hidden html field and populate it with context item ID. I was told that the best way to achieve it will be create derivative from SingleLineText field. Tools like ILSpy or dotPeek was very helpful to take a look at the code inside. The value of Sitecore.Context.Item.ID is taken in OnLoad() method. To make it more flexible I have added field that made it switchable.

Here is the code:

using Sitecore.Form.Core.Attributes;
using Sitecore.Form.Web.UI.Controls;
using System.ComponentModel;
using System.ComponentModel.Design;
 
namespace SitecoreCoffee.WFFM.CustomFields
{
    /// <summary>
    /// Hidden Id Field
    /// </summary>
    [Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design"typeof(IDesigner))]
    public class HiddenIdField : SingleLineText
    {
        private bool _isHidden = true;
 
        /// <summary>
        /// Used to switch hidden/visible
        /// </summary>
        [VisualCategory("Configuration")]
        [VisualFieldType(typeof(Sitecore.Form.Core.Visual.BooleanField))]
        [VisualProperty("Is Hidden?"10), DefaultValue("Yes")]
        public string IsHidden
        {
            get
            {
                return _isHidden ? "Yes" : "No";
            }
 
            set
            {
                _isHidden = value == "Yes";
            }
        }
 
        protected override void OnLoad(System.EventArgs e)
        {
            Text = Sitecore.Context.Item.ID.ToString();
 
            if (_isHidden)
            {
                Attributes["style"= "display: none";
            }
        }
    }
} 

If you are using MVC you should also create ViewModel for that field:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
 
namespace SitecoreCoffee.WFFM.CustomFields
{
    /// <summary>
    /// View Model for Hidden Id Field
    /// </summary>
    public class HiddenIdFieldViewModel : Sitecore.Forms.Mvc.ViewModels.Fields.SingleLineTextField
    {
        [DataType(DataType.Text)]
        public override string Value { get; set; }
 
        /// <summary>
        /// Initializes field
        /// </summary>
        public override void Initialize()
        {
            KeyValuePair<string, string> isHidden = Parameters.FirstOrDefault(x => x.Key.ToUpper() == "ISHIDDEN");
 
            if (isHidden.Value.ToUpper() != "NO")
            {
                if (!string.IsNullOrWhiteSpace(CssClass))
                {
                    CssClass += " hidden";
                }
 
                else
                {
                    CssClass = "hidden";
                }
            }
 
            ShowTitle = false;
 
            Value = Sitecore.Context.Item.ID.ToString();
        }
    }
}

The last part is to create Sitecore item. It should be added below /sitecore/system/Modules/Web Forms for Marketers/Settings/Field Types. This item should be based on template /sitecore/templates/Web Forms for Marketers/Field Type and be populated with:
  • Assembly - file name of code assembly
  • Class - class name of field type with full namespace
  • MVC Type - class name of field type view model with full namespace and its assembly name (after comma)


And it can be used in form:



Hope it will be helpful :)

Share it:

Radosław Kozłowski - Senior Sitecore Developer Radosław Kozłowski

Author & Editor

Senior Sitecore Developer and Team Leader with over 4 years experience

3 comments:

  1. In MVC code instead of Parametes use ParametersDictionary

    ReplyDelete
    Replies
    1. Hi Sukesh, I'm using MVC and tried to use ParametersDictionary. I'm not getting reference for ParametersDictionary.Could you please let me know whether I'm missing any assembly reference or something else. I'm using the above code.
      Thanks in advance!

      Delete