I added a class which handles tha FormSavingNotification.
This will add fields defined in my appsettings.
Ex.:
"DefaultFormTrackingValues": "gclid:[@gclid],utmsource:[@utmsource],sideUrl:[Url],test1:[@test1],test2,test3:[@test3],test4,test5"
If we on a form find any field, that also exist in the list, it adds the missing ones from the list in the same group as the existing.
If we dont find any fields from the list, its creates a new group and add the fields from the list.
The values in the brackets, is the value for the field.
using System;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Cms.Core.Events;
using Umbraco.Forms.Core.Models;
using Umbraco.Forms.Core.Services.Notifications;
using website.core;
using Form = Umbraco.Forms.Core.Models.Form;
namespace Website.Core.Forms.Handlers
{
public class FormSavingNotificationHandler : INotificationHandler<FormSavingNotification>
{
private AppSettings _appSettings;
private string DefaultFormTrackingValues = "";
public FormSavingNotificationHandler(AppSettings settings)
{
_appSettings = settings;
}
/// <summary>
/// Handles the FormSavingNotification event and performs tracking logic based on the specified appSettings.
/// </summary>
/// <param name="notification">The FormSavingNotification object.</param>
public void Handle(FormSavingNotification notification)
{
//Handle Tracking: Handle values speficied in appSettings
DefaultFormTrackingValues = _appSettings.DefaultFormTrackingValues;
List<string> neededFieldsWithValues = DefaultFormTrackingValues.Split(',').ToList();
var requiredFields = DefaultFormTrackingValues.Split(',').Select(val => val.Split(':')[0]).ToList();
var hasTracking = false;
foreach (Form form in notification.SavedEntities)
{
//Form with existing group for tracking
List<KeyValuePair<int, int>> existingFieldSets = new List<KeyValuePair<int, int>>();
List<string> eistingFieldsOnExistingTracking = new List<string>();
var pageIndex = 0;
foreach (Page page in form.Pages)
{
var fieldsetIndex = 0;
foreach (FieldSet fieldset in page.FieldSets)
{
foreach (FieldsetContainer fieldsetContainer in fieldset.Containers)
{
foreach (Field field in fieldsetContainer.Fields)
{
if (requiredFields.Contains(field.Alias))
{
var key = new KeyValuePair<int, int>(pageIndex, fieldsetIndex);
if (!existingFieldSets.Contains(key))
{
existingFieldSets.Add(key);
hasTracking = true;
}
}
eistingFieldsOnExistingTracking.Add(field.Alias);
}
}
fieldsetIndex++;
}
pageIndex++;
}
if (hasTracking && existingFieldSets != null)
{
var existingPage = existingFieldSets[0].Key;
var existingFieldSet = existingFieldSets[0].Value;
//Create missing required fields on existing group
var newFields = requiredFields.Except(eistingFieldsOnExistingTracking).ToList();
var addToContainer = form.Pages[existingPage].FieldSets[existingFieldSet];
CreateFields(newFields, addToContainer.Containers[0]);
}
//Form with no group for tracking
if (!hasTracking)
{
var pagesCount = form.Pages.Count;
//if form dosnĀ“t have any of the required fields and dosn't have a group for them
FieldSet newContainer = new FieldSet();
FieldsetContainer newFieldsetContainer = new FieldsetContainer();
newContainer.Caption = "Query Strings";
newContainer.Containers.Add(newFieldsetContainer);
form.Pages[pagesCount - 1].FieldSets.Add(newContainer);
CreateFields(requiredFields, newFieldsetContainer);
}
}
}
private void CreateFields(List<string> requiredFields, FieldsetContainer newFieldsetContainer)
{
foreach (string f in requiredFields)
{
var ipGuid = Guid.NewGuid();
Field newfield = new Field();
newfield.Alias = f;
newfield.Caption = f;
newfield.FieldTypeId = Guid.Parse("0bca0f8f-7ed9-48a0-8693-bc5c204b3130");
newfield.Id = ipGuid;
newfield.Mandatory = false;
if (GetValue(DefaultFormTrackingValues, f) != "")
{
newfield.Settings.Add("DefaultValue", GetValue(DefaultFormTrackingValues, f));
}
newFieldsetContainer.Fields.Add(newfield);
}
}
private string GetValue(string input, string searchValue)
{
int startIndex = input.IndexOf(searchValue);
if (startIndex == -1)
{
return string.Empty;
}
int semicolonIndex = input.IndexOf(':', startIndex);
if (semicolonIndex == -1)
{
return string.Empty;
}
int commaIndex = input.IndexOf(',', semicolonIndex);
if (commaIndex == -1)
{
commaIndex = input.Length;
}
return input.Substring(semicolonIndex + 1, commaIndex - semicolonIndex - 1);
}
}
}
umbraco forms add extra data to saved entry
Hi,
We need add extra data to each saved form entry. We must collect tracking data from our affiliates.
I can create hidden form fields thru C# and populate their values from JS.
But how do i add the data to each saved form entry, without having to add the field types on each created form in the backoffice?
I tried looking into RecordSavingNotification, but cant figure out how to use this.
I tried this approach on form.cshtml for a theme. Unfortunately thats dosnt add the field to the form. It adds it on the frontend.
What i actually would like is to add my fields to each saved record. Maybe i need to do it someplace else. Can anyone point me in the right direction?
Its Umbraco 10.
Hi,
I found a solution.
I added a class which handles tha FormSavingNotification. This will add fields defined in my appsettings. Ex.: "DefaultFormTrackingValues": "gclid:[@gclid],utmsource:[@utmsource],sideUrl:[Url],test1:[@test1],test2,test3:[@test3],test4,test5"
If we on a form find any field, that also exist in the list, it adds the missing ones from the list in the same group as the existing.
If we dont find any fields from the list, its creates a new group and add the fields from the list.
The values in the brackets, is the value for the field.
is working on a reply...