Is there a way to programatically add a form data field to a form? I don't mean a custom form element / view, but rather a data field within the data submission that can be populated by code rather than a user input
For example - I want to capture some additional data in a form submission which is calculated based on the forms position in the page tree.
The only way I can see to do it, is to create a custom field element that has no view mode display, then hook into a Form Finalised / Submitting event to run some logic to populate the value in the data submission. However this then means an editor has to add that custom form element to the form. I'd rather it just happened magically.
I'm guessing that the form data shape / schema is created at publish time on the form container block. Could I hook in there and remap the form with my additional fields?
Can you create a custom actor to achieve your goal, as you have acess to the HttpRequest object?
We had a similar requirement to meet and went for the last option you mentioned. Forms are just content so you can tie in to the datafactory events. In our case we wanted to make sure that the fields were always added including retrospectively so we tied in to the CreatedContent, CheckingInContent and PublishingContent events to check whether the relevant fields had been added and, if not, added them (there's a ContentArea called ElementsArea which holds the form fields).
I did look into whether you could dynamically add data after the form had been posted in the way that you could with XForms but I couldn't find anything.
Hope that helps,