How can I programmatically add webform elements to an existing webform? I have a webform that is very simple, it's a questionnaire. It is attached to a content type called "event". e.g.

discussion_1:
  '#type': likert
  '#title': 'Discussion 1'
  '#questions':
    'Question 1': 'Question 1'
    'Question 2': 'Question 2'
    'Question 3': 'Question 3'
  '#answers': likert_five_scale


Is it possible to alter this webform when the webform is rendered by populating the questions from another source such as a view?

i.e. there will be a variable number of discussions with variable numbers of questions.

I am trying to dynamically build a webform based on data that is coming from the node to which the webform is attached
So I spent a full day investigating on something similar. Kudos to @jrockowitz for providing the right direction. Below is something I wrote to work.

$webforms = \Drupal::entityTypeManager()
  -&gt;getStorage('webform')
  -&gt;loadByProperties([]);

$new_field = [
   'new_field'=&gt;[ '#type' =&gt; 'textfield',
     '#title' =&gt; 'Question one',]];

foreach ($webforms as $webform) {
     // Applying to all forms.
      if (!in_array('new_field', $webform-&gt;getElementsDecoded())) {
        // $webform-&gt;getElementsDecoded() will get all the previous elements of the webform.
        $new_element_arrangment = array_merge($webform-&gt;getElementsDecoded(), $new_field);
        $webform-&gt;setElements($new_element_arrangment)-&gt;save();
    }
}