Programmatically change select field options in contact form I'm currently rendering a contact form programmatically in a block plugin like so.

    $type = 'contact_message';
    $bundle = 'my_contact_form_name';
    $values = array();
    if (\Drupal::entityManager()-&gt;getDefinition($type)-&gt;hasKey('bundle')) {
        $bundle_key = \Drupal::entityManager()-&gt;getDefinition($type)-&gt;getKey('bundle');
        $values     = array($bundle_key =&gt; $bundle);
    }
    $entity = \Drupal::entityManager()
        -&gt;getStorage($type)
        -&gt;create($values);

    if ($entity instanceof EntityOwnerInterface) {
        $entity-&gt;setOwnerId(\Drupal::currentUser()-&gt;id());
    }

    $form = \Drupal::entityManager()
        -&gt;getFormObject($type, 'default')
        -&gt;setEntity($entity);

    $my_rendered_form = \Drupal::formBuilder()-&gt;getForm($form);
        $content[] = [
            '#type'    =&gt; 'html_tag',
            '#tag'     =&gt; 'h1',
            '#value'   =&gt; 'My Form Title',
            'form'    =&gt; $my_rendered_form,
        ];


It grabs and renders the form just fine. But the form has a select field and I want to alter the options for it. This is because the options should be dynamically generated based off the values of certain nodes. I already have a field called 'my_select_field' but I'm not sure how to change the values from it. When using die(print_r($my_rendered_form['my_select_field'])); I receive a very large array and the my_select_field, and the test data I put in it appears in many locations in the array.
My ultimate goal would be to accomplish something like this

$form['my_select_field']['#options'] = [
    1 =&gt; 'My new option 1',
    2 =&gt; 'My new option 2',
    3 =&gt; 'My new option 3',
];


How would I alter the options in a select field in a programmatically rendered contact form?

UPDATE: I found '#options' in the array 

        [#options] =&gt; Array
            (
                [test1] =&gt; Drupal\Core\Field\FieldFilteredMarkup Object
                    (
                        [string:protected] =&gt; Test 1
                    )

                [test2] =&gt; Drupal\Core\Field\FieldFilteredMarkup Object
                    (
                        [string:protected] =&gt; Test 2
                    )

                [test3] =&gt; Drupal\Core\Field\FieldFilteredMarkup Object
                    (
                        [string:protected] =&gt; Test 3
                    )

                [test4] =&gt; Drupal\Core\Field\FieldFilteredMarkup Object
                    (
                        [string:protected] =&gt; Test 4
                    )

            )


But the options are objects rather than strings I can edit. I should probably also mention that the select field I have is set to multiple checkboxes.

EDIT: I'm willing to use a form alter hook if that is necessary. But if that's the case the same question applies.

UPDATE 2:
I attempted altering the form using the hook form alter method like so

        $form['my_select_field']['widget']['#options'] = [
            1 =&gt; 'New option 1',
            2 =&gt; 'New option 2',
        ];


And the new options do appear, but now when I submit the form I receive the error 


  The value you selected is not a valid choice.
This is probably not a good use case for the core version of contact form based on entity forms. You have not only to take care of the validation in the form api, but also on the entity level. If you want to do this, here is a tutorial: https://chromatichq.com/blog/dynamic-default-and-allowed-values-list-fields-drupal-8

In contrast Webform provides a very flexible handling of select options. You can edit the options separately from the form in predefined options lists and then you can modify the predefined options dynamically, see these examples:

/**
 * Implements hook_webform_options_WEBFORM_OPTIONS_ID_alter().
 */

function mymodule_webform_options_test_alter(array &amp;$options, array &amp;$element) {
  $options += [
    'four' =&gt; t('Four'),
    'five' =&gt; t('Five'),
    'six' =&gt; t('Six'),
  ];
}

/**
 * Implements hook_webform_options_alter().
 */
function mymodule_webform_options_alter(array &amp;$options, array &amp;$element, $id) {
  if ($id == 'custom') {
    $options = [
      'one' =&gt; t('One'),
      'two' =&gt; t('Two'),
      'three' =&gt; t('Three'),
    ];
  }
}