0

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?

2
  • 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 Commented Aug 31, 2021 at 5:52
  • Alireza, how can I get the options in the settings if they are dynamically generated in hook_form_alter() ? Commented Aug 31, 2021 at 12:03

2 Answers 2

0

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.

0

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)

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.