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.
$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.