Dynamic webform component select options

nl flag

In Drupal 7, dynamic options for a select widget could be created with hook_webform_select_options_info():

 * Implements hook_webform_select_options_info().
function module_webform_select_options_info() {
  $items = array();
  $items['custom'] = array(
    'title' => t('Custom'),
    'options callback' => 'module_options_function',
  return $items;

But this hook isn't in Drupal 9. What should be used in Drupal 9? The closest hook I see in webform.api.php is hook_webform_options_alter().

nl flag

There is no replacement hook. In fact, many things that were hooks should now be done with webform handlers.

Below is a simplified version of my solution. Note that in order to set the options dynamically, the field must be added already with dummy option(s), or you need to add the field in the handler prior to setting its options.


namespace Drupal\my_module\Plugin\WebformHandler;

use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\Plugin\WebformHandlerBase;
use Drupal\webform\WebformSubmissionInterface;

 * My Module custom webform handler.
 * @WebformHandler(
 *   id = "my_module_custom_webform_handler",
 *   label = @Translation("My Webform Handler"),
 *   category = @Translation("Custom"),
 *   description = @Translation("Alter form to populate field."),
 *   cardinality = \Drupal\webform\Plugin\WebformHandlerInterface::CARDINALITY_SINGLE,
 *   results = \Drupal\webform\Plugin\WebformHandlerInterface::RESULTS_PROCESSED,
 *   submission = \Drupal\webform\Plugin\WebformHandlerInterface::SUBMISSION_OPTIONAL,
 * )
class MyModuleCustomWebformHandler extends WebformHandlerBase {

   * {@inheritdoc}
  public function alterForm(array &$form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission): void {
    // Clear dummy options and set program options.
    $form['elements']['my_radios']['#options'] = $this->customService->getOptionList();

Though the article below is not exactly what I needed, this was the step in the right direction that helped me find this solution:

I sit in a Tesla and translated this thread with Ai:


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.