How can I update text_format value in Ajax callback? I have a Drupal 8 form that includes the following fields:

// Comtainer for message fields and preview.
$form['email'] = array(
  '#prefix' =&gt; '&lt;div id="email"&gt;',
  '#suffix' =&gt; '&lt;/div&gt;',
);

// Template selection drop-down.
$form['template']['template_select'] = array(
  '#type' =&gt; 'select',
  '#title' =&gt; $this-&gt;t('Select template to use (overwrites message)'),
  '#options' =&gt; $options,
  '#default_value' =&gt; 1,
  '#ajax' =&gt; array(
    'wrapper' =&gt; 'email',
    'callback' =&gt; array($this, 'updateEmailTemplate'),
    'event' =&gt; 'change',
  ),
);

$form['email']['message']['subject'] = array(
  '#type' =&gt; 'textfield',
  '#title' =&gt; $this-&gt;t('Message subject'),
  '#default_value' =&gt; $params['subject'],
  '#ajax' =&gt; array(
    'wrapper' =&gt; 'preview',
    'callback' =&gt; array($this, 'updateEmailPreview'),
    'event' =&gt; 'change',
  ),
);

$form['email']['message']['body'] = array(
  //'#type' =&gt; 'textarea',
  '#type' =&gt; 'text_format',
  '#title' =&gt; $this-&gt;t('Message body'),
  '#description' =&gt; $this-&gt;t('Text for the email body. you may use the following tokens: @tokens.', ['@tokens' =&gt; SimpleConregTokens::tokenHelp()]),
  '#default_value' =&gt; $params['body'],
  //'#value' =&gt; $params['body'],
  '#format' =&gt; $params['format'],
);


My Ajax callback function is:

  // Callback function for Template drop down - load message fields with template.
  public function updateEmailTemplate(array $form, FormStateInterface $form_state) {
    $form_values = $form_state-&gt;getValues();
    $templates = $form_state-&gt;get('templates');

    if (!empty($template = $form_values['template']['template_select'])) {
      //Code here to determine new form values.

      $form['email']['message']['subject']['#value'] = $params['subject'];
      //$form['email']['message']['body']['#type'] = "textarea";
      $form['email']['message']['body']['#value'] = $form['email']['message']['body']['#default_value'];
      $form['email']['message']['body']['#format'] = $params['format'];
    }
    return $form['email'];
  }


So what's happening?

When the user chosen selects a template from the dropdown, the text_format doesn't update.

If I change the #type of the field from text_format to textarea, it will update without any problems, which suggests the callback should work.

I though there might be something in the text editor JavaScript preserving the text_format field from being updated, but if I explicitly set the #value in the form building function, it starts updating in the callbacks. The problem with this is that it also overrides the value when the form is submitted.

Finally, I've tried adding a line to the callback to set the #type to a textarea, and this is also getting ignored. The callback is still getting called, however, because the supbect textbox does update.

Any ideas where I'm going wrong? I'm particularly confused as to why the textarea version works perfectly but changing to text_format doesn't.

James
Don't change $form in an ajax callback. See this documentation for D7:

https://www.drupal.org/docs/7/api/javascript-api/ajax-forms-in-drupal-7


  Details and Warnings
  
  Changes to the form must only be made in the form builder function, or
  validation will fail. The callback function must not alter the form or
  any other state.


For D8 there is in the moment only documentation for the new syntax, but not for the basics. Use the D7 doc's instead.