Add value to a custom composite element in webform via hook_update_N 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()-&gt;getStorage('field_storage_config')-&gt;loadByProperties(array('field_name' =&gt; 'myfield'));

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

     // New value(s) to add here.
     $allowed['test'] = 'bob was here...';  
     $field-&gt;setSetting('allowed_values', $allowed);
     $field-&gt;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.
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()-&gt;getStorage('webform')-&gt;load('webform_id');
    $elements = $webform-&gt;getElementsDecodedAndFlattened();

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

    $webform-&gt;setElements($elements);
    $webform-&gt;save();

    . . .

}


Please let me know if there is a better way.