How can I programatically change the content of a block and keep Full HTML formatting I need to programmatically change the content of a ui configured block. I've partially figured out how to do this, ie:

$ids = \Drupal::entityQuery('block_content')
  -&gt;condition('type', 'custom_block_type')
  -&gt;condition('info', 'Specific block title')
  -&gt;execute();

$my_block = BlockContent::load(array_values($ids)[0]);
$my_block-&gt;set('body','&lt;h1&gt;'.$my_processed_string.'&lt;/h1&gt;');
$my_block-&gt;save();


This does change the content, but when I do this the block loses the Full HTML block format, and the new content in printed out verbatim as html encoded content (ie, the user literally sees '&lt;h1&gt;My altered value&lt;/h1&gt;' on the screen). If I go through the ui and change the Text Format field manually to Full HTML, it renders correctly, but that doesn't really accomplish what I need to do.

How can I change the content programatically, without losing the html formatting?
The body field has at least two properties, value and format. To replace the complete field content, you have to use an array:

$my_block-&gt;set('body', [[
  'value' =&gt; '&lt;h1&gt;' . $my_processed_string . '&lt;/h1&gt;',
  'format' =&gt; 'full_html',
]]);


Normally you don't need to do this. Set only the value property:

$my_block-&gt;body-&gt;value = '&lt;h1&gt;' . $my_processed_string . '&lt;/h1&gt;';


So that you don't overwrite properties you don't want to change.