Programmatically change block content on page render only I have a situation where I want to change the content of a block on certain conditions, but not permanently. 

I'm using a theme_preprocess_block() hook to change the content of the block, but it's not carrying through to the page render:

function MYTHEME_preprocess_block(&amp;$variables) {

  if ( $variables['base_plugin_id'] == 'block_content'
    &amp;&amp; $variables['attributes']['id'] == 'block-mycustomfooterblock' ) {

    $variables['content']['#block_content']-&gt;set('body', [[
      'value' =&gt; 'New Content',
      'format' =&gt; 'full_html',
    ]]);


Even after clearing caches, the block content doesn't get replaced with "New Content".

I added this line

$variables['content']['#block_content']-&gt;save();


However, that permanently saved the change to the block, which I don't want. 

How can I alter the block content just for the page render?
You should probably try

$variables['content']['body'][0]['#text']= "&lt;strong&gt;html content&lt;/strong&gt;";


complete example 

function THEMENAME_preprocess_block(&amp;$variables) {
  if ( $variables['attributes']['id'] == "block-newblock" ) {
    $variables['content']['body'][0]['#text']= "&lt;strong&gt;html content&lt;/strong&gt;";
    $variables['content']['body'][0]['#format']= "full_html";
  }  
}