0

Testing out some bits with webform. I have created a webform via my module. In it I have a number of elements including a custom composite. Inside the custom composite I have several elements, one is a select element with a list of options (i.e. allowed values).

Now comes time I need to do some updates. That is run hook_update_N on an existing module/set-up. I have updates for all other fields (not webform) working correctly. Rough code shown below:

/**
  * Update for mymodule to add more options to the paragraph and webform custom composite.
  */
 function mymodule_update_8001() {
   // Updates the paragraph allowed field
   $fields = \Drupal::entityTypeManager()->getStorage('field_storage_config')->loadByProperties(array('field_name' => 'myfield'));

   foreach ($fields as $field) {
     $allowed = $field->getSetting('allowed_values');

     // New value(s) to add here.
     $allowed['test'] = 'bob was here...';  
     $field->setSetting('allowed_values', $allowed);
     $field->save();
   }

   // Code to update the custom composite element to go here.

 }

I know it is rough code, ignoring that, still getting my head around the changes to Webform too. I know the \Drupal::entityTypeManager()->getStorage method doesn't work for webform composite custom elements and can't see any update function in the composite example module.

So the question is how to I get the options (allowed values) of a select in a custom composite from the webform so I can edit, add, update, etc... when I do other updates to my module?

The method noted in How do I programmatically update the allowed values of a list field? works for basic fields but with for dynamic options. It will not cover what I need here as (a) it is not performing an update of existing fields and (b) doesn't appear to work with custom composite in WebForm, or at least I cannot get it to work, with a select.

3
  • Possible duplicate of How do I programmatically update the allowed values of a list field?
    – user72672
    Dec 3, 2019 at 15:25
  • That is doing dynamic allowed list, it doesn't appear to work with custom composite elements on Webforms.
    – nickbits
    Dec 3, 2019 at 16:42
  • Breaking this down into steps - $webform = \Drupal::entityTypeManager()->getStorage('webform')->load('webform id'); gets the webform, $elements = $webform->getElement('element id'); gets the composite component. Now how do you get the element of that component and set the options? Will report back once got that bit figured out.
    – nickbits
    Dec 3, 2019 at 22:58

1 Answer 1

0

After trawling through the Webform source think I have an answer.

/**
 * Update using hook_update_N for mymodule to add more options to a webform custom composite.
 */

function mymodule_update_8001() {

    . . .   

    // Update a composite element in a webform
    $webform = \Drupal::entityTypeManager()->getStorage('webform')->load('webform_id');
    $elements = $webform->getElementsDecodedAndFlattened();

    // Add new options here.
    $elements['my_composite_id']['#element']['my_composite_element_id']['#options']['my_new_option'] = 'My new option text';

    $webform->setElements($elements);
    $webform->save();

    . . .

}

Please let me know if there is a better way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.