2

I need to extend every form element of Sitecore Forms with a Tooltip field. Obviously I'll have to extend all Razor views, but I'm looking for a way of adding a field to the base form element template and mapping this value to the View Model.

Is there a way to accomplish this without having to override the code of each and every form element?

1

Short answer: no.

But, a lot of the form elements use the same model class. (A lot of them are StringInputViewModels).

It's a good practice to provide an interface containing your new properties and creating new classes extending from the original version + implementing your interface.

For the repeatable code in those classes to init the binding settings and update the binding settings you can create an extension class.

Example:

public interface IBindingSettings
{
    ValueProviderSettings ValueProviderSettings { get; set; }
    bool StoreBindingValue { get; set; }
}

public static class BindingSettingsItemMapperExtension
{
    private const string StoreBindingValueParam = "Store Binding Value";

    public static void InitBindingSettingsProperties(this IBindingSettings bindingSettings, Item item)
    {
        bindingSettings.StoreBindingValue = MainUtil.GetBool(item.Fields[StoreBindingValueParam]?.Value, false);
    }

    public static void UpdateBindingSettingsFields(this IBindingSettings bindingSettings, Item item)
    {
        item.Fields[StoreBindingValueParam]?.SetValue(bindingSettings.StoreBindingValue ? "1" : string.Empty, true);
    }
}

[Serializable]
public class StringInputViewWithBindingsTokenModel : StringInputViewModel, IBindingSettings
{
    public bool StoreBindingValue { get; set; }

    protected override void InitItemProperties(Item item)
    {
        base.InitItemProperties(item);
        this.InitBindingSettingsProperties(item);
    }

    protected override void UpdateItemFields(Item item)
    {
        base.UpdateItemFields(item);
        this.UpdateBindingSettingsFields(item);
    }

}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.