3

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?

2 Answers 2

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);
    }

}
0

If anyone still is looking for an answer what is needed to add extra field like tooltip to each Sitecore Forms field, here is the list:

  • Tooltip folder and Tooltip text item under /sitecore/client/Applications/FormsBuilder/Components/Layouts/PropertyGridForm/PageSettings/Common item in core database, similar to e.g. Label item

  • TooltipExpander item (duplicate of /sitecore/client/Applications/FormsBuilder/Components/Layouts/PropertyGridForm/PageSettings/Common/Sections/DetailsExpander),

  • Tooltip section item in core database - duplicate of /sitecore/client/Applications/FormsBuilder/Components/Layouts/PropertyGridForm/PageSettings/Settings/SingleLineText/Details with 2 items above selected in the ConfigurationItem and ControlDefinitions fields (and for any other field that should have tooltip)

  • new view model class inheriting from StringInputViewModel with TooltipText property (and for any other field that should have tooltip):

     public string TooltipText { get; set; }
    
     protected override void InitItemProperties(Item item)
     {
         base.InitItemProperties(item);
         TooltipText = StringUtil.GetString(item.Fields["Tooltip Text"]);
     }
    
     protected override void UpdateItemFields(Item item)
     {
         base.UpdateItemFields(item);
         item.Fields["Tooltip Text"]?.SetValue(TooltipText, false);
     }
    
  • change to your Views\FormBuilder\FieldTemplates\SingleLineText.cshtml - depending on how you want tooltips to be rendered (and for any other field that should have tooltip),

  • Tooltip text field on /sitecore/templates/System/Forms/Fields/Field

  • /sitecore/system/Settings/Forms/Field Types/Basic/Single-Line Text must referece your new view model class (and for any other field that should have tooltip).

Source of the answer: https://www.skillcore.net/sitecore/sitecore-add-tooltips-to-forms-fields

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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