Hi,
We are building a Drupal intranet system - which means that every system user will be logged in.
I'm looking for a way to add few fields to a form when it is submitted so that the fields will be sent in emails and will appear in the submission results.
Currently I am using hook_form_alter() to calculate the user specific values I wish to add to a form submission, and all that is working fine. What I was hoping to do is to add my new fields to the form in the hook_form_alter routine. Is this possible? If so can anyone give me a start on what the code might look like to create a form field and assign it a default value in hook_form_alter.
If I am able to do this then all of the forms that are created on the system can have these fields added automatically.
Thanks
Bill
Comments
Comment #1
quicksketchGenerally this is not possible because Webform needs to know information about the fields in more places than just when the form is displayed. Webform needs to know the configuration of fields when displaying submissions, building tables of results, or exporting to CSV. If you just form_alter() in the fields, they won't be saved to the database either because as far as Webform is concerned, they're not really there.
I would suggest you use other hooks Webform provides (perhaps hook_webform_submission_insert()) to make it so that configured components save certain dynamic values to the database if needed. You might also try modifying $node->webform['components'] to populate your own custom components, but I'm not sure that approach would work effectively. If you need some kind of complicated Webform field, you might try adding the field as a simple textfield and then theming/form_altering the existing field into some other kind of display. That way Webform will know about the field and display it as a text value everywhere but you can modify the way it is presented in the form.
Because this question is so old and not really within the scope of the Webform queue (we specifically state we don't provide help on custom coding problems), I'm going to close this issue directly.