Score:0

How to add custom option value to select field in Webform and have them save with the submission?

ng flag

I have a Drupal 7 webform when I need to dynamically populate a select field's options. I'm currently using hook_form_alter() and setting the values on the like so:

<?php
 $form['submitted']['left']['select_field_name']['#options'] = $new_options;
</php>

This works for displaying the new options on the webform creation but the values are not saved with the form.

I know there is a hook_webform_select_options_info() hook to create predefined options but it does not take any parameters so I have no way to pass a value from the webform to tell it how to dynamically create the options.

Is there any way to create a dynamic select option list and have it save the values that were not in the original Webform select values list?

Alireza Tabatabaeian avatar
cn flag
if options in $new_options are not contained in select_field_name settings, then they will be ignored in validation stage of webform submission, so you need to have them all in your element settings
quantumized avatar
ng flag
Alireza, how can I get the options in the settings if they are dynamically generated in hook_form_alter() ?
Score:0
ng flag

I solved this issue by populating all possible options through hook_webform_selection_options() and then resetting the options in hook_form_alter() to remove the ones I don't need for that instance.

Score:0
cn flag

You also can use a text field element rather than an Select element , and then in hook_form_alter change the element type to select and apply your desired options to that, in this case webform won't ignore input value as there is no restriction for textfield (if no regex or ... is set)

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.