WFFM 保存操作从编辑器获取字段映射

WFFM Save Action get Field Mapping from Editor

我创建了一个自定义 Save Action,它将 WFFM 字段值写入第 3 方服务。自定义 Save Action 使用开箱即用的 FieldMappings 编辑器,以便内容编辑器可以指定哪些字段映射到发送到服务的哪些属性。

我让它工作,所以所有属性都显示在编辑器中,供用户 select 相关字段。

问题是我找不到如何在 Save ActionExecute 方法处获取这些映射。我反编译了现有的 Tell a Field Save Action,因为它也使用 MappingField 编辑器,但它最终忽略了映射。

public class SaveToSalesForceMarketingCloud : ISaveAction
{
    public string Mapping { get; set; }

    public void Execute(ID formid, AdaptedResultList fields, params object[] data)
    {
        FormItem formItem = Sitecore.Context.Database.GetItem(formid);
        if (formItem == null)
            return;

        string mappingXml = Mapping;

        // Using the Property Name does not return the Mapped Field
        var emailAddressField = fields.GetEntryByName("Email address");
        // Using the actual name of the Field on the Form returns the Field
        var emailField = fields.GetEntryByName("Email");
    }
}

有人知道如何获取映射吗?

我认为它是通过将字段与您的保存操作中的 public 属性匹配来连接起来的 class。

所以对于你的例子:

public string EmailAddress { get; set; }
public string ConfirmEmailAddress { get; set; }
public string Title { get; set ;}
etc..

映射以 key/value 对的形式存储在表单的“保存操作”字段中,然后填充到您定义的 Mapping 属性 中。

检查您的表单的 Save Field,您会注意到字符串的格式类似于 <mapping>key=value1|key=value2</mapping>。这是您在保存操作中可用的字符串值。您需要自己处理,WFFM 不会为您连接任何东西。为了访问映射,您使用 Sitecore util 方法:

NameValueCollection nameValueCollection = StringUtil.ParseNameValueCollection(this.Mapping, '|', '=');

这使您可以访问 key/value 对。然后,您需要枚举这些字段或提交的表单数据(视情况而定)以填充您的对象以供进一步操作。

假设键是 WFFM 字段 ID,值是要映射到的字段,类似于此

foreach (AdaptedControlResult adaptedControlResult in fields)
{
    string key = adaptedControlResult.FieldID; //this is the {guid} of the WFFM field
    if (nameValueCollection[key] != null)
    {
        string value = nameValueCollection[key]; //this is the field you have mapped to
        string submittedValue = adaptedControlResult.Value; //this is the user submitted form value
    }
}

查看 Sitecore.Forms.Custom 中的 Sitecore.Form.Submit.CreateItem 以获取类似操作的示例以及使用它的字段映射编辑器。